【文件属性】:
文件名称:单片机时钟程序
文件大小:2KB
文件格式:C
更新时间:2017-08-15 15:42:59
时钟
#include
#define uchar unsigned char;
#define uint unsigned int;
sbit KEY=P3^4;
uchar SEG_date[11] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar SEG_wei[8] = {0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00};
uint time, count, miao, fen, shi;
uchar time_wei[8], i;
void init_EX(void);
void delay_ms(unsigned char count);
void BDC(void);
void main(void)
{
TMOD = 0x20;
TH1 = -200;
TL1= -200;
TR1= 1;
ET1 = 1;
EA = 1;
miao = 0;
fen = 0;
shi = 0;
time = 0;
init_EX();
while(1)
{
if (KEY == 0)
{
delay_ms(200);
if (KEY == 0)
{
shi--;
if (shi < 0)
{
shi = 24;
}
}
}
if (time >= 5000)
{
time = 0;
miao++;
}
else if (miao > 59)
{
fen++;
miao = 0;
}
else if (fen > 59)
{
shi++;
fen =0;
}
else if (shi > 24)
{
miao = 0;
fen = 0;
shi = 0;
}
else if (fen < 0)
{
fen = 59;
}
else if (shi < 0)
{
shi = 24;
}
BDC();
for (i = 0; i < 8; i++)
{
P0 = SEG_date[time_wei[i]];
P2 = SEG_wei[i];
delay_ms(5);
}
}
}
void ZTIME() interrupt 3
{
time++;
}
void BDC(void)
{
time_wei[0] = miao % 10;
time_wei[1] = miao / 10;
time_wei[2] = 10;
time_wei[3] = fen % 10;
time_wei[4] = fen / 10;
time_wei[5] = 10;
time_wei[6] = shi % 10;
time_wei[7] = shi / 10;
}
void delay_ms(unsigned char count)
{
unsigned char x;
while(count--)
{
for (x= 0; x < 40; x++);
}
}
void init_EX(void)
{
IT0 = 1;
EX0 =1;
IT1 = 1;
EX1 = 1;
EA = 1;
}
void outint0(void) interrupt 0
{
fen++;
}
void outint1(void) interrupt 2
{
fen--;
}