8数码管闹钟c程序Word文档格式.docx
《8数码管闹钟c程序Word文档格式.docx》由会员分享,可在线阅读,更多相关《8数码管闹钟c程序Word文档格式.docx(22页珍藏版)》请在冰豆网上搜索。
![8数码管闹钟c程序Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-11/24/d81ac652-ac79-4e7a-a088-15086992675b/d81ac652-ac79-4e7a-a088-15086992675b1.gif)
display(a0,a1,a2,a3,a4,a5,a6,a7);
//显示
chuli();
aloutput();
if(stoptime)stoptimes();
if(starttime)starttimes();
if(!
settime)timerup();
if(saveiic)iicsave();
if(readiic)iicread();
if(function==1||function_al!
=0)reset=0;
}
delay1ms_c.c>
ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x40,0xb8,0xf4};
//0~9,OFF,-,h.,L.
//***************显示子程序*******************************************************************
voiddisplay(uchara7,uchara6,uchara5,uchara4,uchara3,uchara2,uchara1,uchara0)
{P0=table[a0];
P1=0xfe;
delay
(2);
P0=table[a1];
P1=0xfd;
P0=table[a2];
P1=0xfb;
P0=table[a3];
P1=0xf7;
P0=table[a4];
P1=0xef;
P0=table[a5];
P1=0xdf;
P0=table[a6];
P1=0xbf;
P0=table[a7];
P1=0x7f;
//******************将数送至缓冲单元,当shan的标志位为1时,再根据此时的此时的功能标示,对该位熄灭**************************
voidsend()
{if(function_al==0)//time
{a0=sec%10;
a1=sec/10;
a2=11;
a3=min%10;
a4=min/10;
a5=11;
a6=hour%10;
a7=hour/10;
if(function_al!
=0)//al
{a0=al[function_al-1].s%10;
a1=al[function_al-1].s/10;
a3=al[function_al-1].m%10;
a4=al[function_al-1].m/10;
a6=al[function_al-1].alon+add;
a7=function_al;
if(shan==1&
next==0&
(function_al!
=0||function!
=0))
{a0=10,a1=10;
a2=10;
a3=10,a4=10;
a5=10;
a6=10,a7=10;
}
next==1&
}
next==2&
{a3=10,a4=10;
next==3&
{a6=10,a7=10;
voiddisplay(uchara7,uchara6,uchara5,uchara4,uchara3,uchara2,uchara1,uchara0);
voidsend();
voiddelay(uintcount);
#defineAT24C020xa0//AT24C02地址
bitISendStr(unsignedcharsla,unsignedcharsuba,unsignedchar*s,unsignedcharno);
bitIRcvStr(unsignedcharsla,unsignedcharsuba,unsignedchar*s,unsignedcharno);
ucharreset,t0,t1,sec,min,hour,function,a0,a1,a2,a3,a4,a5,a6,a7;
ucharfunction_al,next,stop;
ucharadd=11,pDat[24];
bitshan,stoptime,starttime,settime,saveiic,readiic;
struct{ucharalon;
ucharm;
uchars;
}al[8];
//8AL结构数组
voidresets()
init_ds1302();
al[0].m=8;
al[0].s=3;
al[0].alon=1;
al[1].m=12;
al[1].s=0;
al[1].alon=2;
al[2].m=13;
al[2].s=33;
al[2].alon=1;
//al[3].m=17;
al[3].s=30;
al[3].alon=2;
//al[4].m=18;
al[4].s=3;
al[4].alon=1;
//al[5].m=21;
al[5].s=0;
al[5].alon=2;
//al[6].m=0;
al[6].s=0;
al[6].alon=1;
//al[7].m=8;
al[7].s=0;
al[7].alon=2;
saveiic=1;
voidiicsave()//保存数据
{uchari;
EA=0;
2;
{pDat[i+0]=al[i].s;
pDat[i+2]=al[i].m;
pDat[i+4]=al[i].alon;
ISendStr(AT24C02,0,&
pDat[0],6);
delay(20);
for(i=2;
4;
{pDat[i+4]=al[i].s;
pDat[i+6]=al[i].m;
pDat[i+8]=al[i].alon;
saveiic=0;
EA=1;
voidiicread()//读取数据
IRcvStr(AT24C02,0,&
{al[i].s=pDat[i+0];
al[i].m=pDat[i+2];
al[i].alon=pDat[i+4];
}delay(20);
{al[i].s=pDat[i+4];
al[i].m=pDat[i+6];
al[i].alon=pDat[i+8];
readiic=0;
voidstoptimes()
write_ds1302(0x8e,0x00);
//WP写保护关
write_ds1302(0x80,0x80|((sec/10)*16+(sec%10)));
//stop秒
write_ds1302(0x8e,0x80);
//写保护开
stoptime=0;
starttime=0;
voidstarttimes()
write_ds1302(0x80,0x00|((sec/10)*16+(sec%10)));
voidsavetime()//savetime
write_ds1302(0x82,0x80|((min/10)*16+(min%10)));
write_ds1302(0x84,0x80|((hour/10)*16+(hour%10)));
//***********************************************
ucharkeychuli()
{uchark;
k=P3;
//P1口内容送K
k=~k;
//取反
k=(k&
0x3f);
//屏蔽高3位,如果扫描8键,屏蔽该句。
return(k);
//返回键值
}
/************************************************************************
*函数原型:
key();
*功能:
键盘扫描函数,函数返回值即键值。
************************************************************************/
ucharkey()
ucharkeyzhi,keyzhii;
//电子钟键盘按键键值临时存放
keyzhi=keychuli();
//调P1口处理函数
if(keyzhi!
=0)//有键动作延时去抖动,否则函数返回
{
send();
display(a0,a1,a2,a3,a4,a5,a6,a7);
//再次调P1口处理函数
=0)//真正有键按下,取键值并暂存
keyzhii=keyzhi;
while(keyzhi!
=0)//判按键是否释放,没有释放延时去抖动等待释放
{
send();
keyzhi=keyzhii;
//按键释放后恢复按键键值
}
return(keyzhi);
//返回按键键值
//**************键值处理程序*************************
voidchuli()
{switch(key())
case0:
break;
case1:
//
{function++;
switch(function)
{case1:
if(function_al==0){settime=1;
stoptime=1;
TR1=1;
shan=1;
if(function_al!
=0){settime=0;
stoptime=0;
starttime=1;
TR1=0;
shan=0;
next=0;
function=0;
function_al=0;
}break;
case2:
savetime();
settime=0;
break;
//
break;
case2:
//+++
{
if(function_al==0&
function!
=0)
{switch(next)
{case1:
sec++;
if(sec==60)sec=0;
case2:
min++;
if(min==60)min=0;
case3:
hour++;
if(hour==24)hour=0;
if(function_al!
al[function_al-1].s++;
if(al[function_al-1].s==60)al[function_al-1].s=0;
al[function_al-1].m++;
if(al[function_al-1].m==24)al[function_al-1].m=0;
al[function_al-1].alon++;
if(al[function_al-1].alon==3)al[function_al-1].alon=0;
}}
}break;
case4:
//---
sec--;
if(sec==255)sec=59;
min--;
if(min==255)min=59;
hour--;
if(hour==255)hour=23;
al[function_al-1].s--;
if(al[function_al-1].s==255)al[function_al-1].s=59;
al[function_al-1].m--;
if(al[function_al-1].m==255)al[function_al-1].m=23;
al[function_al-1].alon--;
if(al[function_al-1].alon==255)al[function_al-1].alon=2;
case8:
//al
{function_al++;
switch(function_al)
{case1:
readiic=1;
TR1=1;
//case4:
//case5:
//case6:
//case7:
//case8:
case4:
case16:
//next
{if(function||function_al!
=0)next++;
switch(next)
case32:
{if(function==0&
function_al==0&
reset<
5)reset++;
switch(reset)
case5:
reset=0;
resets();
voidtimerup()//BCD
sec=(read_ds1302(0x81)&
0x0f)+((read_ds1302(0x81)>
>
4)&
0x07)*10;
//秒
min=(read_ds1302(0x83)&
0x0f)+((read_ds1302(0x83)>
//分
hour=(read_ds1302(0x85)&
0x0f)+((read_ds1302(0x85)>
0x03)*10;
//时
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitsclk=P3^6;
sbitio=P3^4;
sbitrst=P3^5;
voiddelay1302(uintx)
uinti,j;
for(i=x;
i>
0;
i--)
for(j=114;
j>
j--);
voidwrite_ds1302byte(uchardat)//上升沿写1字节数据
ACC=dat;
//数据放入累加器中
//rst=1;
启动数据传送
for(i=8;
io=ACC0;
//累加器数据赋给io送出
sclk=0;
sclk=1;
//上升沿
ACC=ACC>
1;
//累加器数据左移一位,先写最低位
ucharread_ds1302byte()//下降沿读1字节数据
//累加器数据左移一位,先读最低位
ACC7=io;
//把io线上的数据赋给累加器
//下降沿
returnACC;
//返回累加器数据
voidwrite_ds1302(ucharaddr,uchardat)//写地址,写数据
rst=0;
sclk=0;
delay1302
(1);
rst=1;
write_ds1302byte(addr);
//写地址
write_ds1302byte(dat);
//写数据
sclk=1;
//delay1302
(1);
//io=1;
ucharread_ds1302(ucharaddr)//写地址,读数据
ucharda;
da=read_ds1302byte();
returnda;
voidinit_ds1302()
//WP写保护关
write_ds1302(0x80,0x00);
//写秒,十进制初值
write_ds1302(0x82,0x00);
write_ds1302(0x84,0x00);
write_ds1302(0x86,0x10);
//day
write_ds1302(0x88,0x03);
//month
write_ds1302(0x8a,0x6);
//week
write_ds1302(0x8c,0x12);
//year
//write_ds1302(0x90,0x00);
//涡流^_^是指充电的了
//*********************