Sunteți pe pagina 1din 7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Singular Engineer
In progress. Home About Home

Type text to search here...

Home > Electronics > Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)


May 13th, 2013 singularengineer Leave a comment Go to comments Pulse Width Modulation (PWM) is used everywhere from motors to communication! It uses timer as its foundation to toggle the pins high-low at a defined period (1/frequency) and duty cycle (10 bit resolution 210 = 1024 steps). If you use high frequency, then you wont get the 10 bit resolution. Read the document bellow to find more information. Read this document and youll know a lot about PWM : http://ww1.microchip.com/downloads/en/devicedoc/31014a.pdf So in previous tutorial we saw about using a timer. Now my task is to make a LED do the heart beat effect (gradually increase/decrease the brightness of an LED using PWM). To set a PWM: Setup the timer (to the frequency/period) Set the duty cycle (you will be using the functions from pwm.h) So I am going to set the PWM frequency to 2KHz (since using 8MHz as clock source and prescale of 16, getting less than 2KHz is difficult), so the formula according to the datasheet will be (we are going to find PR2) PWM period = [(PR2) + 1] 4 TOSC (TMR2 prescale value) where PWM period = 1/ Frequency (that will be 1/2000 = .0005) .0005 = [PR2 + 1] [1 / 8000000] 16 PR2 + 1 = [.0005 8000000] / 16
singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/ 1/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

PR2 + 1 = 250 PR2 = 249 PR2 = 0xF9 ( 249 in hex)

Note : Read the datasheet and peripheral library document. PWM1 uses Timer 2. You can use other timers as source for your PWM, but read and make sure if you can or cant in the datasheet of the device you are using. To set timer2 for PWM, we just need to set the presale value
u n s i g n e dc h a rT i m e r 2 C o n f i g=T 2 _ P S _ 1 _ 1 6 ; O p e n T i m e r 2 ( T i m e r 2 C o n f i g ) ;

And then open the PWM


O p e n P W M 1 ( 0 x F 9 ) ;/ / 2 K H z

So here is the whole sample PWM program and the video below it

singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/

2/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

# i n c l u d e< x c . h > # i n c l u d e" c o n f i g . h " # i n c l u d e< p l i b / t i m e r s . h > # i n c l u d e< p l i b / p w m . h > # d e f i n e_ X T A L _ F R E Q8 0 0 0 0 0 0/ / T h es p e e do fy o u ri n t e r n a l ( o r ) e x t e r n a l o s c i l l a t o r # d e f i n eU S E _ A N D _ M A S K S i n ti=0 ; i n tc o u n t u p=1 ;/ / o u rf l a gt os e tu p / d o w nd u t y c y c l e i n tD u t y C y c l e=0 ; u n s i g n e dc h a rT i m e r 2 C o n f i g ;/ / P W Mc l o c ks o u r c e v o i dS e t u p C l o c k ( v o i d ) ; v o i dm a i n ( i n ta r g c ,c h a r * *a r g v ){ S e t u p C l o c k ( ) ;/ /I n t e r n a lC l o c kt o8 M H z T R I S C b i t s . R C 2=0 ;/ / P W Mp i ns e ta so u t p u t T i m e r 2 C o n f i g=T 2 _ P S _ 1 _ 1 6 ;/ / p r e s c a l e1 6 O p e n T i m e r 2 ( T i m e r 2 C o n f i g ) ; O p e n P W M 1 ( 0 x F 9 ) ;/ / O p e np w ma t2 K H z w h i l e ( 1 )/ / i n f i n i t el o o p { i f ( c o u n t u p= =1 ) { S e t D C P W M 1 ( D u t y C y c l e ) ;/ / C h a n g ed u t yc y c l e D u t y C y c l e + + ; i f ( D u t y C y c l e= =1 0 2 4 ) c o u n t u p=0 ; } e l s e { S e t D C P W M 1 ( D u t y C y c l e ) ;/ / C h a n g ed u t yc y c l e D u t y C y c l e ; i f ( D u t y C y c l e= =0 ) c o u n t u p=1 ; } _ _ d e l a y _ m s ( 1 ) ; } } v o i dS e t u p C l o c k ( ) { O S C C O N b i t s . I R C F 0=1 ; O S C C O N b i t s . I R C F 1=1 ; O S C C O N b i t s . I R C F 2=1 ; }

singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/

3/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

And here is the video

Categories: Electronics Tags: 18F4550 pwm, PIC18, PIC18F PWM, PWM, pwm.h, XC8 compiler Comments (5) Trackbacks (0) Leave a comment Trackback 1. singularengineer October 24th, 2013 at 15:26 | #1 Reply | Quote @Dave You are right. I missed it. Editing a post is difficult since the format messes up. Will make sure wont happen again. Thanks for looking in. 2. Dave October 24th, 2013 at 12:02 | #2 Reply | Quote Looks like you dropped the 4 in the equation. 0.0005 = [PR2 + 1] * 4 * [1/8000000] * 16 so PR2 = 61.5 3. Ruben August 30th, 2013 at 05:50 | #3 Reply | Quote
singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/ 4/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

@singularengineer Hahaha. Sorry, i forgot to write the rest of the code. I wanted to say the code line Timer2Config T2_PS_1_16. It doesnt accept the T2_PS_1_16 because timer2 isnt enabled in timers.h plib. The code its in grey unlike the timer0 that is in blue and black. 4. singularengineer August 30th, 2013 at 00:45 | #4 Reply | Quote Timer2Config is a variable (look above the main function where a bunch of variables are declared) unsigned char Timer2Config; 5. Ruben August 29th, 2013 at 19:38 | #5 Reply | Quote Hi. I cant use/access the code Timer2Config. I opened the library and i found that the corresponding code to timer2 is written in grey and dont have that comment trace and asterisk /*. Do you possibly know why that is and how i can enable the timer2 plib easy coding? Thanks in advance. 1. No trackbacks yet. Name (required) E-Mail (will not be published) (required) Website

Subscribe to comments feed


Submit Comment

four + 3 = Notify me of follow-up comments by email.


singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/ 5/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Notify me of new posts by email. Programming PIC 18 using XC8 (MPLAB X) : USART (PIC18F Serial) Programming PIC 18 using XC8 (MPLAB X) : Timers RSS

Search for:

Search

Recent Posts
Programming PIC 18 using XC8 (MPLAB X) : ADC (using adc.h) Programming PIC 18 using XC8 (MPLAB X) : USART (PIC18F Serial) Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h) Programming PIC 18 using XC8 (MPLAB X) : Timers Programming PIC 18 using XC8 (MPLAB X) : Interrupts

Recent Comments
singularengineer on Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h) aLt on STM32F4 Discovery Board running .NET MicroFramework Dave on Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h) singularengineer on Programming PIC 18 using XC8 (MPLAB X) : IO ports Sebastian on Programming PIC 18 using XC8 (MPLAB X) : IO ports

Archives
May 2013 April 2013 October 2012 September 2012

Categories
Electronics

Meta
singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/ 6/7

10/25/13

Singular Engineer Programming PIC 18 using XC8 (MPLAB X) : PWM (using pwm.h)

Register Log in Entries RSS Comments RSS WordPress.org

Categories
Electronics

Blogroll Archives
May 2013 April 2013 October 2012 September 2012

Meta
Register Log in Top WordPress Copyright 2012-2013 Singular Engineer Theme by NeoEase. Valid XHTML 1.1 and CSS 3. Follow

Follow Singular Engineer


Get every new post on this blog delivered to your Inbox. Join other followers:
Enter email address Sign me up!

singularengineer.com/programming-pic-18-using-xc8-mplab-x-pwm-using-pwm-h/

7/7

S-ar putea să vă placă și