Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PWM duty cycle using PIC

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Hi

I have been reading the data sheet regarding the PWM duty cycle and Im not entirely 100 percent sure about it because I could not calculated the value of maximum duty cycle in order to determine (1F) 25% duty cycle.

The detail of code c an be found in page 14-14 from this link:

https://ww1.microchip.com/downloads/en/DeviceDoc/31014a.pdf


I would appreicate someone explain me how to calculate the PWM duty cycle at the 78.125kHz.

Regards

MM

PS If the 25% duty cycle is used, the value should be 3F instead of IF?

Cuz the maximum resolution is 8 bits??
 

You are using Mid-range PIC, aren't you?

In PWM mode, both switching frequency and duty cycle are described in 10-bit. However, you could only specify 8 highest bits for the frequency, by using 8-bit register PR2. The 2 lowest bits are internally concatenated to PR2.

For duty cycle, you specify the 8 highest bits in CCPRxL register (x is the number of the CCP module), and the 2 lowest bits in the CCPxCON<5:4> bits.

Therefore, you can have 10-bit resolution for duty cycle, but can only get 8 highest bits set for the frequency.

In the example in the document '31034a.pdf' at page 14-14, you would need to set CCPR1L to 0x10, and CCP1CON<5:4> to '00', to get a 25% duty cycle, with PR2 set to 0x3F.

Hope this helps!

Cheers,
 
Sorry...

I still don't understand. Would you please kindly show me the calculation in order to determine the PWM duty cycle?

MM
 

ok, let's say we have 20 MHz clock frequency as in the .pdf file.

At PWM frequency as 78.125 kHz, your number of count for PWM period
would be: 20,000,000/78,125 = 256 = 0x100.
You will put the highest 8 bits minus one to PR2, 'cause (PR2 + 1) = (PWM period)/4.
8 highest bits are 0x40, so you should put 0x3F into PR2.

For duty cycle of 25%, your number of count would be 256*0.25 = 64 = 0x40.
That will be the number to be put into CCPR1L:CCP1CON<5:4>.
So CCPR1L will contain 8 highest bits, which are 0x10, and CCP1CON<5:4> will contain the 2 lowest bits, which are '00' in this case.

Note that we are considering the counts as 10-bit values.

With this PWM frequency, you can only have 256 counts for the period, and you maximum duty cycle will be that number, or 8-bit PWM resolution.

You can have 10-bit resolution, if you use 4 times smaller PWM frequency, i.e. about 19.5 kHz.

Cheers,
 
Hi

Picking up an old thread.

Still can see how you can have 10-bit resolution on the PWM frequency when it is assinged by an 8-bit register PR2?

Thnak you
 

Hii namqn,

Good work..really u explained PWM quite simple n clear...
Cheers...

Regards
Kawal
 

Hi frnd

I am varying dutycycle in PC (0-255 in 156khz), PR2=0x1F;
in my program,using CCPR1L=receivevalue/8; for every 8 bit changing PWM duty cycle.
But i need every 2bit changing PWM dutycycle at 156khz. How to change it?
 

can i noe hw can set 50% duty cycle using serial communication of PIC 16F877a
 

If u receive 50 (any other data) from serial communication, this value directly move to CCPR1L.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top