CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Please teach me how to use PWM built-in function in 16F877!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eakaphon
Guest







Please teach me how to use PWM built-in function in 16F877!!
PostPosted: Tue Aug 16, 2005 10:03 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Tue Aug 16, 2005 10:25 pm     Reply with quote

Oh!! sorry... My PCW and PCM version 3.18 too.

Thanks.
Eakaphon
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 16, 2005 11:14 pm     Reply with quote

Quote:
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.

http://www.pond.ie/techinfo/x003/

Quote:
2.2) How can I calculate the frequency and duty cycle fo PWM.

http://www.ccsinfo.com/forum/viewtopic.php?t=17993

Quote:
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)

http://www.ccsinfo.com/forum/viewtopic.php?t=21490
Eakaphon



Joined: 16 Aug 2005
Posts: 2
Location: Thailand

View user's profile Send private message MSN Messenger

PostPosted: Wed Aug 17, 2005 1:29 am     Reply with quote

Thanks very much for your fast reply!!!

Eakaphon
digitalfreak



Joined: 16 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Thu Sep 01, 2005 2:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 01, 2005 2:30 pm     Reply with quote

I think your question is about using 8-bit or 10-bit resolution
for the PWM duty cycle. See the following thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=18259
Ttelmah
Guest







PostPosted: Thu Sep 01, 2005 2:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 02, 2005 12:04 pm     Reply with quote

thx a lot to PCM and Ttelmah
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group