1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| #include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
}; // 共阳数码管码表: 0 - F
void delay(uint n) // 延时函数
{
uint x, y;
for(x = 0 ; x < n; x++)
for(y = 0 ; y < 122 ; y++); //空跑122次大概为1ms
}
uint * split_num(uint n) // 将四位数拆分为独立的四个数字
{
uint nums[4];
nums[0] = n / 1000; // 千位
nums[1] = n % 1000 / 100; // 百位
nums[2] = n % 100 / 10; // 十位
nums[3] = n % 10; // 个位
return nums;
}
void main()
{
uint i, j, x, L1, *nums;
while(1)
{
for(i=0 ; i < 10000 ; i++) // i为在数码管上显示的4位数
{
nums = split_num(i); // 拆分数字
for(x=0 ; x<100 ; x++) // 该层循环控制数字增长速率
{
L1 = 0xF7; // 初始位选
for(j=0 ; j<4 ; j++) // 该层让四个数码管循环显示数字,利用余晖效应制造出同时显示的效果
{
P1 = table[*(nums + j)]; // 段选
P0 = L1; // 位选
delay(1); // 延时
L1 >>= 1; // 右移一位数码管
P0 = 0x00; // 消影
}
}
}
}
}
|