View previous topic :: View next topic |
Author |
Message |
Eakaphon Guest
|
Please teach me how to use PWM built-in function in 16F877!! |
Posted: Tue Aug 16, 2005 10:03 pm |
|
|
Please teach me how to use PWM built-in function in 16F877!!
I'm very newbie in PIC, could someone give me some advise please?
1) I want to control the speed of DC motor (1-direction) by built-in function of PCH (version 3.18) ==> setup_ccp2(PWM), give me some advise please.
2) When I read the manual, it said that I need to set the timer2 first but I don't understand anything in it..please explain me
2.1) What is the meaning of each parameter?
2.2) How can I calculate the frequency and duty cycle fo PWM.
3) It's very kind of you if you can give some example how to set PWM in 40Khz. 50% duty cycle using CCP2. (And a little description)
Thank you very much and sorry for bothering you.
Regards,
Eakaphon (Thailand) |
|
|
Eakaphon
Joined: 16 Aug 2005 Posts: 2 Location: Thailand
|
|
Posted: Tue Aug 16, 2005 10:25 pm |
|
|
Oh!! sorry... My PCW and PCM version 3.18 too.
Thanks.
Eakaphon |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Eakaphon
Joined: 16 Aug 2005 Posts: 2 Location: Thailand
|
|
Posted: Wed Aug 17, 2005 1:29 am |
|
|
Thanks very much for your fast reply!!!
Eakaphon |
|
|
digitalfreak
Joined: 16 Feb 2005 Posts: 25
|
|
Posted: Thu Sep 01, 2005 2:05 pm |
|
|
I kind have the similar question...How do I set duty cycle?
checked the above links
according to your formula
50% duty cycle is half of the (PR2+1) right?
then example in the CCS reference manual says something like
Code: | duty cycle* Crystal frequency
duty value = ------------------------------------- = duty cycle*(PR2+1)*4
PWM frequency * t2div |
yours is off by factor 4, and i know yours is correct!.
what am i missing? can you please clarify
thx for your time. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Thu Sep 01, 2005 2:40 pm |
|
|
The 'key', is that the duty cycle setting, is one of the few clocks inside the PIC that internally is capable of using the raw 'clock', rather than the clock/4. There are two extra bits, that allow the duty cycle to access these extra divider bits.
If you set up a PWM, with the divisor at 255, so it counts 0-255 and round (256 cycles of it's incoming clock), the half duty value needed in the CCPR1L register would be 128 (the obvious answer). _But_ the actual value used for the comparison, is the 8bits from this register, concatentated with two more bits from the CCP1CON register, to give a 10 bit value. These extra bits are attached at the bottom of the resulting number, and compared with two extra bits of 'count' from the prescaler feeding timer2.
Now the set_pwm_duty command, looks at the size of the value you send it, to see if you want to access these extra bits. So if you use:
Code: |
int8 duty;
duty=128;
set_pwm_duty(duty);
|
With the PWM set up as described, you will get the 50:50 duty. However, if you use;
Code: |
int16 duty;
duty=512L;
set_pwm_duty(duty);
|
You will also get the 50:50 ratio!. The compiler sees that the value being passed is a 'long' integer (and hence more than 8bits long), and switches to handling the extra two bits.
So the value you need, depends on whether you want to get the finer resolution these extra counter bits give you, and use the larger int16 for the handling or not.
Best Wishes |
|
|
digitalfreak
Joined: 16 Feb 2005 Posts: 25
|
|
Posted: Fri Sep 02, 2005 12:04 pm |
|
|
thx a lot to PCM and Ttelmah |
|
|
|