程序:通过按键来改变流水灯间隔的时间,要求间隔时间在0.1S到5S可调,并显示在数码管上。我写好后出现按按键能控制时间间隔显示在数码管,但是再按开流水灯键,但是隔一段时间再显示流水灯而不是按下流水灯键后就显示流水灯,在这之后就正常了!为什么会隔一段时间后才显示流水灯???谢谢 不知道是不是逻辑出问题了
#include<reg52.h>
#include"smguan.h"
uint16 counter = 0;
uint16 n=0 ,temp;
uint16 a[3];
code uint8 table_A[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff};
code uint8 ledcode_1[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
code uint8 ledcode_2[] = {0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10,0xff}; //带小数点
void delay(void)
{
uint16 i=2000;
while (i--);
}
/*
*数码管的定义变量显示子函数
*/
void refresh_led()
{
static uint8 j = 0;
P0=0xff;
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_1[a[0]];break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_2[a[1]];break;
case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;j=0;P0 = table_A[a[2]];break;
default:break;
}
}
/*键盘扫描
*/
int key_board()
{
if(!KeyIn1)
{
delay();
if(!KeyIn1)
{
if(n >= 50)
n = 0;
else
n++;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn1);
}
}
if(!KeyIn2)
{
delay();
if(!KeyIn2)
{
if(n<= 0)
n = 50;
else
n--;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn2);
}
}
if(!KeyIn3)
{
delay();
if(!KeyIn3)
{
if (temp == 0) //如果该键按下不需重新计时,保留该判断
{
ET1 = 0;
counter = 0; //因为你调整速度时该值已经不是零,所以会有等待.
ET1 = 1;
}
temp=1;
while(!KeyIn3);
}
}
return n;
}
/*
*定时器中断初始化
*/
void timer1_init()
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFc; //1ms
TL1 = 0x67;
TR1 = 1;
EA = 1;
ET1 = 1;
}
/*
*主函数
*/
void main()
{
KeyOut1 =0;
ENLED1 = 0;ADDR3 = 1;
timer1_init();
while(1);
}
/*
*中断服务函数
*/
void interrupt_timer1() interrupt 3
{
static uint32 sec=0;
int m;
TH1 = 0xFC;
TL1 = 0x67;
counter++;
m=key_board();
if (counter >= m*100) //防止运行后更改运行速度
{
sec++;
counter = 0;
if(temp==1)
{
sec %= 10; //防止sec在加到255时,你的灯会显示出错一次
a[2] = sec;
}
}
refresh_led();
}
一周热门 更多>