|
帮忙看一下一个动态扫描数码管的程序(关于单片机延时的问题)
[复制链接]
在51单片机中,为什么要延时,什么时候要延时,延时多长时间?
我的程序是这样写的,我给出一个四位十进制数,我想用动态扫描数据管的方法显示出这个四位数(硬件电路中有四个数码管,单片机为at89c51)
program:
/*--------------------------------------------------
@pro:led.c
@author:aniu
@brief: 简单的LED测试
--------------------------------------------------*/
//编译预处理指令
#include
#define LEDDATA P0 //p0口接数码管的7段
#define LED0 P1_0 //p1.0--p1.3接数码管的选通
#define LED1 P1_1
#define LED2 P1_2
#define LED3 P1_3
//全局变量声明
unsigned int displayNum = 1441;
//定义缓冲区
unsigned char buffer[4];
//函数声明
void ledDecoder(int inputNum); //字形编码
void enaControl(int enaIn); //选通控制
void ledDisplay(int numInput); //显示
//主函数
void main(void)
{
while(1){ledDisplay(displayNum);}
}
void ledDecoder(int inputNum)
{
switch(inputNum)
{
case 0:
LEDDATA = 0x3f;
break;
case 1:
LEDDATA = 0x06;
break;
case 2:
LEDDATA = 0x5b;
break;
case 3:
LEDDATA = 0x4f;
break;
case 4:
LEDDATA = 0x66;
break;
case 5:
LEDDATA = 0x6d;
break;
case 6:
LEDDATA = 0x7d;
break;
case 7:
LEDDATA = 0x07;
break;
case 8:
LEDDATA = 0x7f;
break;
case 9:
LEDDATA = 0x6f;
break;
}
return;
}
void enaControl(int enaIn)
{
switch(enaIn)
{
case 0:
LED0 = 0;
LED1 = 1;
LED2 = 1;
LED3 = 1;
break;
case 1:
LED0 = 1;
LED1 = 0;
LED2 = 1;
LED3 = 1;
break;
case 2:
LED0 = 1;
LED1 = 1;
LED2 = 0;
LED3 = 1;
break;
case 3:
LED0 = 1;
LED1 = 1;
LED2 = 1;
LED3 = 0;
break;
}
}
void ledDisplay(int numInput)
{
int i;
int a;
buffer[0] = numInput/1000;
buffer[1]=(numInput/100) %10;
buffer[2]=(numInput/10)%10;
buffer[3]=numInput%10;
for(a=0;a<5000;a++);
for(i =0;i<4;i++)
{
//这里要延时吗?延时多少
enaControl(i);
ledDecoder(buffer);
for(a=0;a<50000;a++);
if(i==3) i=0;
}
}
|
|