/**************************************************
实验名称:独立按键实验一
实验目的:数码管前三位显示一个跑表,从000到999之间
以1%秒速度运行,当按下一个独立键盘时跑表
停止,松开手后跑表继续。用定时器设计表。
实验备注:按下按键时跑表是停止了,但只显示最后一位
数字,因为执行按键程序的while了,display
就没有继续动态显示前两位数码管的数字,能
否改善这个问题咧?
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key_01 = P3^4;
sbit DUAN = P2^6;
sbit WEI = P2^7;
uchar flag;
uint num;
uchar bai, shi, ge;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
void delay_1ms(uchar);
void display(uchar,uchar,uchar);
void main()
{
num = 0;
flag = 0;
TMOD = 0x01;
TH0 = (65536-9216)/256;
TL0 = (65536-9216)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
while(key_01 == 0)//等待按键释放
{
delay_1ms(5);
if(key_01 == 0)
{
ET0 = 0;
}
}
ET0 = 1;
if(flag == 10)
{
flag = 0;
num++;
if(num >= 1000)
num = 0;
bai = num/100;
shi = num%100/10;
ge = num%10;
}
display(bai,shi,ge);
}
}
void delay_1ms(uchar x)
{
uchar i, j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
void display(uchar bai, uchar shi, uchar ge)
{
DUAN = 1;
P0 = duan_table[bai];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[shi];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[ge];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[2];
WEI = 0;
delay_1ms(1);
}
/*void display(uchar i,uchar j,uchar k)
{
DUAN = 1;
P0 = duan_table[i];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[j];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[k];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[2];
WEI = 0;
delay_1ms(1);
}*/
void Timer0() interrupt 1
{
TH0 = (65536-9216)/256;
TL0 = (65536-9216)%256;
flag++;
}
/**************************************************
实验名称:独立按键实验二
实验目的:数码管前三位显示一个跑表,从000到999之间
以1%秒速度运行,按下第一个键时计时停止,
按下第二个键时计时开始,按下第三个键时数
值清零从头开始。
实验备注:
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DUAN = P2^6;
sbit WEI = P2^7;
sbit key_01 = P3^4;
sbit key_02 = P3^5;
sbit key_03 = P3^6;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
uchar flag;
void delay_1ms(uchar);
void display(uchar, uchar, uchar);
void main()
{
uchar bai, shi, ge;
uint num;
bai = 0;
shi = 0;
ge = 0;
num = 0;
flag = 0;
TMOD = 0x01;
TH0 = (65536-9216)/256;
TL0 = (65536-9216)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(key_01 == 0)
{
delay_1ms(5);
if(key_01 == 0)
{
ET0 = 0;
while(!key_01);//等待按键释放
}
}
if(key_02 == 0)
{
delay_1ms(5);
if(key_02 == 0)
{
ET0 = 1;
while(!key_02);//等待按键释放
}
}
if(key_03 == 0)
{
delay_1ms(5);
if(key_03 == 0)
{
num = 0;
while(!key_03);//等待按键释放
}
}
if(flag == 1)
{
flag = 0;
num++;
bai = num/100;
shi = num%100/10;
ge = num%10;
if(num >= 1000)
num = 0;
}
display(bai,shi,ge);
}
}
void delay_1ms(uchar x)
{
uchar i,j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
void display(uchar bai,uchar shi, uchar ge)
{
DUAN = 1;
P0 = duan_table[bai];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[shi];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[ge];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[2];
WEI = 0;
delay_1ms(1);
}
void Timer0() interrupt 1
{
TH0 = (65536-9216)/256;
TL0 = (65536-9216)%256;
flag++;
}