📄 main.c
字号:
#include<reg51.h>
#include<stdio.h>
#include<absacc.h>
#include"key.h"
#include"lcd1602.h"
#define uint unsigned int
#define uchar unsigned char
#define DIGPORT P2
#define WORDPORT P0
bit int0_flag=0; ///中断申请标记位
/*数码管显示编码0-----9*/
uchar data CLOCK[4]={0,0,0,0};
//主函数
void main(void){
uchar temp[16];
uchar key;
EA=1;
TR0=1;ET0=1;TR0=1;
TMOD=0X01;
TH0=0Xdc;TL0=0x88; //T0定时
IT0=1;TR1=0;EX0=1;IE0=1;
LCD_init();
while(1)
{
if(int0_flag)
{
if(CLOCK[2]>35&&CLOCK[2]<60)
{CLOCK[3]=CLOCK[3]+1;
CLOCK[1]=0;
CLOCK[2]=0;
sprintf(temp,"TIME:%2bu:%2bu:%2bu",CLOCK[3],CLOCK[2],CLOCK[1]);
LCD_setxy(1,1);
LCD_string(temp);
}
if(CLOCK[2]>0&&CLOCK[2]<25)
{CLOCK[3]=CLOCK[3];
CLOCK[2]=0;
CLOCK[1]=0;
sprintf(temp,"TIME:%2bu:%2bu:%2bu",CLOCK[3],CLOCK[2],CLOCK[1]);
LCD_setxy(1,1);
LCD_string(temp);
}
int0_flag=0;
}
//小时
key=keyscan();
sprintf(temp,"TIME:%2bu:%2bu:%2bu",CLOCK[3],CLOCK[2],CLOCK[1]);
LCD_setxy(1,1);
LCD_string(temp);
switch(key)
{
case 1:
{TR0=0;
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60)CLOCK[2]=0;
delay_10ms();
}TR0=1;
break;
case 2:
{TR0=0;
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)CLOCK[3]=0;
delay_10ms();
}TR0=1;
break;
case 3:
{TR0=0;
CLOCK[1]=CLOCK[1]+1;
if(CLOCK[1]==60)CLOCK[1]=0;
delay_10ms();
}TR0=1;
break;
case 4:
CLOCK[2]=0;
CLOCK[1]=0;
CLOCK[3]=0;
break;
}
}
}
//子函数24小时时钟
void timer0(void) interrupt 1 using 1
{
TH0=0xdc;
TL0=0x88;
//时钟处理
CLOCK[0]=CLOCK[0]+1;
if (CLOCK[0]==100)
{
CLOCK[0]=0;
CLOCK[1]=CLOCK[1]+1;
if(CLOCK[1]==60)
{
CLOCK[1]=0;
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60)
{
CLOCK[2]=0;
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
}
}
}
}
/************外部中断函数*****************/
void intflag() interrupt 0
{
int0_flag=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -