单片机电子时钟C语言编写程序.docx
《单片机电子时钟C语言编写程序.docx》由会员分享,可在线阅读,更多相关《单片机电子时钟C语言编写程序.docx(13页珍藏版)》请在冰豆网上搜索。
单片机电子时钟C语言编写程序
#include
//***函数定义***
voidlong_delay(void); //长延时
voidshort_delay(void); //短暂延时
voiddelay10ms(unsignedchar); //延时10MS
voidwrite7279(unsignedchar,unsignedchar); //写入到HD7279
unsignedcharread7279(unsignedchar); //从HD7279读出
voidsend_byte(unsignedchar); //发送一个字节
unsignedcharreceive_byte(void); //接收一个字节
voidint1_7279key(void);
voidtest1(void);
voidjishi0(void);
voidxianshi0(void);
voidjishi1(void);
voidxianshi1(void);
voidalarm(void);
//***变量及I/O口定义***
unsignedchardigit[5];
unsignedcharkey_number,j,k,m=0,Flag=0;
unsignedinttmr,count,shi,fen,miao,nian,yue,day,shi1,fen1,miao1;
unsignedlongwait_cnter;
unsignedcharrqi[12]={32,28,32,31,32,31,32,32,31,32,31,32};
unsigned inti;
unsignedcharflash=0xfc;
bitkey_flag;//*定义按键标志*/
sbitcs=P2^6; //csatP1.4
sbitclk=P1^0; //clk连接于P1.5
sbitdat=P1^1; //dat连接于P1.2
sbitkey=P3^3; //key连接于P1.3
//******HD7279A指令******
#defineCMD_RESET0xa4
#defineCMD_TEST0xbf
#defineDECODE00x80
#defineDECODE10xc8
#defineCMD_READ0x15
#defineUNDECODE0x90
#defineRTL_CYCLE0xa3
#defineRTR_CYCLE0xa2
#defineRTL_UNCYL0xa1
#defineRTR_UNCYL0xa0
#defineACTCTL0x98
#defineSEGON0xe0
#defineSEGOFF0xc0
#defineBLINKCTL0x88
//***主程序***
main()
{
for(tmr=0;tmr<0x2000;tmr++); //上电延时
delay10ms(150);
EX1=1;//*外部中断1使能*/
IT1=1;//*外部中断1下降沿触发*/
EA=1;//*中断使能*/
TMOD=0x01;
TR0=1;
ET0=1;
TL0=0xED;
TH0=0x37;
test1();
shi=23;
fen=59;
miao=50;
shi1=00;
fen1=00;
miao1=00;
nian=2010;
yue=11;
day=25;
while
(1)
{
if(key_flag)
{
key_flag=0;
switch(key_number)
{
case0:
while
(1)
{ xianshi0();
if(key_flag)break; }break;
case1:
while
(1)
{xianshi1();
if(key_flag)break;}break;
case2:
while
(1)
{ xianshi0() ;
write7279(BLINKCTL,flash);
fen++;
if(fen==60)fen=0; xianshi0();
if(key_flag)break;}break;
case3:
while
(1)
{ xianshi0() ;
write7279(BLINKCTL,flash);
shi++;
if(shi==24) shi=0;xianshi0();
if(key_flag)break;}break;
case4:
while
(1)
{day++ ;
if(day==rqi[yue-1])day=1;xianshi1();
if(key_flag)break;}break;
case5:
while
(1)
{yue++;
if(yue==13)yue=1;xianshi1();
if(key_flag)break;}break;
case6:
while
(1)
{nian++;
xianshi1();
if(key_flag)break;}break;
case7:
while
(1)
{nian--;
xianshi1();
if(key_flag)break;}break;
case8:
while
(1)
{alarm();
if(key_flag)break;}break;
case9:
shi1++;alarm();
write7279(BLINKCTL,0xfc);
if(shi1==24)shi1=0;alarm();break;//闹钟调时
case10:
fen1++;alarm();
write7279(BLINKCTL,0xfc);
if(fen1==60)fen1=0;alarm();break; //闹钟调分
case11:
Flag=1;break;
case12:
Flag=0;break;
default:
break;
}
While(Flag==1)
{
if(fen1==fen&&shi1==shi)
{write7279(BLINKCTL,0x00);
if(key_flag)break;}
}
}
}
voidxianshi0(void)
{
write7279(DECODE0,miao%10);
write7279(DECODE0+1,miao/10);
write7279(DECODE0+2,0x0a);
write7279(DECODE0+3,fen%10);
write7279(DECODE0+4,fen/10);
write7279(DECODE0+5,0x0a);
write7279(DECODE0+6,shi%10);
write7279(DECODE0+7,shi/10);
}
voidxianshi1(void)
{
write7279(DECODE0,day%10);
write7279(DECODE0+1,day/10);
write7279(DECODE0+2,0x80+yue%10);
write7279(DECODE0+3,yue/10);
write7279(DECODE0+4,0x80+((nian%1000)%100)%10);
write7279(DECODE0+5,(((nian/10)%100)%10));
write7279(DECODE0+6,(nian/100)%10);
write7279(DECODE0+7,nian/1000);
}
voidtest1(void) //*复位加显示*/
{
cs=0;
send_byte(0xa4);//*CMD_RESETHD7279复位*/
write7279(0xc8,0x01);//*上电显示2010.11.01,方式1译码:
0xc8,显示位置0xc8+(0~7)*/
write7279(0xc9,0x00);
write7279(0xca,0x81);
write7279(0xcb,0x01);
write7279(0xcc,0x80);
write7279(0xcd,0x01);
write7279(0xce,0x00);
write7279(0xcf,0x02);
}
voidalarm(void)
{
write7279(DECODE0,miao1%10);
write7279(DECODE0+1,miao1/10);
write7279(DECODE0+2,0x0a);
write7279(DECODE0+3,fen1%10);
write7279(DECODE0+4,fen1/10);
write7279(DECODE0+5,0x0a);
write7279(DECODE0+6,shi1%10);
write7279(DECODE0+7,shi1/10);
}
voidjishi0(void)
{
if(miao==60)
{miao=0;fen++;
if(fen==60)
{fen=0;shi++;
if(shi==24){shi=0;
day++;}
}
}
}
voidjishi1(void)
{if(nian%4==0&&nian%100!
=0||nian%400==0)
rqi[1]=29;
elserqi[1]=28;
if(day==rqi[yue-1])
{yue++;
if(yue==12)nian++;
}
}
voidtimer0(void)interrupt1
{
TL0=0xED;
TH0=0x37;
count++;
if(count==20)
{count=0;
miao++;jishi0();jishi1();}
}
/************************************************************************************/
voidint1_7279key(void)interrupt2/*外部中断1函数*/
{
key_number=read7279(0x15);/*读取键值*/
if(key_number<=0x0f)
{
key_flag=1; /*设置键标志*/
}
}
/************************************************************************************/
voidwrite7279(unsignedcharcmd,unsignedchardta)/*HD7279写函数*/
{
send_byte(cmd);
send_byte(dta);
}
/************************************************************************************/
unsignedcharread7279(unsignedcharcommand) /*HD7279读函数*/
{
send_byte(command);
return(receive_byte());
}
/************************************************************************************/
voidsend_byte( unsignedcharout_byte) /*HD7279发送字节函数*/
{
unsignedchari;
cs=0;
long_delay();
for(i=0;i<8;i++)
{
if(out_byte&0x80)
{
dat=1;
}
else
{
dat=0;
}
clk=1;
short_delay();
clk=0;
short_delay();
out_byte=out_byte*2;
}
dat=0;
}
/************************************************************************************/
unsignedcharreceive_byte(void) /*HD7279接收字节函数*/
{
unsignedchari,in_byte;
dat=1; //settoinputmode
long_delay();
for(i=0;i<8;i++)
{
clk=1;
short_delay();
in_byte=in_byte*2;
if(dat)
{
in_byte=in_byte|0x01;
}
clk=0;
short_delay();
}
dat=0;
return(in_byte);
}
/************************************************************************************/
voidlong_delay(void) /*长延时函数*/
{
unsignedchari;
for(i=0;i<0x30;i++);
}
/************************************************************************************/
voidshort_delay(void) /*短延时函数*/
{
unsignedchari;
for(i=0;i<8;i++);
}
/************************************************************************************/
//*************************延时n*10ms*************************
voiddelay10ms(unsignedchartime)
{
unsignedchari;
unsignedintj;
for(i=0;i