PICmicro & dsPIC >> PICmicro & dsPIC

Pages: 1 | 2 | >> (show all)
906
stranger


Reged: Nov 17 2007
Posts: 14
how to calculate the PWM period?
      #30789 - Thu Nov 22 2007 05:34 PM

How can I calculate a time base (period) and duty cycle for PWM output? If I use PIC16F877A and 20MHz for crystal

CCP1CON = 0b00001100; // LSB = 00, set to PWM mode
CCP2CON = 0b00001100;
T2CON = 0b01111111; // Postscaler = 16; Prescaler = 16
PR2 = 0xFF; // PR2 value set to 0xFF, 255
CCPR1L= 0x60;
CCPR2L= 0x60;

Is it correct?
PWM period = [PR2+1]*4*Tosc*(TMR2 Prescale Value)
= (255+1)*4*(1/20M)*16
= 0.8192ms or 1.22 kHz

How can I calculate the duty cycle?
PWM duty cycle = (CCPR1L:CCP1CON)*TOSC • (TMR2 Prescale Value)?

Besides that, how can I set the duty cycle for “on” and “off” in PWM mode?

Edited by 906 (Thu Nov 22 2007 05:36 PM)


Post Extras: Print Post   Remind Me!   Notify Moderator  
Frank Everest

***

Reged: Oct 16 2003
Posts: 1294
Loc: Herts, UK
Re: how to calculate the PWM period? [Re: 906]
      #30792 - Thu Nov 22 2007 08:36 PM

Section 8 of the 16F87X data sheet should answer all your questions.

Post Extras: Print Post   Remind Me!   Notify Moderator  
906
stranger


Reged: Nov 17 2007
Posts: 14
Re: how to calculate the PWM period? [Re: Frank Everest]
      #30797 - Fri Nov 23 2007 12:08 AM

Thanks, I had read it. It’s useful, but I don’t understand the formula for PWM duty cycle.

PWM duty cycle = (CCPR1L:CCP1CON<5:4>)*TOSC • (TMR2 Prescale Value)

What is the meaning of <5:4>?

If I set my CCPR1L = 0x60

Is it?
PWM duty cycle = 96*5/4*(1/20M) • (16)
= 0.096ms

Can anyone explain further? Thanks….


Post Extras: Print Post   Remind Me!   Notify Moderator  
Dan HenryModerator
Guru
****

Reged: Oct 16 2003
Posts: 4622
Loc: Colorado
Re: how to calculate the PWM period? [Re: 906]
      #30804 - Fri Nov 23 2007 03:26 AM

Quote:

What is the meaning of <5:4>?



That is described in data sheet section 8.3.2.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Frank Everest

***

Reged: Oct 16 2003
Posts: 1294
Loc: Herts, UK
Re: how to calculate the PWM period? [Re: Dan Henry]
      #30808 - Fri Nov 23 2007 04:47 AM

Quote:

What is the meaning of <5:4>?




...specifically, it means bits 5 & 4 of CCP1CON, whose function in turn is clearly described in (and this is a quote from the data sheet):
Register 8.1: CCP1CON REGISTER/CCP2CON REGISTER (ADDRESS 17h/1Dh)
...
bit 5-4 CCPxX:CCPxY: PWM Least Significant bits
Capture mode:
Unused.
Compare mode:
Unused.
PWM mode:
These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPRxL

...etc.


Post Extras: Print Post   Remind Me!   Notify Moderator  
906
stranger


Reged: Nov 17 2007
Posts: 14
Re: how to calculate the PWM period? [Re: Frank Everest]
      #30817 - Fri Nov 23 2007 07:11 PM

Thanks, I understand after your explanation. Last question (sorry), is my calculation correct? I just want to confirm it. Thanks

If I set my CCPR1L = 0x60
PWM duty cycle = 96*(1/20M) * (16)
= 76.8u second?


Post Extras: Print Post   Remind Me!   Notify Moderator  
Frank Everest

***

Reged: Oct 16 2003
Posts: 1294
Loc: Herts, UK
Re: how to calculate the PWM period? [Re: 906]
      #30818 - Fri Nov 23 2007 07:46 PM

Quote:

PWM period = [PR2+1]*4*Tosc*(TMR2 Prescale Value)
= (255+1)*4*(1/20M)*16




Quote:

PWM Duty Cycle =(CCPR1L:CCP1CON<5:4>) •
TOSC • (TMR2 Prescale Value)




Quote:

PWM duty cycle = 96*(1/20M) * (16)
= 76.8u second.




Substituting your numbers (and assuming CCP1CON <5:4> are zero) seems to confirm 76.8usec. But why did you doubt it? Why not try in your hardware and have a look with a 'scope?


Post Extras: Print Post   Remind Me!   Notify Moderator  
906
stranger


Reged: Nov 17 2007
Posts: 14
Re: how to calculate the PWM period? [Re: Frank Everest]
      #30821 - Fri Nov 23 2007 10:32 PM

Thanks, i gain a lot here.But i do not have that equipment, even at school.

Post Extras: Print Post   Remind Me!   Notify Moderator  
Frank Everest

***

Reged: Oct 16 2003
Posts: 1294
Loc: Herts, UK
Re: how to calculate the PWM period? [Re: 906]
      #30822 - Fri Nov 23 2007 11:14 PM

Quote:

But I do not have that equipment



Ok, so do you have a multimeter? If so, set the duty cycle to 50%, run the program in the device, and verify that the output pin voltage measured on the multimeter sits at half the supply voltage. If so, you've got a working program.


Post Extras: Print Post   Remind Me!   Notify Moderator  
906
stranger


Reged: Nov 17 2007
Posts: 14
Re: how to calculate the PWM period? [Re: Frank Everest]
      #30829 - Sun Nov 25 2007 01:59 AM

Thanks,Frank Everest.

Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1 | 2 | >> (show all)



Extra information
0 registered and 38 anonymous users are browsing this forum.

Moderator:  mikerj, jtemples, jeff, Dan Henry, Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 9686

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5