Basic decode for 4 character 7-segment common cathode display. 57-1093, anodes connected to Port D and segments controlled from port C0-C3.
// // main.c - A simple example 7-segment LED control program. // This program use a Table Lookup approach to decode a 7-segment LED // multi-digit LED dsiplay. // // Written by Richard HAyes - last modified 23-03-2012 // // The following header file defines various values that are // relevant to this chip, such as port names, etc #include "p18f4620.h" // Configure some global chip settings, such as oscillator type and clock speed // #pragma config OSC = INTIO67 // Sets the oscillator mode to HS #pragma config WDT = OFF // Turns the watchdog timer off #pragma config MCLRE = OFF // Turns low voltage programming off // Configure Pins. void configure_pins(void); void main(void) { // Declare variables int n; int delay = 8; char LUT[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67}; int number[4]={1,2,3,5}; int N=3; int M=4; int L=8; int K=1; LATC=0x0f; // Configure which pins are inputs and which are outputs configure_pins(); while(1) { LATC=0x0f; // All Digits off LATD = LUT[number[0]]; LATCbits.LATC3=0; // Most Significant Digit for (n=0 ; n<delay ; ++n); // delay LATC=0x0f; // All Digits off LATD = LUT[number[1]]; LATCbits.LATC2=0; // Next Significant Digit for (n=0 ; n<delay ; ++n); // delay LATC=0x0f; // All Digits off LATD = LUT[number[2]]; LATCbits.LATC1=0; // Next Significant Digit for (n=0 ; n<delay ; ++n); // delay LATC=0x0f; // All Digits off LATD = LUT[number[3]]; LATCbits.LATC0=0; // Next Significant Digit for (n=0 ; n<delay ; ++n); // delay /* LATD = 0x00; // Blank LATC=0b00000001; // MS Digit LATD = LUT[L]; // Least Significant Digit for (n=0 ; n<delay ; ++n); // delay LATD = 0x00; // Blank // LATC=0b00000010; // MS Digit*/ } } // Function to configure digital I/O pins etc. // This fucntion only configures port D so far - later on, // you will need to configure settings for other pins on // the chip. void configure_pins() { // Configure port C digital inputs and outputs // Inputs: 15, 16, 17, 18 (RC0, RC1, RC2 and RC3) // Outputs: 19, 20, 21 and 22 (RD0, RD1, RD2 and RD3) LATC = 0; // Set port C data latch, so that any pins that any outputs pins will be 0V initially. TRISC = 0b11110000; // This binary value sets pins C0-3 as outputs and RC4-7 as inputs. // Configure port D digital i/o // Outputs: 19, 20, 21 and 22 27, 28, 29 and 30(RD0, RD1, RD2, RD3,RD4, RD5, RD6 and RD7) LATD = 0; // Set port D data latch, so that any pins that any outputs pins will be 0V initially. TRISD = 0b00000000; // This binary value sets pins RD0-7 as outputs. }
Filed under: Displays