Implementing Custom 3-Digit Display with C51 Microcontroller
C51 Microcontroller 3-Digit Display Implementation
Code Example:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DIGIT_SELECT = P2^7; // Digit selection latch
sbit SEGMENT_SELECT = P2^6; // Segment selection latch
const uchar digit_patterns[] = {
0x3F, // "0"
0x06, // "1"
0x5B, // "2"
0x4F, // "3"
0x66, // "4"
0x6D, // "5"
0x7D, // "6"
0x07, // "7"
0x7F, // "8"
0x6F // "9"
};
void delay_ms(uint ms) {
uint i, j;
for(i = ms; i > 0; i--)
for(j = 114; j > 0; j--);
}
void show_number(uint num) {
uchar hundreds = num / 100;
uchar tens = (num % 100) / 10;
uchar units = num % 10;
// Display hundreds digit
P0 = 0xFF;
DIGIT_SELECT = 1;
P0 = 0xFE;
DIGIT_SELECT = 0;
SEGMENT_SELECT = 1;
P0 = digit_patterns[hundreds];
SEGMENT_SELECT = 0;
delay_ms(5);
// Display tens digit
P0 = 0xFF;
DIGIT_SELECT = 1;
P0 = 0xFD;
DIGIT_SELECT = 0;
SEGMENT_SELECT = 1;
P0 = digit_patterns[tens];
SEGMENT_SELECT = 0;
delay_ms(5);
// Display units digit
P0 = 0xFF;
DIGIT_SELECT = 1;
P0 = 0xFB;
DIGIT_SELECT = 0;
SEGMENT_SELECT = 1;
P0 = digit_patterns[units];
SEGMENT_SELECT = 0;
delay_ms(5);
}
void main() {
while(1) {
show_number(123); // Change this value to display different numbers
}
}
Implementation Notes:
- The pin definitionss (
DIGIT_SELECTandSEGMENT_SELECT) should be adjusted accroding to your specific hardware configuration. - The
show_number()function parameter determines the displayed value (must be a 3-digit number). - This implementation uses a common cathode 7-segment display with the specified digit paterns.