Continue to Site

# PWM PIC16F877 Doubts need to be cleared

Status
Not open for further replies.

#### zhizhi16

##### New Member
Sigh.. Can't seem to get my PWM working as i wanted. I've set all the registers

correctly it worked with the example i got on the net.

I am trying to practice on PWM generation. I was thinking of generating Musical Notes

C D E F G A B. They have frequencies 523.3Hz, 587.3Hz, 659.3Hz ..... 987.8Hz. The

sound is produced by a piezo buzzer.

Fosc = 4Mhz

To calculate Period:
=============
PR2 = [(Fosc / 4* TMR2 prescale value * Fpwm)] -1

eg. Note C 523.3Hz

PR2 = [(4000000/4*16*523.3)]-1
= 118.4

To calculate duty cycle:
===============
Resolution = [log(Fosc/Fpwm) / log(2)]

eg. Note C 523.3Hz

Resolution = [log (4000000/523.3) / log(2)]
= 12.90 bits

Here are some of my doubts:
1. How do we know what prescaler value to use?
- My guess, its anything we want, its used for keeping value of PR2 to be <255.

2. Duty Cycle since PIC16F877 is a 10bit PWM.
The max is 10bits? Thus the above i would round it off to 10bits?

3. Is there a maximum frequency that can be generated with this PIC16F877 with a

crystal(Fosc) 4Mhz?
- can generate 0.0000001 Hz?
- can generate 999999999999 Hz?

4. If im running a motor how much should i set the Fpwm? Again is there a minimum or

maximum?

Heres my PWM configuration(in C):
ccp1con = 0
t2con = 0
set_bit(t2con, X) //set timer2 prescaler

pr2 = PWMPeriod calculated as above

ccpr1L = 0xFF (Can some one tell me what value should i configure
this to? what is it for?)
ccp1con = XXXbits

set_bit(pie1, 1) //timer2 interrupt enable
set_bit(t2con, 2) //turn on timer2

Thank u for all ur sincere help

Status
Not open for further replies.