PWM Setup For 1Khz

Status
Not open for further replies.

Suraj143

Active Member
I want to generate 1Khz PWM frequency with 50% duty cycle.OSC is 4Mhz.

PWM Period=(PR2+1)*4*TOSC*TMR2 PreScaller

Those are the values I'm going to use.

PR2=00h
Pre Scaller=1:1

PWM Duty Cycle=[CCPR1L:CCP1CON<5:4>]*TOSC *TMR2 PreScaller

CCPR1L:CCP1CON=b'00000000 10' ; D'2'

I'm unsure will the PWM module works because PR2 is 00h.
 
I don't understand how you plugged in your values and got 1KHz:

(0+1)*4*(1/4,000,000)*1 = 1MHz (not 1KHz)

The calculator Jim posted works.
 
To work out the timer2 value do the instruction speed divided by the period. 1,000,000/1,000 = 1000. Timer2 can't count to 1,000 but it can count to a quarter of that so set timer2 prescaler to 4. Our period is then 250 counts of timer2 so we set PR2= 250-1 = 249. To get 50% duty cycle ccprl must be 50% of PR2 so we set it to 125. Simple really.

Mike.
 
Last edited:
Hi thanks for pointing me out Mike & upand_at_them.I understood but that software generating incorrect duty cycle value.

This is the result of that pwm calculator

/*
* PWM registers configuration
* Fosc = 4000000 Hz
* Fpwm = 1000.00 Hz (Requested : 1000 Hz)
* Duty Cycle = 50 %
* Resolution is 10 bits
* Prescaler is 4
* Ensure that your PWM pin is configured as digital output
* see more details on Microcontrollers projects, circuits and source code examples
* this source code is provided 'as is',
* use it at your own risks
*/
PR2 = 0b11111001 ;
T2CON = 0b00000101 ;
CCPR1L = 0b01111100 ;
CCP1CON = 0b00111100 ;

The duty cycle is 499uS not 500uS thats what I mentioning.
 
Try,

CCPR1L=125
CCP1CON=0b00001100

Mike.
 
Last edited:
I like it when I can do a calculation in my head and get it right and a calculator gets it wrong.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…