Programming AT89C51 for 7-Segment Display
Connect each of the seven segments (A-G) and the decimal point (DP) of the display to a separate I/O pin on the microcontroller. The common cathode pin should be connected to the ground (0V).
For example, you could connect segment A to P0.0, segment B to P0.1, segment C to P0.2, and so on, up to segment G on P0.6. If you're using the decimal point, you could connect it to P0.7.
Initialize the I/O pins connected to the display as output pins.
To display a digit or character, send the appropriate signals to the segment pins. For a common cathode display, a 'HIGH' signal turns a segment on, and a 'LOW' signal turns it off.
For a common anode display, it's the opposite: a 'LOW' signal turns a segment on, and a 'HIGH' signal turns it off.
Here's an example with bit size -
#include
// Define the segment pins
sbit segA = P2^0;
sbit segB = P2^1;
sbit segC = P2^2;
sbit segD = P2^3;
sbit segE = P2^4;
sbit segF = P2^5;
sbit segG = P2^6;
void delay(unsigned int time) {
// Simple delay function
unsigned int i, j;
for(i=0; i
Here's an example with byte size-
#include
//define segment port
#define segPort P0
void delay(unsigned int time) {
// Simple delay function
unsigned int i, j;
for(i=0; i
In a common cathode 7-segment display, a segment is turned on by applying a 'HIGH' signal and turned off by applying a 'LOW' signal.
To display the digit '1', you would turn on segments B and C, and turn off all the other segments. This gives us the following pattern:
Segment A: off
Segment B: on
Segment C: on
Segment D: off
Segment E: off
Segment F: off
Segment G: off
If we represent 'on' as '1' and 'off' as '0', we get the binary number 0000110. If we convert this binary number to hexadecimal, we get 0x06.Get all hexadecimal codes from here -
Digits And Characters Code Table for common cathode
Here's an example with byte array
#include
//define segment port
#define segPort P0
// digit mapping for common cathode 7-segment display
unsigned char seg_code[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
void delay(unsigned int time) {
// Simple delay function
unsigned int i, j;
for(i=0; i
In this example, we have an array named seg_code[] which is storing the 7-segment codes for displaying the digits from 0-9 on a common cathode 7-segment display.
In the main function, we are running a loop from 0 to 9 and selecting each digit's 7-segment code from the array using the digit as the index.
After setting the port to the 7-segment code, we wait for a delay before moving on to the next digit. This process continues indefinitely.