View previous topic :: View next topic |
Author |
Message |
cengav4r
Joined: 31 May 2012 Posts: 10
|
18F4550 PWM |
Posted: Tue Dec 23, 2014 5:37 am |
|
|
I have a board which has 18F4550 with 20Mhz crystal. I want to turn right and left servo motor with respect to buttons(Low-High) or Pot value.
Now, I want to try to get a Pwm signal approximately Tp>2msec.
#use delay(clock=48000000)
setup_timer_2(T2_DIV_BY_A,B,1); //A can be 1,4,16 B can be 0-255
From using;
PWM period (Tpwm) = (PR2+1) x 4 x Tosc x TMR2 prescaler
PWM Duty Cycle=(value) x (Tosc) x TMR2 prescaler x 4
How can I get Tp>2msec with using A and B values or other ways?Thx |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Tue Dec 23, 2014 5:42 am |
|
|
You can't with the PWM.
The PWM, is designed to give _fast_ pulses without processor intervention.
Search for CCP and servo in the forum search. Code has been posted here (and may be in the code library as well), to use the CCP module for this. |
|
|
cengav4r
Joined: 31 May 2012 Posts: 10
|
|
Posted: Tue Dec 30, 2014 8:45 am |
|
|
Could you give more detail information about using CCP module? Do you also mean software PWM? I knew that CCP is capture/compare/pwm, ?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Tue Dec 30, 2014 9:30 am |
|
|
Not software, or hardware PWM.
The CCP module does three separate functions. Counter. Compare. PWM.
Now the counter is obvious (it counts). The PWM similarly develops a PWM signal. However this only uses a 8+2bit counter (two directly from the master clock, as opposed to the normal Fosc/4). This is designed to give fast pulse widths. The key is the middle C, which is the 'compare' functionality. This allows the hardware to trigger an 'event' based upon a timing function, and offers full 16bit counting, from the Fosc/4 source (or a division of this - so supports frequencies 256* lower than the hardware PWM). Hence the CCP module in compare mode, can be used to generate lower frequency 'PWM' functionality, with hardware accuracy. It needs some software, but 99% of the work is done by the hardware.
Code to do this for a servo has been published here. |
|
|
|