In this programming example, I will show a very simple display multiplexing using a two-digit seven-segment display. It will show a value that read from the lower nibble of PortB.
Program Simulation |
The display is common cathode type. I use a resistor network to cut down its forward current. The 74HC04 chip replace driving transistors, due to simulation difficulty. The lower nibble of PortB reads the digital inputs from a DIP switch. We will not need additional resistors because all input pins are turned high via software.
/* * muxDisplayDigitalRead.c * * Created: 5/30/2022 6:19:32 PM * Author : dell */ #include <avr/io.h> #define F_CPU 4000000UL #include <util/delay.h> int main(void) { unsigned char temp; /*Common Cathode Display*/ unsigned char displayData[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; /*PortB Input*/ DDRB=0x00; /*Turn on PD0...3 of PortB*/ PORTB=0x0F; /*PortB and PortD Output*/ DDRC=0xFF; DDRD=0xFF; while (1) { /*Read PortB*/ temp=PINB&0x0F; /*Multiplexing Display Process*/ PORTD=0x00; PORTC=displayData[temp/10]; /*Turn On Digit 1*/ PORTD=0x01; /*Activate for 5ms*/ _delay_ms(5); PORTD=0x00; PORTC=displayData[temp%10]; /*Turn On Digit 2*/ PORTD=0x02; /*Activate for 5ms*/ _delay_ms(5); } }
Click here to download its source file.
Schematic Diagram |
We can multiplex multi-digit 7-Segment display using the SN74HC168 serial to parallel shift registers chip.