/* EX 2.4 Debounce Light Switch */ /* David Miles April 2006 */ #include /* Set 8 MHz clock frequency */ #pragma CLOCK_FREQ 8000000 /* Set PIC16 configuration word */ #pragma DATA _CONFIG1, _EXTRC_CLKOUT & _WDT_OFF & _LVP_OFF unsigned char key ( void ) { unsigned char count = 0 ; unsigned char oldv, newv ; oldv = portb & 0x01 ; while ( count < 20 ) { newv = portb & 0x01 ; if ( oldv == newv ) { count++ ; } else { count = 0 ; oldv = newv ; } } return oldv ; } void main ( void ) { trisa = 0xfe ; /* set bit 0 of PORTA for output */ trisb = 0xff ; /* set all of PORTB for input */ ansel = 0x00 ; /* set all of PORTA in digital mode */ /* repeat forever */ while (1){ /* wait for the button to go down */ while (key () == 0); porta ^=0X01; /* wait for the button to go up again */ while (key() == 1); } }