Continue to Site

# need help on generating a pic16F887 frequency generator through pwm

Status
Not open for further replies.

#### timi

##### Junior Member level 2
Hi guys,

i am tying to make up a program to calculate the PR2 and Timer2 prescaler for a given frequency received by RS232.

i have the rs232 part of getting to read the data, but i am short of finding the right algorithm for the finding of the optimal:
PR2, Timer2 Prescaler.

Can anyone help me out.

Timi,

RS-232 is the standard for serial communication. Are you receiving some required frequency input serially???
And based on that value you want to generate the frequency by calculating the PR2 value???

If you are going for the serial input then follow these steps,

1. Make sure you first convert it from ASCII to integer value..
2.Use PR2=(PWM)/(4*Tosc*TMR2 Prescale) Select PWM based on duty cycle i.e 100% is (11 1111 1111)bit
and put a variable for TMR2 Prescale, make a poll for 1,4,16 by restriction on PR2 not more than (1111 1111)bit / 256 Decimal. 8-bit register.

Hope that helps..
-M. Aakif Raza

Hi makifraza,

thanks a lot that is what i was searching for. i will try to make the code into the pic and see what i can do about it.

regards,

Timi

I missed a little in last post. The correct formula for PR2 is
Use PR2= ( (PWM)/(4*Tosc*TMR2 Prescale) ) - 1

Also reference from Datasheet. The pwm section is given at Page 130. Topic 11.5 And the Equation is 11-1

and yes give it a few tries, if still you have any problems just post your code on the forum. :smile:

timi

Points: 2