//**************************************************************** // 7SEGLED.C Driving a single 7-Segment LED // // // Version: 1.0 // // // By: J. Winpenny // // // // // Notes: // 7 segment led on PORT D ( DP not used ) // Two push switches on PORT B, bits 0, 1 // // //**************************************************************** #define COMMON_CATHODE_LED // ***** #define COMMON_ANODE_LED ******* #define LED_PORT PORTD char TRISD@0x88; char PORTD@0x08; void DriveLED( char Number ); //*********************************** // PORT Connections for LED //*********************************** // Segment of LED //*********************************** // g f e d c b a // // Bit position 6 5 4 3 2 1 0 //*********************************** void main(void) { char count; TRISD = 0; // Set PORT D to all outputs TRISB = 0x03; // PORT B bits 1, 0 are inputs count = 0; DriveLED( count ); // Start with LED showing Zero while(1) // Poll swithes non-stop { //******************************************** // if input switch on PORTB bit 0 goes low //******************************************** if ( input_pin_port_b(0) == 0 ) { //***************** count++; // Count up //***************** DriveLED( count ); while ( input_pin_port_b(0) == 0 ) { //************************** // Wait for switch release //************************** } delay_ms(10); // Debounce time // Could check the pin again here if ( count >= 9 ) count = 0; } } } //************************************************** // Display a number 0 - 9 ( binary ) on a 7 SEG LED //************************************************** void DriveLED( char Number ) { // Array for Numbers 0 thru 9 #ifdef COMMON_CATHODE_LED char CommonCathode7SEG[] = { 0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67 }; #endif #ifdef COMMON_ANODE_LED char CommonAnode7SEG[] = { 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18 }; #endif if ( Number < 10 ) // So we don't exceed the array bounds { // Light segments for number n #ifdef COMMON_CATHODE_LED LED_PORT = CommonCathode7SEG[Number]; #endif #ifdef COMMON_ANODE_LED LED_PORT = CommonAnode7SEG[Number]; #endif } }