三个按键分别为开始、暂停、加\减。当系统复位后,初试时间为00.00.00,按下开始按键,显示为23.59.59,默认以减计时器,每秒减一,计时期间任何时候按下暂停按钮,计时停止,并保持,再次按下开始按钮,继续计时。用PROTUS仿真,图为
按照你的要求写的程序如下,你拿去测试吧!
#include "REG52.H"
sbit SHUG_01 = P2^0; //数码管1
sbit SHUG_02 = P2^1; //数码管2
sbit SHUG_03 = P2^2; //数码管3
sbit SHUG_04 = P2^3; //数码管4
sbit SHUG_05 = P2^4; //数码管5
sbit SHUG_06 = P2^5; //数码管6
sbit KEY0_01 = P0^0; //按键 启动或停止
sbit KEY0_02 = P0^1; //按键 加
sbit KEY0_03 = P0^2; //按键减
#define LED_DATA P1 //P1数据端口
unsigned char value_temp = 0;
unsigned char value_shio = 23;
unsigned char value_feno = 59;
unsigned char value_miao = 59;
unsigned char code value_tab0[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; //共阳极
unsigned char code value_tab1[]={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10}; //共阳极
/*
* 延时1ms延时子函数
**/
void Delay_1ms(unsigned int Time)
{
unsigned int x, y;
for(x = Time; x > 0; x--)
for(y = 120; y > 0; y--);
}
/*
* 按键开关【启动和停止】子函数
**/
void Scan_keyoutCmd(void)
{
if(KEY0_01 == 0)
{
Delay_1ms(10);
if(KEY0_01 == 0)
{
TR0 = ~ TR0;
}
while(KEY0_01 == 0);
}
if(KEY0_02 == 0)
{
Delay_1ms(10);
if(KEY0_02 == 0)
{
value_miao--;
if(value_miao == -1)
{
value_feno--;
value_miao = 59;
if(value_feno == 0)
{
value_shio--;
value_feno = 59;
if(value_shio == 0)
{
value_shio = 23;
}
}
}
while(KEY0_02 == 0);
}
}
if(KEY0_03 == 0)
{
Delay_1ms(10);
if(KEY0_03 == 0)
{
value_miao++;
if(value_miao == 60)
{
value_miao = 0;
value_feno++;
if(value_feno == 60)
{
value_feno = 0;
value_shio++;
if(value_shio == 24)
{
value_shio = 0;
}
}
}
while(KEY0_03 == 0); //检测松手
}
}
}
/*
* 程序运行【启动和停止】子函数
**/
void Disp_layledketCmd(void)
{
/***小时***/
SHUG_01 = 1;
LED_DATA = value_tab0[value_shio / 10]; /*******小时的十位数显示***************/
Delay_1ms(5);
SHUG_01 = 0;
SHUG_02 = 1;
LED_DATA = value_tab1[value_shio % 10]; /*******小时的个位数显示***************/
Delay_1ms(5);
SHUG_02 = 0;
/***分钟***/
SHUG_03 = 1;
LED_DATA = value_tab0[value_feno / 10]; /*******分钟的十位数显示***************/
Delay_1ms(5);
SHUG_03 = 0;
SHUG_04 = 1;
LED_DATA = value_tab1[value_feno % 10]; /*******分钟的个位数显示***************/
Delay_1ms(5);
SHUG_04 = 0;
/***秒钟***/
SHUG_05 = 1;
LED_DATA = value_tab0[value_miao / 10]; /*******秒钟的十位数显示***************/
Delay_1ms(5);
SHUG_05 = 0;
SHUG_06 = 1;
LED_DATA = value_tab0[value_miao % 10]; /*******秒钟的个位数显示***************/
Delay_1ms(5);
SHUG_06 = 0;
}
void Tero_Init(void)
{
TMOD = 0x01;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/*
* 函数主体
**/
int main(void)
{
P1 = P2 = P3 = 0x00;
Tero_Init();
while(1)
{
Scan_keyoutCmd();
Disp_layledketCmd(); //执行应运程序
}
}
/*
* 定时器中断服务程序
**/
void Tmero()interrupt 1
{
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
value_temp++;
if(value_temp == 20)
{
value_temp = 0;
value_miao--;
if(value_miao == 0)
{
value_feno--;
value_miao = 59;
if(value_feno == 0)
{
value_shio--;
value_feno = 59;
if(value_shio == 0)
{
value_shio = 23;
}
}
}
}
}