C语言编写单片机
#include"reg51.h"
chartab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
charpos[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
unsigned chardot[6]={0x00,0x80,0x00,0x80,0x00,0x00};
unsigned char idatadat[7];
unsigned charhour=12,chour=10;
unsigned charmin=10,cmin=10;
unsigned charsec=39,csec=10;
unsigned charhour_h,hour_l,min_h,min_l,sec_h,sec_l,value;
unsigned charsetmode=0;
unsigned charsetpos=0;
unsigned char dispos=0;
sbit key_set=P1^0;
sbit key_add=P1^1;
sbit key_dec=P1^2;
unsigned charflash_cnt;
bit flash_status;
unsigned chartime_cnt;
void main()
{
TMOD=0x11;
EA=1;
EX0=1;
IT0=1;
ET0=1;
ET1=1;
TH0=(65536-6000)/256;
TL0=(65536-6000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR1=1;
TR0=1;
while(1);
}
void time_t1() interrupt 3 //50ms
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
time_cnt++;
if(time_cnt==20) //1s
{
time_cnt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
hour=0;
}
}
}
}
void led_time() interrupt 1
{
TH0=(65536-6000)/256;
TL0=(65536-6000)%256;
hour_h=hour/10;
hour_l=hour%10;
min_h=min/10;
min_l=min%10;
sec_h=sec/10;
sec_l=sec%10;
dat[0]=hour_h;
dat[1]=hour_l;
dat[2]=min_h;
dat[3]=min_l;
dat[4]=sec_h;
dat[5]=sec_l;
P2=0xff;
if(dispos==3)
{
if (flash_status==0)
{
P0=tab[dat[dispos]]+0x00;
}
else
{
P0=tab[dat[dispos]]+0x80;
}
flash_cnt++;
if(flash_cnt==5)
{
flash_cnt=0;
if(flash_status==0)
{
//P0=tab[dat[dispos]]+0x80;
flash_status=1;
}
else
{
//P0=tab[dat[dispos]]+0x00;
flash_status=0;
}
}
}
else
P0=tab[dat[dispos]]+dot[dispos];
//P0=tab[dat[dispos]];
P2=pos[dispos];
dispos++;
dispos=dispos%6;
}
void key_int()interrupt 0
{
hour_h=hour/10;
hour_l=hour%10;
min_h=min/10;
min_l=min%10;
sec_h=sec/10;
sec_l=sec%10;
dat[0]=hour_h;
dat[1]=hour_l;
dat[2]=min_h;
dat[3]=min_l;
dat[4]=sec_h;
dat[5]=sec_l;
if(key_set==0)
{
if(setmode==0)
{
setmode=1;
TR0=0;
P0=tab[dat[0]];
P2=pos[0];
}
else
{
setpos++;
if(setpos==6)
{
setmode=0;
setpos=0;
TR0=1;
}
P0=tab[dat[setpos]]+dot[setpos];
P2=pos[setpos];
}
}
else if(key_add==0)
{
if(setmode==1)
{
switch(setpos)
{
case 0: hour_h++;
hour_h=hour_h%3;
break;
case 1: hour_l++;
hour_l=hour_l%10;
break;
case2: min_h++;
min_h=min_h%6;
break;
case3: min_l++;
min_l=min_l%10;
break;
case4: sec_h++;
sec_h=sec_h%6;
break;
case 5: sec_l++;
sec_l=sec_l%10;
break;
}
}
}
else if(key_dec==0)
{
if(setmode==1)
{
switch(setpos)
{
case 0: hour_h--;
hour_h=hour_h%3;
break;
case 1: hour_l--;
hour_l=hour_l%10;
break;
case2: min_h--;
min_h=min_h%6;
break;
case3: min_l--;
min_l=min_l%10;
break;
case4: sec_h--;
sec_h=sec_h%6;
break;
case 5: sec_l--;
sec_l=sec_l%10;
break;
}
}
}
dat[0]=hour_h;
dat[1]=hour_l;
dat[2]=min_h;
dat[3]=min_l;
dat[4]=sec_h;
dat[5]=sec_l;
P0=tab[dat[setpos]]+dot[setpos];
P2=pos[setpos];
hour=hour_h*10+hour_l;
min=min_h*10+min_l;
sec=sec_h*10+sec_l;
}还差闹钟的部分,望大神补充程序
1,数码管显示时、分、秒,可以通过第1个按键进入时间设置,2、3按键进行每一个时间数的加减设置
2,可以设置闹钟,设置闹钟到时间后LED闪烁几次
用AT89C51 ,C语言编写,
Keil C51软件编程,Proteus ISIS 仿真运行
望大神给程序 有注释更好了
1 0 0 2 9 2 5 0 9 4