52单片机0-9的显示
时间: 2023-12-10 21:39:12 浏览: 24
52单片机可以通过连接七段数码管来显示0-9的数字。七段数码管有7个LED灯,每个LED灯代表数码管的一段,这些灯可以组合成数字0-9的样式。
下面是一个简单的代码示例,可以实现在4位七段数码管上显示0-9的数字:
```c
#include <reg52.h>
// 数码管段选控制端口
sbit LED_A = P2^0;
sbit LED_B = P2^1;
sbit LED_C = P2^2;
sbit LED_D = P2^3;
sbit LED_E = P2^4;
sbit LED_F = P2^5;
sbit LED_G = P2^6;
sbit LED_DP = P2^7;
// 数码管位选控制端口
sbit DIGIT_1 = P1^0;
sbit DIGIT_2 = P1^1;
sbit DIGIT_3 = P1^2;
sbit DIGIT_4 = P1^3;
// 数码管显示字符数组
unsigned char code LEDChar[] = {
0x3F, // "0"
0x06, // "1"
0x5B, // "2"
0x4F, // "3"
0x66, // "4"
0x6D, // "5"
0x7D, // "6"
0x07, // "7"
0x7F, // "8"
0x6F // "9"
};
void delay(int count) {
int i, j;
for (i = 0; i < count; i++) {
for (j = 0; j < 125; j++) {
}
}
}
void Display(unsigned char *p) {
int i;
for (i = 0; i < 4; i++) {
P0 = LEDChar[p[i]];
switch (i) {
case 0: DIGIT_1 = 0; break;
case 1: DIGIT_2 = 0; break;
case 2: DIGIT_3 = 0; break;
case 3: DIGIT_4 = 0; break;
}
delay(1);
DIGIT_1 = 1;
DIGIT_2 = 1;
DIGIT_3 = 1;
DIGIT_4 = 1;
}
}
void main() {
unsigned char num[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
while (1) {
for (i = 0; i < 10; i++) {
Display(&num[i]);
delay(1000);
}
}
}
```
这个代码片段使用了 P2 口控制数码管的段选,P1 口控制数码管的位选,通过循环显示 num 数组中的数字。你可以将这个代码编译并烧录到 52单片机 中,然后连接七段数码管进行测试。