
In this article, we will tell you how to operate a 7-segment display (7SD in short) with a microcontroller. There are two (perhaps more) options to do this. The first is to connect the 7SD directly to the output ports of the microcontroller as shown in the picture above. This may cause a port shortage in large projects. As a second way to avoid this; We can run it by connecting a decoder to the 7SD. We will show both for you.
We will use a function to display the desired number in 7SD. This function is as follows;
Display Num Function Code
void displayNum(char num) { TRISC = 0x00; switch (num) { case 0: PORTC = 0b00111111; break; case 1: PORTC = 0b00000110; break; case 2: PORTC = 0b01011011; break; case 3: PORTC = 0b01001111; break; case 4: PORTC = 0b01100110; break; case 5: PORTC = 0b01101101; break; case 6: PORTC = 0b01111101; break; case 7: PORTC = 0b00000111; break; case 8: PORTC = 0b01111111; break; case 9: PORTC = 0b01100111; break; default: PORTC = 0b00111111; }
The numbers we assign to PORTC are the equivalent of that number in 7SD. Here we used the first 7 bits of PORTC. Now let’s come to the main program;
Main Program Code
int main() { // Make pins as digital. ANSEL = 0x00; // Make PORTC as output. TRISC = 0x00; PORTC = 0x00; // Make RA0 as input. TRISA = 0b00001001; while (1) { // Displays numbers with 300ms delay. for (char i = 0; i < 10; i++) { displayNum(i); __delay_ms(300); // Wait for switch. while (RA0); } } return (0); }
First of all, to make pins digital, we need to reset the ANSEL Register. Then we reset TRISC and PORTC. With TRISA, we set pin A0 as input. We show the numbers in 7SD respectively with 300 ms intervals in the infinite loop. If the A0 pin is 1 (5V), we are waiting.
The second way is to run the 7SD using the 4511 IC (decoder). The circuit for this will be as follows.

With the decoder, we can run 7SD by simply entering the number. For this, we can change the function as follows;
Display Num Function Code For 4511
void displayNum(char num) { TRISC = 0x00; PORTC = num; }
Thus, we reduced the number of pins used and did not use the program memory much.
Finally, below is a simulation made with Proteus.

We hope it has been helpful. Stay healthy …

Comments
Post a Comment