Hi Freaks,
I am trying to use a simple switch on PB0 interrupt pin on my M48. When the switch is pressed, I have a speaker connected to PC0 which should beep. I have already proven the beeping code and it works. However, my interrupts are not working. Here is my code:
#include#include #include #include #include //switch on PB0 PCINT0 interrupt //10K pull up from PB0 to supply //0.1uF cap to ground for switch debouncing //Disable CKOUT for PB0 volatile unsigned char switch_on = 0; volatile unsigned char temp; volatile unsigned char temp1; void beep() { for(int i=0;i<500;i++) { PORTC = 0x01; _delay_ms(0.5); PORTC = 0x00; _delay_ms(0.5); } } void delay() //delay for beeping { _delay_ms(500); } void ext_interrupt_init() { PCMSK0 |= (1 << PCINT0); PCICR |= (1 << PCIE0);//enable interrupt flag sei(); } ISR(PCINT0_vect) { unsigned char switch_on = 0; //use debouncing temp = PINB & (0x01); _delay_ms(100); temp1 = PINB & (0x01); //set switch_on flag here if (temp == temp1) switch_on = 1; } int main(void) { DDRB = 0xFF; DDRC = 0xFF; ext_interrupt_init(); for(;;) { if (switch_on) { for(int j=0; j<10; j++) { delay(); beep(); switch_on = 0; } } } }
I am using a very simple debouncing logic.Other than that, what could be wrong in the above code?
(I am working on making a more efficient timer based beep, but for now please ignore the beeping logic, I have another post for that)
Any help appreciated.