Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM Woes... not PMS - Help needed PIC16F877

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.

Can some one please help me see if i missed out anything

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

Thus I loaded 0x76.


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
 
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.
This is correct. In your example above, prescale value is 16.

2. Duty Cycle since PIC16F877 is a 10bit PWM.
The max is 10bits? Thus the above i would round it off to 10bits?
Maximum is 10 bits. That happens when the PR2 value is 0FFh. Resolution is reduced if the value of PR2 is lowered. For example, if PR2 is 80h, then the resolution drops to 9 bits. If PR2=40h, resolution is 8 bits.

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?
Maximum is 500Khz.
PR2 period = (1+1)*1uSec*1(prescale)=2uSec or 500Khz.

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

I don't fully understand your question. Nevertheless, my recommendation is to use a 20Mhz crystal and run the Fpwm at about 20Khz to keep the chopping frequency above audible range.

Finally, a better way to generate a square wave is to use the compare mode instead of the PWM mode. This offers a wider and finer frequency range. Use interrupts to toggle between set and reset the output pin.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top