/************************************************************************/ /* */ /* Key + LED over the same wire (4*) */ /* */ /* Author: P. Dannegger */ /* */ /************************************************************************/ #include typedef unsigned char u8; typedef signed char s8; typedef unsigned short u16; typedef signed short s16; typedef unsigned long u32; typedef signed long s32; #define NADA() asm volatile(""::) // do nothing, but immediately #define NOIL(fkt) __attribute__ ((noinline)) fkt; fkt u8 key_state; // debounced key state: // bit = 1: key pressed u8 key_press; // key press detect static void wait10ms( void ) // static to avoid code doubling ! { u16 i = 20000; // 10ms at 8MHz do NADA(); while( --i ); } NOIL( void wait7( void)) // 7 cycle { NADA(); // avoid removing completely ! } static u8 scan_tast( void ) { u8 saveport = PORTB; u8 keys = 7; // >= 7 to avoid loop unrolling ! PORTB = 0xFF; // weak pull ups on DDRB = 0; ACSR = 1<1: key press detect } static u8 get_key_press( u8 key_mask ) { // cli(); key_mask &= key_press; // read key(s) key_press ^= key_mask; // clear key(s) // sei(); return key_mask; } int main( void ) { DDRB = 0xFF; PORTB = 0; for(;;){ wait10ms(); // debouncing time key_poll(); PORTB ^= get_key_press( 0xFF ); // toggle on key press } }