简单时钟的C程序
#include<AT89X51.H>
#define uchar unsigned char
#define uint unsigned int
char data_7seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x80,0x90};
uchar hour=0,min=0,sec=0;
uchar deta=0;
sbit s1=P3^3;
sbit s2=P3^4;
sbit s3=P3^5;
sbit com6=P0^7;
sbit com5=P0^6;
sbit com4=P0^5;
sbit com3=P0^4;
sbit com2=P0^3;
sbit com1=P0^2;
sbit clk=P0^1;
sbit sdata=P0^0;
uchar set=0;
void send(uchar count);
void delay(uint k);
void conv();
void disp();
/*------------------发送数码管代码----------------------*/
void send(uchar count)
{uchar i,a=0x80,b;
for(i=0;i<8;i++)
{b=count&a;
if(b==a)
sdata=1;
else
sdata=0;
clk=0;
clk=1;
count=count<<1;
}
}
/*-------------------走时时钟部分-----------------------*/
void init_timer()
{TMOD=0x01;
TH0=-(4800/256);
TL0=-(4800%256);
IE=0x82;
TR0=1;
}
/*------------------------------------------------------*/
/*---------------5ms定时中断服务函数--------------------*/
void T0_srv(void) interrupt 1
{TH0=-(4800/256);
TL0=-(4800%256);
deta++;
}
/*------------------------------------------------------*/
/*-----------------时分钞走时单元转换-------------------*/
void conv()
{if(deta>=200) {deta==0;sec++;}
if(sec==60) {min++;sec=0;}
if(min==60) {hour++;min=0;}
if(hour==24) {hour=0;}
}
/*-------------------显示函数部分------------------------*/
/*------------------走时时间显示函数---------------------*/
void disp()
{uint m=1;
while(m)
{send(data_7seg[hour/10]);
com1=0;
delay(1);
com1=1;
send(data_7seg[hour%10]);
com2=0;
delay(1);
com2=1;
send(data_7seg[min/10]);
com3=0;
delay(1);
com3=1;
send(data_7seg[min%10]);
com4=0;
delay(1);
com4=1;
send(data_7seg[sec/10]);
com5=0;
delay(1);
com5=1;
send(data_7seg[sec%10]);
com6=0;
delay(1);
com6=1;
if(s3==0)
break;
}
}
/*------------------调整时间部分-------------------------*/
void set_time()
{if(s1==0)delay(1);
if(s1==0)hour++;
if(hour==24)hour=0;
disp();
delay(1);
if(s2==0)delay(1);
if(s2==0)min++;
if(min==60)min=0;
disp();
delay(1);
}
/*----------------按键扫描及延时函数部分-----------------*/
/*-----------------走时时间程序函数----------------------*/
void time()
{conv();
disp();
}
/*---------------------扫描按键函数----------------------*/
void scan_key()
{delay(1);
if(s3==0)set++;
if(set>=2)set=0;
}
/*---------------------延时函数--------------------------*/
void delay(uint k)
{uint i,j;
for(i=0;i<k;i++)
for(j=0;j<121;j++);
}
/*---------------------主函数----------------------------*/
void main()
{init_timer();
while(1)
{if(s3==0) scan_key();
switch(set)
{case 0:time();break;
case 1:set_time();break;
default:break;
}
}
}