[DIY] The Design Of Multiplexed Seven Segment Counter
Two digits multiplexed common cathode seven segment displays are connected to PORT C of the the MCU. Segments “a” through “g” are connected to PC0 through PC6respectively. The cathode of digits 1(tens) and 2(units) are connected to PD0 and PD1 respectively. The MCU extracts the ten and the unit digits, sends them to the seven segment display and activates the appropriate digit by pulling its cathode low. This happens very fast that it appears to be continuous to the eyes.Two buttons are connected to PB0 and PB1 through 10k pull down resistors.
The button at PB0 is used to increase the count while PB1 decreases the count. The circuit is designed to respond to a single button press at a time (a button must be released before it or another button is pressed).
The circuit diagram(captured from Proteus) is shown below. Vcc and ground connections are not shown but must be included to make the MCU work.
The C source code (well commented) is shown below.
/**************************************************************************
Title: Seven segment display
Version: 1.0
Target MCU: ATMEGA 16
Crystal: 1MHz
IDE/Compiler: AVR Studio 4/WinAVR
www.microscale-embedded.com
Date: August, 2011
**************************************************************************/
#include <avr/io.h>
#include <util/delay.h>
#define seven_segment PORTC
#define increase PINB&(1<<PB0)
#define decrease PINB&(1<<PB1)
#define unit_select 0xED
#define ten_select 0xFE
uint8_t Seven_seg_code[10] = {0x3f,0×06,0x5b,0x4f,0×66,0x6d,0x7d,0×07,0x7f,0x6f};
//uint8_t decode(int); //decode funtion declared
int main()
{
int count, status, status1; //variable to hold count
DDRC=0xff; //PORT C configured as output
DDRB=0×00; //PORT B configured as input
DDRD=0xff; //PORT D configured as output
PORTD = 0xff;
PORTC = 0×00;
//endless loop
while(1)
{
if(status&&status1)
{
if(increase) //increase count if increase button is pressed
count++;
if(decrease) //decrease count if increase button is pressed
count–;
}
if(count>99) //rolls count back to zero(0) if it becomes
count=0; //greater than 99
if(count<0) //change count to 99 if it becomes negative
count=99;
int tens = count/10;
int units = count%10;
PORTD = unit_select;
seven_segment= Seven_seg_code[units];
_delay_ms(5);
PORTD = ten_select;
seven_segment= Seven_seg_code[tens];
//Checks key release after being pressed
if(increase)
status = 0;
else
status = 1;
if(decrease)
status1 = 0;
else
status1 = 1;
_delay_ms(100); // a brief delay
}
return 0;
}
Editor’s Note: Microscale Embedded contributed this piece.
Page 1 of 2 | Next page