求一个关于用C语言编写的电子时钟的程序,内容要有闹钟,可以调整时间

如题所述

#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#define unit unsigned int
#define uchar unsigned char
//#define HZ 12
sbit key0=P0^0; // 分钟调整
sbit key1=P0^1; // 小时调整

sbit P2_0=P2^7; //秒 指示灯
sbit MN_RXD=P3^6;
sbit MN_TXD=P3^7;

uchar data CLOCK[4]={0,0,0,12};//存放时钟时间(百分秒,秒,分,和时位)

//数码管显示表0-f 灭
uchar code TABLE[]={0xBE,0x06,0xEA,0x6E,0x56,0x7C,0xFC,0x0E,0xFE,0x7E,0x00};
//**********************************
//模拟串口发送一个字节数据 函数
//**********************************
void SendData(unsigned char senddata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((senddata&0x01)==0)
MN_RXD=0;
else
MN_RXD=1;
_nop_();
MN_TXD=0;
_nop_();
MN_TXD=1;
senddata=senddata>>1;
}
}

//**********************************
//显示程序函数
//**********************************
void display(void)
{
// unsigned int n;
uchar temp;
temp=CLOCK[1]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[1]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp/10; SendData(TABLE[temp]);

/*
for(n=0;n<5000;n++);

for(n=0;n<6;n++)
{
SendData(TABLE[10]);
}
*/
}

//**********************************
//按键控制函数
//**********************************
void keycan()
{
unsigned int n;
EA=0;
if(key0==0) // 分钟调整
{
for(n=0;n<10000;n++); //延时去抖动
while(key0==0);
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一时
{
CLOCK[2]=0;
}
display();

}
if(key1==0) // 小时调整
{
for(n=0;n<10000;n++); //延时去抖动
while(key1==0);
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
display();

}
EA=1;
}

//**********************************
//T0中断服务函数
//**********************************
void time0() interrupt 1 //using 1
{
TH0=0xD8; TL0=0xF0; //重置初值
// TH0=0xB1; TL0=0xE0;
//时钟处理
CLOCK[0]=CLOCK[0]+1;

}
//**********************************
//主函数
//**********************************
void main()
{
EA=1;
ET0=1;
TMOD=0x01; //T0方式1定时
TH0=0xD8; TL0=0xF0; //D8F0 定时10ms
// TH0=0xB1; TL0=0xE0; //定时 20ms
TR0=1;
for(;;)
{
if(CLOCK[0]==100) //到一秒 10ms*100
{
CLOCK[0]=0;
P2_0=~P2_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;
}
}
}
display();
}

keycan();
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-23
下面有了!!!!
第2个回答  2011-05-21
可以找我
相似回答