Home   Simple Keypad


Embedded C code for Simple Keypad


Sample code for Simple Keypad.
Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.

 

#include <avr/io.h> #include <util/delay.h> #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif void initports(void); int main(void) { initports(); while(1) { if(bit_is_clear(PINA,0)) { _delay_ms(5); // debounce delay if(bit_is_clear(PINA,0)) { cbi(PORTB,0); sbi(PORTB,1); sbi(PORTB,2); } else { } } else { } if(bit_is_clear(PINA,1)) { _delay_ms(5); if(bit_is_clear(PINA,1)) { sbi(PORTB,0); cbi(PORTB,1); sbi(PORTB,2); } else { } } else { } if(bit_is_clear(PINA,2)) { _delay_ms(5); if(bit_is_clear(PINA,2)) { sbi(PORTB,0); sbi(PORTB,1); cbi(PORTB,2); } else { } } else { } } return 0; } void initports(void) { cbi(DDRA,0); //set A.0 as input cbi(DDRA,1); //set A.1 as input cbi(DDRA,2); //set A.2 as input sbi(DDRB,0); //set B.0 as output sbi(DDRB,1); //set B.1 as output sbi(DDRB,2); //set B.2 as output PORTB = 0xFF; //turn off all LEDs return; }