C语言的问题,求纠错,动态扫描仿真的时候一直在闪
时间:10-02
整理:3721RD
点击:
/*******************附代码************************/
#include<reg52.h>
#define ON 1
#define OFF 0
#define scan_delay 20
#define button_delay 50
unsigned char code LED[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char n=0;//表示状态的变化
unsigned char sec,min,hour;//秒,分,时
unsigned char min_0,min_1,hour_0,hour_1;//分,时 个位,十位
unsigned char key;
sbit D1=P0^3;sbit D2=P0^2;sbit D3=P0^1;sbit D4=P0^0; //段选
sbit set=P1^0;sbit plus=P1^1;sbit minus=P1^2;//按键
void delay(unsigned int a)//延迟函数
{
for(;a!=0;a--);
}
void scan_min_0()// 分 个位
{
P2=0;delay(scan_delay);P2=LED[min%10];delay(scan_delay);D4=ON;
delay(scan_delay);D4=OFF;delay(scan_delay);
}
void scan_min_1()// 分 十位
{
P2=0;delay(scan_delay);P2=LED[min/10];delay(scan_delay);D3=ON;
delay(scan_delay);D3=OFF; delay(scan_delay);
}
void scan_hour_0()// 时 个位
{
P2=0;delay(scan_delay);P2=LED[hour%10];delay(scan_delay);D2=ON;
delay(scan_delay);D2=OFF;delay(scan_delay);
}
void scan_hour_1()// 时 十位
{
P2=0;delay(scan_delay);P2=LED[hour/10];delay(scan_delay);D1=ON;
delay(scan_delay);D1=OFF; delay(scan_delay);
}
void scan_LED()// 总扫描函数
{
if(n==0)
{
scan_min_0();
scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==1)
{
if(n>=10)scan_min_0();
scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==2)
{
scan_min_0();
if(n>=10)scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==3)
{
scan_min_0();
scan_min_1();
if(n>=10)scan_hour_0();
scan_hour_1();
}
else if(n==4)
{
scan_min_0();
scan_min_1();
scan_hour_0();
if(n>=10)scan_hour_1();
}
}
void button()
{
// set按键设置
if(set==1)
{
delay(button_delay);
if(set==1)
{
while(set==1)
scan_LED();
n++;
n=n%5;
}
}
// 加一键的设置
if(plus==1)
{
delay(button_delay);
if(plus==1)
{
while(plus==1)
scan_LED();
key=1;
}
}
// 减一键的设置
if(minus==1)
{
delay(button_delay);
if(minus==1)
{
while(plus==1)
scan_LED();
key=2;
}
}
}
void status_0() // 状态0
{
scan_LED();
}
void status_1() // 状态1
{
scan_LED();
if(n==1)
{
min_0++;
min_0=min_0%10;
}
else if(n==2)
{
min_0--;
if(min_0>9)min=9;
}
}
void status_2() // 状态2
{
scan_LED();
if(n==1)
{
min_1++;
min_1=min_1%6;
}
else if(n==2)
{
min_0--;
if(min_0>5)min=5;
}
}
void status_3() // 状态3
{
scan_LED();
if(n==1)
{
hour_0++;
hour_0=hour_0%10;
}
else if(n==2)
{
hour_0--;
if(hour_0>9)hour=9;
}
}
void status_4() // 状态4
{
scan_LED();
if(n==1)
{
hour_1++;
hour_1=hour_1%3;
}
else if(n==2)
{
hour_1--;
if(hour_1>2)hour=2;
}
}
void status()
{
min_0=min%10; //将 分时 的个位十位提取出来
min_1=min/10;
hour_0=hour%10;
hour_1=hour/10;
if(n==0) status_0();
else if(n==1) status_1();
else if(n==2) status_2();
else if(n==3) status_3();
else if(n==4) status_4();
min=min_0+min_1*10;
hour=hour_0+hour_1*10;
if(hour>=24)hour=23;
}
void main()
{
EA=1;
ET0=1;
TMOD=1;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
while(1)
{
button();
status();
}
}
void Time() interrupt 1 //计时器中断
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
n++;
if(n==20)
{
n=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
hour=hour%24;
}
}
}
}
/***************************************仿真*****************************/
#include<reg52.h>
#define ON 1
#define OFF 0
#define scan_delay 20
#define button_delay 50
unsigned char code LED[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char n=0;//表示状态的变化
unsigned char sec,min,hour;//秒,分,时
unsigned char min_0,min_1,hour_0,hour_1;//分,时 个位,十位
unsigned char key;
sbit D1=P0^3;sbit D2=P0^2;sbit D3=P0^1;sbit D4=P0^0; //段选
sbit set=P1^0;sbit plus=P1^1;sbit minus=P1^2;//按键
void delay(unsigned int a)//延迟函数
{
for(;a!=0;a--);
}
void scan_min_0()// 分 个位
{
P2=0;delay(scan_delay);P2=LED[min%10];delay(scan_delay);D4=ON;
delay(scan_delay);D4=OFF;delay(scan_delay);
}
void scan_min_1()// 分 十位
{
P2=0;delay(scan_delay);P2=LED[min/10];delay(scan_delay);D3=ON;
delay(scan_delay);D3=OFF; delay(scan_delay);
}
void scan_hour_0()// 时 个位
{
P2=0;delay(scan_delay);P2=LED[hour%10];delay(scan_delay);D2=ON;
delay(scan_delay);D2=OFF;delay(scan_delay);
}
void scan_hour_1()// 时 十位
{
P2=0;delay(scan_delay);P2=LED[hour/10];delay(scan_delay);D1=ON;
delay(scan_delay);D1=OFF; delay(scan_delay);
}
void scan_LED()// 总扫描函数
{
if(n==0)
{
scan_min_0();
scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==1)
{
if(n>=10)scan_min_0();
scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==2)
{
scan_min_0();
if(n>=10)scan_min_1();
scan_hour_0();
scan_hour_1();
}
else if(n==3)
{
scan_min_0();
scan_min_1();
if(n>=10)scan_hour_0();
scan_hour_1();
}
else if(n==4)
{
scan_min_0();
scan_min_1();
scan_hour_0();
if(n>=10)scan_hour_1();
}
}
void button()
{
// set按键设置
if(set==1)
{
delay(button_delay);
if(set==1)
{
while(set==1)
scan_LED();
n++;
n=n%5;
}
}
// 加一键的设置
if(plus==1)
{
delay(button_delay);
if(plus==1)
{
while(plus==1)
scan_LED();
key=1;
}
}
// 减一键的设置
if(minus==1)
{
delay(button_delay);
if(minus==1)
{
while(plus==1)
scan_LED();
key=2;
}
}
}
void status_0() // 状态0
{
scan_LED();
}
void status_1() // 状态1
{
scan_LED();
if(n==1)
{
min_0++;
min_0=min_0%10;
}
else if(n==2)
{
min_0--;
if(min_0>9)min=9;
}
}
void status_2() // 状态2
{
scan_LED();
if(n==1)
{
min_1++;
min_1=min_1%6;
}
else if(n==2)
{
min_0--;
if(min_0>5)min=5;
}
}
void status_3() // 状态3
{
scan_LED();
if(n==1)
{
hour_0++;
hour_0=hour_0%10;
}
else if(n==2)
{
hour_0--;
if(hour_0>9)hour=9;
}
}
void status_4() // 状态4
{
scan_LED();
if(n==1)
{
hour_1++;
hour_1=hour_1%3;
}
else if(n==2)
{
hour_1--;
if(hour_1>2)hour=2;
}
}
void status()
{
min_0=min%10; //将 分时 的个位十位提取出来
min_1=min/10;
hour_0=hour%10;
hour_1=hour/10;
if(n==0) status_0();
else if(n==1) status_1();
else if(n==2) status_2();
else if(n==3) status_3();
else if(n==4) status_4();
min=min_0+min_1*10;
hour=hour_0+hour_1*10;
if(hour>=24)hour=23;
}
void main()
{
EA=1;
ET0=1;
TMOD=1;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TR0=1;
while(1)
{
button();
status();
}
}
void Time() interrupt 1 //计时器中断
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
n++;
if(n==20)
{
n=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
hour=hour%24;
}
}
}
}
/***************************************仿真*****************************/