View previous topic :: View next topic |
Author |
Message |
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
Using #int_timer2 to on/off buzzer |
Posted: Fri Dec 19, 2003 1:37 am |
|
|
Mabuhay!
I have a problem with the code below... it would not generate a monotone on my buzzer.. anybody in the forum who can check and review my code.. I plan to generate a 100Hz monotone using interrupt timer2 at PortA4 of PIC18F452.
The code listed below:
Code: | #include <18F452.h> // Target PIC Microcontroller IC
#device *=16 // use a 16-bit pointer
#device ICD=TRUE
#fuses HS,NOPROTECT,NOWDT // PIC MCU Configuration
#use delay(clock=20000000) // 20MHz Crystal clock speed
//**********************************************************************
// Configure PortA4 for buzzer output
#bit BuzzBIT=0xF80.4
const int tones[] = { 70,74,76,83,88,94,99,105,112,118,126,133,141 };
int tone_reqd;
//**********************************************************************
#int_timer2
void tick(void) {
static int tick;
static int toggle;
if (tick) --tick;
else {
tick=tones[tone_reqd];
toggle^=1;
BuzzBIT=toggle;
}
}
//**********************************************************************
main()
{
setup_timer_2(T2_DIV_BY_4,40,1);
enable_interrupts(int_timer2);
enable_interrupts(global);
tone_reqd=1;
while (1) ;
} |
I'll appreciate any help.
Thank you. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Dec 19, 2003 8:58 am |
|
|
Didn't look at your code, but RA4 stands out. That is the first thing I look for when the hardware guys do designed. They always forget to add a pullup. Refer to the datasheet if you are trying to drive some device. You will need to add a pullup resistor or tie one pin of the buzzer to 5V. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Dec 19, 2003 9:10 am |
|
|
It also look like your frequecy is around 156Hz.
20000000/4 = 5000000 = internal clock
5000000/4 = 1250000 = timer2 increments
1250000/(256-40) = 5787 = timer2 interrupts
5787/74 = 78 is the 1/2 frequecy
Now I am not sure what you are trying to drive, but I don't think it is a subwoofer That sounds like too low a frequency. Most piezo's are in the KHz. |
|
|
rrb011270
Joined: 07 Sep 2003 Posts: 51
|
|
Posted: Sun Dec 21, 2003 6:37 pm |
|
|
Mark wrote: | Didn't look at your code, but RA4 stands out. That is the first thing I look for when the hardware guys do designed. They always forget to add a pullup. Refer to the datasheet if you are trying to drive some device. You will need to add a pullup resistor or tie one pin of the buzzer to 5V. |
Based on the data sheet, the RA4 is an open drain output... so I use the buzzer as a pull-up with a diode protection. I guess this will work coz wen I invoke output_low the buzzer will sound... there is no need for a resistor as pull-up coz the buzzer will serve similar to a resistor... |
|
|
|