#include #include #include typedef unsigned char u08; //PB0->LED, PB1->Relle //Phone KeyPad PB2->3, PB3->6, PB4->9 //======================= 63, 66, 69, 93 ========== // | | | | const u08 PROGMEM dig1[]={ 8, 8, 8, 16}; const u08 PROGMEM dig2[]={ 4, 8, 16, 4}; u08 get_event(void); void delay1(u08); void blinkled(void); int main (void) { u08 e, pe; pe = 0; //set PORTB for output DDRB = 0xFF; //set PORTD as input DDRD = 0; //off all output bits PORTB = 0; PORTB |= 1 << 0; //Alarm LED OFF //Main loop while (1) { e = get_event(); if (e != pe) { if (e) { PORTB &= ~(1 << 0); //Alarm LED ON delay1(20); //Wait PORTB |= 1 << 1; //Hook ON delay1(40); //Wait PORTB |= pgm_read_byte(&dig1[e-1]); //Press key 1 delay1(5); PORTB &= ~pgm_read_byte(&dig1[e-1]); //Leave key1 delay1(5); PORTB |= pgm_read_byte(&dig2[e-1]); //Press key 2 delay1(5); PORTB &= ~pgm_read_byte(&dig2[e-1]); //Leave key 2 delay1(160); PORTB &= ~(1 << 1); //Hook OFF } else { PORTB &= 0xe0; //PB0 - PB4 OFF //PORTB |= 1 << 0; //Alarm LED OFF } } pe = e; blinkled(); } return 1; } u08 get_event(void) { u08 k, evnt=0, tmp, counter; k = PIND; k &= 0xf; if (k < 15) { counter = 0; while (counter != 2) { _delay_loop_2(50000); counter++; } k = PIND; k &= 0xf; if (k < 15) { evnt = ~k; evnt &= 0x0f; switch (evnt) { case 1: tmp = 1; break; case 2: tmp = 2; break; case 4: tmp = 3; break; case 8: tmp = 4; break; //default: // tmp = 15; } evnt = tmp; } } return(evnt); } void delay1(u08 times) { u08 counter; counter = 0; while (counter != times) { _delay_loop_2(50000); counter++; } } void blinkled(void) { if (PORTB & 1) //If Led is OFF { PORTB &= 0xfe; //Led ON delay1(1); } else { PORTB |= 1 << 0; //Led OFF delay1(15); } }