单片机时钟电子钟程序

是基于at89s51 5461bg uln2803等器件实现四位数字电子钟
如需要电路图可以发邮件到我邮箱 [email protected]
认为少分的可以用别的号再加40分!! 不要用c语言编的啊 !

#include <reg51.h>
sbit sec=P1^7;//秒显示
sbit P0_4=P0^4;
sbit P0_5=P0^5;
sbit P0_6=P0^6;
sbit P0_7=P0^7;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit P2_6=P2^6;
static int i=1;//LED位控制
static int key;//键盘
static char keytime;
static int second_T0;//T0秒计数
static int second_T1;//T1秒计数
unsigned char count_s;//秒
unsigned char count_m;//分
unsigned char count_h;//时
static bit flag_1=1;//LED分钟个位显示标志位
static bit flag_2=1;//LED分钟十位显示标志位
static bit flag_3=1;//LED小时个位显示标志位
static bit flag_4=1;//LED小时十位显示标志位
int m1,m2,h1,h2;//LED取值
unsigned char led[10]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
main()
{
void delay_10ms();//延时10ms
bit flag_m,flag_h;//分、时标志位
unsigned char x_temp,y_temp,readkey;//键盘变量
TMOD=0x11;
TH0=0xfc;
TL0=0x18;//1ms
TH1=0x3c;
TL1=0xb0;//50ms
EA=1;//总中断
IP=1;//优先级
ET0=1;//T0中断
ET1=1;//T1中断
PT0=1;//T0优先
TR0=1;//打开T0
TR1=1;//打开T1
P0=0;//LED输出
P2=0xf7;//置P2为11110111
keytime=0xe;
second_T0=1000;//计数1000
second_T1=10;//计数18
while(1)
{
if(count_s==0x3c)//秒计数60
{
count_m++;//分钟
flag_m=1;//分钟进位标志
count_s=0;//秒清零
}
if(count_m==0x3c&&flag_m==1)//分60
{
count_h++;//小时
flag_h=1;//小时进位标志
count_m=0;//分钟清零
}
if(count_h==0x18&&flag_h==1)
{
count_h=0;//小时清零
}
m1=count_m/10;//分钟十位
m2=count_m-m1*10;//分钟个位
h1=count_h/10;//小时十位
h2=count_h-h1*10;//小时个位
if(P2_3==0)//如果P2_3为零
{
x_temp=P2&0xff;//将第一次按键保存在x_temp
delay_10ms();//延时
P2=0xf7;//置P2为11110111
y_temp=P2&0xff;//将第一次按键保存在y_temp
if(x_temp==y_temp)//x_temp是否等于y_temp
{
readkey=x_temp;//将值保存在readkey
}
else
{
readkey=0;//如果第一次和第二次按键不一样readkey为0
}
switch(readkey)//查表
{
case 0xf6:key=1;break;
case 0xf5:key=2;break;
case 0xf3:key=3;break;
default:break;
}
}
}
}
void Time_T0() interrupt 1//T0中断5ms
{
TH0=0xfc;
TL0=0x18;//重载1ms
while(second_T0==0)//循环1000次为1s
{
second_T0=1000;
sec=~sec;//秒指示灯取反
count_s++;//每秒加1
}
second_T0--;
if(i==1&&flag_1==1)//LED分钟个位显示
{
P0=(led[m2]|0x80);
}
else if(i==2&&flag_2==1)//LED分钟十位显示
{
P0=(led[m1]|0x40);
}
else if(i==3&&flag_3==1)//LED小时个位显示
{
P0=(led[h2]|0x20);
}
else if(i==4&&flag_4==1)//LED小时十位显示
{
P0=(led[h1]|0x10);
}
else if(i==5)//显示位清零
{
i=0;
}
i++; //显示位累加
}
void Time_T1() interrupt 3//T1中断
{
TH1=0x3c;
TL1=0xb0;//重载50ms
while(second_T1==0)//定时600ms循环
{
second_T1=10;//循环次数
if(key==1)//按键1
{
flag_1=~flag_1;//LED分钟个位显示取反
flag_2=~flag_2;//LED分钟十位显示取反
flag_3=1;//LED小时个位显示控制置位
flag_4=1;//LED小时十位显示控制置位
if(flag_1==0&&flag_2==0)//如果LED分钟显示控制位关闭
{
P0_7=0;//个位
P0_6=0;//十位
}
}
if(key==2)//按键2
{
flag_3=~flag_3;//LED小时个位显示取反
flag_4=~flag_4;//LED小时十位显示取反
flag_1=1;//LED分钟个位显示控制置位
flag_2=1;//LED分钟十位显示控制置位
if(flag_3==0&&flag_4==0)//如果LED小时显示控制位关闭
{
P0_5=0;//个位
P0_4=0;//十位
}
}
if(key==3)//按键3恢复显示控制位置位
{
flag_1=1;
flag_2=1;
flag_3=1;
flag_4=1;
}
}
while(keytime==0)
{
if(P2_6==0)
{
if(key==1)
{
count_m++;
if(count_m==0x3c)
{
count_m=0;
}
count_s=0;
}
if(key==2)
{
count_h++;
if(count_h==0x18)
{
count_h=0;
}
count_s=0;
}
}
keytime=0xe;
}
keytime--;
second_T1--;//递减
}
void delay_10ms()
{
int n;
int count;
for(count=10;count>=1;count--)
{
for(n=0;n<124;n++)
{
;
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-02-26
这太简单了,不过20分太少了点!
第2个回答  2010-02-25
我有51的,
相似回答