C51实现数码管动态计数
kying-star的博客
# C51实现数码管动态计数
# 在8位数码管上显示一串数字
#include <reg52.h>
#include <intrins.h>
void delay2ms(void) //误差 0us
{
unsigned char a, b;
for (b = 4; b > 0; b--)
for (a = 248; a > 0; a--)
;
_nop_(); //if Keil,require use intrins.h
}
void main()
{
unsigned int i = 0, j = 0;
unsigned char cnt = 0;
unsigned char a = 0xfe;
unsigned char nums[8] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07};
while (1)
{
P2 = nums[i];
P3 = a;
delay2ms();
a = _crol_(a, 1);
i++;
if (i % 100)
{
P2 = nums[j];
j++;
}
if (i % 8 == 0)
{
i = 0;
a = 0xfe;
}
}
}
# 用两个数码管显示数字从00到59
#include <reg52.h>
#include <intrins.h>
void delay30ms(void) //误差 0us
{
unsigned char a, b;
for (b = 101; b > 0; b--)
for (a = 147; a > 0; a--)
;
}
void delay500ms(void) //误差 0us
{
unsigned char a, b, c;
for (c = 23; c > 0; c--)
for (b = 152; b > 0; b--)
for (a = 70; a > 0; a--)
;
}
void main()
{
unsigned int i = 0, j = 0, k = 0, t = 0;
unsigned char cnt = 0;
unsigned char a = 0xfe;
unsigned char nums[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
unsigned char sacn[2] = {0x01, 0x02};
while (1)
{
//扫描
for (i = 0; i < 6; i++)
{
for (j = 0; j < 10; j++)
{
P2 = nums[i];
delay30ms();
P2 = nums[j];
delay30ms();
for (k = 0; k < 6; k++)
{
delay30ms();
P3 = sacn[0];
P2 = nums[i];
delay30ms();
P3 = sacn[1];
P2 = nums[j];
}
}
}
}
}