PDA

View Full Version : Changing Freq of HPWM



Rayneobid
- 12th April 2005, 18:55
Hello,
I've been trying to implement a siren sound using the PWM module in a 16F77 as shown in the code below. The dutycycle needs to stay at about 50%, while the frequency goes up and down.
It functions and sounds ok with the exception of about a 5ms dead time that gets inserted into the output at apparently random times. This can be heard in the speaker as a "click" and seen on a scope. I'm using a 4.00Mhz resonator and have the WDT disabled.
I have tried to narrow down just where this dead time comes from by toggling a pin right after the "Pause 25" and the dead time appears right after the pin change, but again not all the time.

Any ideas?
Thanks!

Include "modedefs.bas"

StartSound:
TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
CCP1CON = %00001100 ' Set CCP1 to PWM
T2CON = %00000111 ' Turn on Timer2, Prescale=16
PR2 = 150
CCP1CON.4 = 0
CCP1CON.5 = 0
SoundUp:
Pause 25
PR2 = PR2 + 1
CCPR1L = PR2 >> 1
IF PR2 < 156 Then SoundUp
GoTo SoundDown
SoundDown:
Pause 25
PR2 = PR2 - 1
CCPR1L = PR2 >> 1
IF PR2 > 52 Then SoundDown
GoTo SoundUp

End