A seven-segment display is a form of electronic display device for displaying decimal numerals.
Seven-segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.
Common Anode:
Common cathode:
Common pins in both 7-segment:
In common anode, VCC is connected to COM terminal.
In common cathode, GND is connected to COM terminal.
In common cathode, if VCC is connected to every pin, a to g, the corresponding segment will be ON.
In common anode, if GND is connected to every pin, a to g, the corresponding segment will be ON.
The following table shows the truth table, the equal hexadecimal and binary format for common cathode 7-segment display.
Now we’re going to learn to make two counters using a 7-segment.
We have to declare the 7-segment numbers in flash memory of ATMEGA16 as the following matrix. When a data will be stored in flash memory, it will be defined as retentive.
The counting process starts via a button.
In AVR Codevision
The data type of every element in display matrix is char that includes 8 bits.
flash char display0[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash char display1[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
Now it is time to set initial value and data direction of PORTD and PORTB.
PINB.0 of PORTB is utilized as input with initial value of 1.
PORTB=0x01; //declaring initial logical value of 1 for PINB.0
DDRB=0x00; //setting PINB.0 as input
PORTB and PORTD are utilized as output with initial value of 0.
PORTB=0x00; //declaring initial logical value of 1 for PORTB
DDRB=0xFF; //setting PORTB as output
PORTD=0x00; //declaring initial logical value of 1 for PORTD
DDRD=0xFF; //setting PORTD as output
Now it is time to write code.
// First of all, declaring two local values as a counter like i and j with initial value of 0.
int i = 0; // counter for common anode 7-segment
int j = 0; // counter for common cathode 7-segment
// Inside while loop:
while(1)
{
if (PINB.0 == 0) //when button is pressed PINB.0 is equal to 0
{
PORTC = display0 [i];
PORTD = display1 [j];
i = i + 1;
j = j + 1;
delay_ms(500);
}
}
Finally reset i and j
Both matrixes have 10 elements. When reaching the last element of the matrixes, the counters must be reset.
As the counter values are equal to 10 by using conditional command if, it is necessary to set initial value 0 for i and j.
if(i == 10, j == 10)
{
i = 0;
j = 0;
}
The final code is:
flash char display0[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash char display1[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int i = 0; // declaring counter variable i
int j = 0; // declaring counter variable j
PORTB = 0x01; // setting initial value for PORTB
DDRB = 0x00; // setting data direction for PORTB
PORTD = 0x00; // setting initial value for PORTB
DDRD = 0xFF; // setting data direction for PORTB
While (1)
{
If (PINB.0 == 0)
{
PORTC = Display0 [ i ];
PORTD = Display1 [ j ];
i = i + 1;
j = j + 1;
}
if( i == 10, j == 10) // resetting counter values of i and j
{
i == 0;
j == 0;
}
}
In proteus 8
In project tab select “build all”.
Double click on ATMEGA16.
Download EXE file to ATMEGA16.
In proteus click on play.
When button is pressed, the value of PINA.0 is equal to “0”.
You did it!!!
Good luck
▶️ 3Speak