利用51单片机和DS12C887时钟芯片制作万年历

半个学期51的学习之后在终于动手敲了人生中第一个模块,期间出现了很多的bug,但还好的是最后都改正了

希望对大家有一点用处。

(单片机小白一个,欢迎大佬指点。如果有大佬碰巧看见的话能不能帮我找一下相关的proteus仿真图,不胜感激!)


#include

#include


void delayms(uint xms)
{
   int i,j;
   for(i=1;i<110;i++)
       for(j=1;j}


void di()
{
  beep=0;
  delayms(100);
  beep=1;
}


void write_com(uchar com)
{
   lcdrs=0;
   lcden=0;
   P0=com;
   delayms(5);
   lcden=1;
   delayms(5);
   lcden=0;
}


void write_date(uchar date)
{
   lcdrs=1;
   lcden=0;
   P0=date;
   delayms(50);
   lcden=1;
   delayms(5);
   lcden=0;
}


void  init()
{
   uchar num;
   EA=1;
   EX1=1;
   IT1=1;
   flag1=0;
   t0_num=0;
   s1num=0;
   week=1;
   dula=0;
   wela=0;
   lcden=0;
   write_12c78(0xA0,0x20);
   write_12c78(0xB0,0x26);
   set_time();
   write_com(0x38);
   write_com(0x0c);
   write_com(0x06);
   write_com(0x01);
   write_com(0x80);
   for(num=0;num<15;num++)
   {
      write_date(table[num]);
 delayms(5);
   }
   write_com(0x80+0x40+6);
   write_date(':');
   delayms(5);
   write_com(0x80+0x40+9);
   write_date(':');
   delayms(5);
}


void write_sfm(uchar adress,uchar date)
{
   char shi,ge;
   shi=date/10;
   ge=date%10;
   write_com(0x80+0x40+adress);
   write_date(0x30+shi);
   write_date(0x30+ge);
}


void write_nyr(uchar adress,uchar date)
{
   uchar shi,ge;
   shi=date/10;
   ge=date%10;
   write_com(0x80+adress);
   write_date(0x30+shi);
   write_date(0x30+ge);
}


void write_week(uchar week)
{  
   write_com(0x80+12);
   switch(week)
   { 
     case 1:
   write_date('M');
   delayms(5);
   write_date('O');
   delayms(5);
   write_date('N');
   delayms(5);

case 2:   
   write_date('T');
   delayms(5);
   write_date('U');
   delayms(5);
   write_date('E');
   delayms(5); 
case 3:
   write_date('W');
   delayms(5);
   write_date('E');
   delayms(5);
   write_date('D');
   delayms(5); 
case 4:
   write_date('T');
   delayms(5);
   write_date('H');
   delayms(5);
   write_date('U');
   delayms(5);
case 5:
   write_date('F');
   delayms(5);
   write_date('R');
   delayms(5);
   write_date('T');
   delayms(5);  
case 6:
   write_date('S');
   delayms(5);
   write_date('A');
   delayms(5);
   write_date('T');
   delayms(5);
case 7:
   write_date('S');
   delayms(5);
   write_date('U');
   delayms(5);
   write_date('N');
   delayms(5); 
   }
}


void keyscan()
{  
    if(flag_ri==1)
{
  if((s1==0)||(s2==0)||(s3==0)||(s4==0))
  {
      if((s1==0)||(s2==0)||(s3==0)||(s4==0))
  delayms(5);
  {
     while(!(s1&&s2&&s3&&s4));
 di();
 flag_ri=0;  
  }
  }
}
if(s1==0)
{
  delayms(5);
  if(s1==0)
  {
     s1num++;
 if(flag1==1)
    if(s1num==4)
    s1num=1;
 flag=1;
 while(!s1);
 di();   
 switch(s1num)
 { 
    case 1:write_com(0x80+0x40+10);
       write_com(0xf0);
break;
case 2:
       write_com(0x80+0x40+7);
break;
case 3:
       write_com(0x80+0x40+4);
break;
case 4:
      write_com(0x80+12);
  break;
case 5:   
       write_com(0x80+9);
break;
case 6:
       write_com(0x80+6);
break;
case 7:
      write_com(0x80+3);
      break;
case 8:
       s1num=0;
write_com(0xc0);
flag=0;
write_12c78(1,miao);
write_12c78(2,fen);
write_12c78(3,shi);
write_12c78(4,week);
write_12c78(5,day);
write_12c78(6,month);
write_12c78(7,year);
break;
 }
  }  
}


if(s1num!=0)
{
  if(s2==0)
  {
     delayms(5);
 if(s2==0)
 {  
    while(!s2);
di();
switch(s1num)
{
   case 1:
      miao++;
  if(miao==60)
      miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_12c78(1,miao);
break;
case 2:
      fen++;
  if(fen==60)
      fen=0;
  write_sfm(7,fen);
  write_com(0x80+0x40+7);
  write_12c78(2,fen);
  break;
case 3:
     shi++;
 if(shi==24)
    shi=0;
 write_sfm(4,shi);
 write_com(0x80+0x40+4);
 write_12c78(3,shi);
 break;
case 4:
     week++;
 if(week==8)
     week=1;
 write_week(week);
 write_com(0x80+12);
 write_12c78(4,week);
 break;
case 5:
     day++;
 if(day==32)
     day=1;
 write_nyr(9,day);
 write_com(0x80+9);
 write_12c78(5,day);
 break;
case 6:
      month++;
  if(month==13)
      month=1;
  write_nyr(6,month);
  write_com(0x80+6);
  write_12c78(6,month);
  break;
  case 7:
         year++;
 if(year==100)
     year=0;
 write_nyr(3,year);
 write_com(0x80+3);
 write_12c78(7,year);
 break;
}
 }
  }


  if(s3==0)
  {
     delayms(5);
 if(s3==0)
 {  
    while(!s3);
di();
switch(s1num)
{
   case 1:
      miao--;
  if(miao==-1)
      miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_12c78(1,miao);
case 2:
       fen--;
if(fen==-1)
   fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_12c78(2,fen);
break;
case 3:
       shi--;
if(shi==-1)
   shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_12c78(3,shi);
break;
case 4:
       week--;
if(week==-1)
   week=7;
write_week(week);
write_com(0x80+12);
write_12c78(4,week);
break;
case 5:
       day--;
if(day==-1)
   day=31;
write_nyr(9,day);
write_com(0x80+9);
write_12c78(5,day);
break;
case 6:
       month--;
if(month==-1)
   month=13;
write_nyr(6,month);
write_com(0X80+6);
write_12c78(6,month);
break;
case 7:
       year--;
if(year==-1)
   year=99;
write_nyr(3,year);
write_com(0x80+3);
write_12c78(7,year);
break;
}
 }
  }  


  if(s4==0)
  {
     delayms(5);
 if(s4==0)
 {
    flag1=~flag1;
while(!s4);
di();
if(flag1==0)
{
   flag=0;
write_com(0x80+0x40);
   write_date(' ');
write_date(' '); 
write_com(0x0c);
write_12c78(1,miao);
write_12c78(2,fen);
write_12c78(3,shi);
}
else
{
  read_alarm();
  miao=newmiao;
  fen=newfen;
  shi=newshi;
  write_com(0x80+0x40);
               write_date('R');
  write_date('i');
  write_com(0x80+0x40+3);
  write_sfm(4,newshi);
  write_sfm(7,newfen);
  write_sfm(10,newmiao);
}
 }
  }
}
}


void write_12c78(uchar adress,uchar date)  //12c78写地址,写数据函数。
{
   dscs=0;
   dsas=1;
   dsds=1;
   dsrw=1;
   P0=adress;
   dsas=0;
   dsrw=0;
   P0=date;
   dsrw=1;
   dsas=1;
   dscs=1;
}


uchar read_12c78(uchar daress)   //12c78读地址,数据函数。
{ //daress代表所取数据的地址
   uchar date;
   dsas=1;
   dsds=1;
   dsrw=1;
   P0=daress;
   dsas=0;
   dsds=0;
   P0=0xff;
   date=P0;
   dsds=1;
   dsas=1;
   return date;
}


void set_time()
{
  write_12c78(0,0);
  write_12c78(1,0);
  write_12c78(2,0);
  write_12c78(3,0);
  write_12c78(4,0);
  write_12c78(5,0);
  write_12c78(6,0);
  write_12c78(7,0);
  write_12c78(8,0);
  write_12c78(9,0);
}


void read_alarm()
{
   newmiao=read_12c78(1);
   newfen=read_12c78(2);
   newshi=read_12c78(3);
}


void main()
{
   init();
   while(1)
   {
     keyscan();
if(flag_ri==1)
{
   di();
delayms(100);
di();
delayms(500);
}
if(flag==0&&flag1==0)
{
   keyscan();
year=read_12c78(7);
month=read_12c78(6);
day=read_12c78(5);
week=read_12c78(4);
shi=read_12c78(3);
fen=read_12c78(2);
miao=read_12c78(1);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,miao);
write_week(week);
write_nyr(3,year);
write_nyr(6,month);
write_nyr(9,day);
}
   }
}


void exter() interrupt 2
{  
   uchar c;
   flag_ri=1;
   c=read_12c78(0x0c);

}



调用的define.h函数模块如下:

#define uchar unsigned char
#define uint unsigned  int


sbit dula=P2^6;
sbit wela=P2^7;
sbit lcdrs=P3^0;
sbit lcden=P3^1;
sbit s1=P1^4
sbit s2=P1^5;
sbit s3=P1^6;
sbit s4=P1^7;
sbit beep=P2^4;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirp=P3^3;


bit flag1,flag_ri;
uchar count,s1num,flag,t0_num;


uchar miao,fen,shi,month,week,day,year,newmiao,newshi,newfen;


uchar code table[]="20  -  -  ";


uchar code table1[]="    :  :  :";


void write_12c78(uchar,uchar);


void set_alarm(uchar,uchar,uchar);


void read_alarm();


uchar read_12c78(uchar);


void set_time();  

你可能感兴趣的:(利用51单片机和DS12C887时钟芯片制作万年历)