当前位置:文档之家› 基于单片机的电子日历时钟设计

基于单片机的电子日历时钟设计

#include<STC12C5A.h>#define uchar unsigned char#define uint unsigned int//----端口定义---sbit ACC_7=ACC^7;sbit RST1=P2^5;sbit IO=P2^6;sbit SCLK=P2^7;sbit k1=P3^2;sbit k2=P3^3;sbit k3=P2^2;sbit k4=P2^3;//uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // 数码的位选,左到右uchar tab_1302[7]={45,50,11,19,1,1,15};uchar tab_time[8]={0,0,10,0,0,10,0,0}; //时间uchar tab_day[8]={0,0,10,0,0,10,0,0,}; //年月日uchar tab_num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//0 1 2 3 4 5 6 7 8 9 - {"0123456789-"}////////////=============函数声明============////////////////void display_time();void delayms(uint);void display_day();void ds1302(); //获取DS1302的时间void ds1302_init(); //DS1302的初始化void write1302(uchar,uchar); //指定地址向DS1302写数据uchar read1302(uchar); //指定地址向DS1302读数据void ds1302();void int0_init();/////////=======中断初始化=======///////////void int0_init(){EX0=1;IT0=1;EX1=1;IT1=1;EA=1;}///////////========时间显示======///////////// void display_time(){P1=0x7f;P0=tab_num[tab_time[7]];delayms(10);P1=0xbf;P0=tab_num[tab_time[6]];delayms(10);P1=0xdf;P0=tab_num[tab_time[5]];delayms(10);P1=0xef;P0=tab_num[tab_time[4]];delayms(10);P1=0xf7;P0=tab_num[tab_time[3]];delayms(10);P1=0xfb;P0=tab_num[tab_time[2]];delayms(10);P1=0xfd;P0=tab_num[tab_time[1]];delayms(10);P1=0xfe;P0=tab_num[tab_time[0]];delayms(10);}//////////=========延时函数========//////////////// void delayms(uint x){int i,j;for(i=x;i>=0;i--)for(j=0;j<=110;j++);}//////////=======日期显示======///////////void display_day(){P1=0x7f;P0=tab_num[tab_day[7]];delayms(10);P1=0xbf;P0=tab_num[tab_day[6]];delayms(10);P1=0xdf;P0=tab_num[tab_day[5]];delayms(10);P1=0xef;P0=tab_num[tab_day[4]];delayms(10);P1=0xf7;P0=tab_num[tab_day[3]];delayms(10);P1=0xfb;P0=tab_num[tab_day[2]];delayms(10);P1=0xfd;P0=tab_num[tab_day[1]];delayms(10);P1=0xfe;P0=tab_num[tab_day[0]];delayms(10);}////////////=========DS1302初始化======//////////// void ds1302_init() //DS1302初始化,设置初始时间{uchar i,add;add=0x80;write1302(0x8e,0x00);for(i=0;i<7;i++){write1302(add,tab_1302[i]);add+=2;}/* write1302(0x80,0x50); // 秒...write1302(0x82,0x59); // 分write1302(0x84,0x23); // 时write1302(0x86,0x19); // 日write1302(0x88,0x01); // 月write1302(0x8a,0x01); // 周write1302(0x8c,0x15); // 年*/write1302(0x8e,0x80);}//////////============从DS1302获取数据============///////////// uchar read1302(uchar add){uchar i,temp,dat1,dat2;RST1=0;SCLK=0;RST1=1;for(i=8;i>0;i--) //发送地址{SCLK=0;temp=add;IO=(bit)(temp&0x01);add>>=1;SCLK=1;}for(i=8;i>0;i--) //读取数据{ACC_7=IO;SCLK=1;ACC>>=1;SCLK=0;}RST1=0;dat1=ACC;dat2=dat1/16;dat1=dat1%16;dat1=dat1+dat2*10;return(dat1);}/////////============向DS1302写入数据程序================//////////// void write1302(uchar add,uchar dat){uchar i,temp;RST1=0;SCLK=0;RST1=1;for(i=0;i<8;i++) //发送地址{SCLK=0;temp=add;IO=(bit)(temp&0x01);add>>=1;SCLK=1;}temp=(dat/10<<4)|(dat%10);for(i=0;i<8;i++) //发送数据{SCLK=0;IO=(bit)(temp&0x01);temp>>=1;SCLK=1;}RST1=0;}/////////-------------获取DS1302的时间-----------////////void ds1302(){uchar i,add=0x81;write1302(0x8e,0x00); //允许向DS1302写入数据for(i=0;i<7;i++){tab_1302[i]=read1302(add); //获得的数据已转换为十进制add+=2;}write1302(0x8e,0x80); //获取完一次时间,禁止向DS1302写入数据,提高可靠}///////////============转换函数============///////////void change(){tab_time[0]=tab_1302[2]/10; //小时十位tab_time[1]=tab_1302[2]%10; //小时个位tab_time[3]=tab_1302[1]/10; //分十位tab_time[4]=tab_1302[1]%10; //分个位tab_time[6]=tab_1302[0]/10; //秒十位tab_time[7]=tab_1302[0]%10; //秒个位tab_day[0]=tab_1302[6]/10; //年十位tab_day[1]=tab_1302[6]%10; //年个位tab_day[3]=tab_1302[4]/10; //月十位tab_day[4]=tab_1302[4]%10; //月个位tab_day[6]=tab_1302[3]/10; //日十位tab_day[7]=tab_1302[3]%10; //日个位}////////////========主函数=======///////////// void main(){P2M0=0xff;P2M1=0;ds1302_init();int0_init();while(1){ds1302();change();display_time();while(!k4){ds1302();change();display_day();}}}/////========time=========////////////void int0() interrupt 0{uchar ky=0,n1,n2,n3=1,flag=1;while(flag!=0){write1302(0x8e,0x00);write1302(0x80,0x80|tab_1302[0]);P1=0xdf;P0=tab_num[tab_time[5]];delayms(10);P1=0xfb;P0=tab_num[tab_time[2]];delayms(10);if(k3==0){delayms(50);if(k3==0){ky++;while(!k3);if(ky==5){ky=0;}}}//////////=======1========////////if(ky==1){n1=tab_1302[0];if(k4==0){delayms(30);if(k4==0){n1++;while(!k4);if(n1>=60)n1=0;}}tab_1302[0]=n1;change();P1=0x7f;P0=tab_num[tab_time[7]];delayms(10);P1=0xbf;P0=tab_num[tab_time[6]];delayms(10);}////////=======2======////////if(ky==2){n2=tab_1302[1];if(k4==0){delayms(30);if(k4==0){n2++;while(!k4);if(n2>=60)n2=0;}}tab_1302[1]=n2;change();P1=0xef;P0=tab_num[tab_time[4]];delayms(10);P1=0xf7;P0=tab_num[tab_time[3]];delayms(10);P1=0x7f;P0=tab_num[tab_time[7]];delayms(10);P1=0xbf;P0=tab_num[tab_time[6]];delayms(10);}/////==3==///if(ky==3){n3=tab_1302[2];if(k4==0){delayms(10);if(k4==0){n3++;while(!k4);if(n3==24)n3=1;}}tab_1302[2]=n3;change();P1=0xfd;P0=tab_num[tab_time[1]];delayms(10);P1=0xfe;P0=tab_num[tab_time[0]];delayms(10);P1=0xef;P0=tab_num[tab_time[4]];delayms(10);P1=0xf7;P0=tab_num[tab_time[3]];delayms(10);P1=0x7f;P0=tab_num[tab_time[7]];delayms(10);P1=0xbf;P0=tab_num[tab_time[6]];delayms(10);}//////===4====////if(ky==4){flag=0;ds1302_init();}}}/////////////=============day================///////////// void int1() interrupt 2{uchar ky=0,n1,n2=1,n3=1,flag=1;while(flag!=0){write1302(0x8e,0x00);write1302(0x80,0x80|tab_1302[0]);P1=0xdf;P0=tab_num[tab_day[5]];delayms(10);P1=0xfb;P0=tab_num[tab_day[2]];delayms(10);if(k3==0){delayms(50);if(k3==0){ky++;while(!k3);if(ky==5){ky=0;}}}//////////=======1========////////if(ky==1){n1=tab_1302[6];if(k4==0){delayms(30);if(k4==0){n1++;while(!k4);if(n1==50)n1=0;}}tab_1302[6]=n1;change();P1=0xfd;P0=tab_num[tab_day[1]];delayms(10);P1=0xfe;P0=tab_num[tab_day[0]];delayms(10);}////////=======2======////////if(ky==2){n2=tab_1302[4];if(k4==0){delayms(30);if(k4==0){n2++;while(!k4);if(n2==13)n2=1;}}tab_1302[4]=n2;change();P1=0xfd;P0=tab_num[tab_day[1]];delayms(10);P1=0xfe;P0=tab_num[tab_day[0]];delayms(10);P1=0xef;P0=tab_num[tab_day[4]];delayms(10);P1=0xf7;P0=tab_num[tab_day[3]];delayms(10);}/////==3==///if(ky==3){n3=tab_1302[3];if(k4==0){delayms(10);if(k4==0){n3++;while(!k4);if(n3==31)n3=1;}}tab_1302[3]=n3;change();P1=0x7f;P0=tab_num[tab_day[7]];delayms(10);P1=0xbf;P0=tab_num[tab_day[6]];delayms(10);P1=0xef;P0=tab_num[tab_day[4]];delayms(10);P1=0xf7;P0=tab_num[tab_day[3]];delayms(10);P1=0xfd;P0=tab_num[tab_day[1]];delayms(10);P1=0xfe;P0=tab_num[tab_day[0]];delayms(10);}if(ky==4){flag=0;ds1302_init();}}}。

相关主题