Hello,
Can anyone explain to me how to control buzzer via timer interrupts? I want to play different tones on a buzzer using timer interrupts. So far I have this code:
Init timer:
//initialize and start Timer #0 T1TCR = 0x00000002; //disable and reset Timer0 T1PC = 0x00000000; //no prescale of clock T1MR0 = FOSC / notes[note]; //calculate no of timer ticks 7.6ms T1IR = 0x000000ff; //reset all flags before enable IRQs T1MCR = 0x00000003; //reset counter and generate IRQ on MR0 match //initialize VIC for Timer0 interrupts VICIntSelect &= ~0x20; //Timer1 interrupt is assigned to IRQ (not FIQ) VICVectAddr5 = (tU32)timer1ISR; //register ISR address VICVectCntl5 = 0x25; //enable vector interrupt for timer0 VICIntEnable |= 0x20; //enable timer1 interrupt T1TCR = 0x00000001; //start Timer1
timerISR:
tU16 notes[] = {262, 294, 330, 349, 392, 440, 494, 523}; //C D E F G A B C int note = 0; static void timer1ISR(void) { if(note < 8) note++; else note = 0; setLED(LED_GREEN, toggleC == 0 ? FALSE : TRUE); setBuzzer(toggleC == 0 ? FALSE : TRUE); toggleC = ~toggleC; countC++; if(countC == notes[note]) { T1TCR = 0x02; T1MR0 = FOSC / notes[note]; T1TCR = 0x01; countC = 0; } T1IR = 0x000000ff; //reset all IRQ flags VICVectAddr = 0x00000000; //dummy write to VIC to signal end of interrupt }
But it only gives me only one tone and I don't know why :( I'm new to ES programming :(
You say buzzer.
Do you mean a piezoelectric buzzer?
They are designed to be fed DC and generate their own frequency.
A small loudspeaker, on the other hand, is designed to get AC or pulses and sound at the frequency of the AC or the pulse frequency.
Yep, it's piezoelectric buzzer. Isn't there any way to play different tones on it using timer/timer interrupts?
Not if it is the type where you just give it a supply, and it generates the tone itself.
You need a speaker - not a buzzer (piezoelectric or otherwise).
Isn't there any way to play different tones on it using timer/timer interrupts?
I wonder how you managed to arrive at the conclusion that you should use that buzzer, but without learning enough about it along the way to let you answer this question yourself.