⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendar.c

📁 p89c668是增强型51单片机
💻 C
字号:
#include<reg668.h>

#include<calender.h>

#define TIMER 500     //

bit GetBus(void)
{
 uchar a;
 S1CON=0xc5;          /*总线的初始化:设置时钟为(101)200K,MCU 主频为12M,ENS1=1,STA=0,和AA=1*/
 STA=1;               /*申请成为主机,起动总线*/
 while(SI==0)
 {
  a++;
  if(a>TIMER)return(0);
 }                 /*等待起始位的发送*/
 return(1);
}

bit SendByte(uchar c)
{
 uchar a;
 S1DAT=c;
 S1CON=0XC5; /*清除SI 位等等*/
 while(SI==0)
 {
  a++;
  if(a>TIMER)return(0);
 }                 /*等待起始位的发送*/
 return(1);
}

bit w_i2c(uchar sla,uchar suba,uchar s)
{
 SDA=1;
 SCL=1;
 if(GetBus())
   {
     if(SendByte(sla))
       {
        if(S1STA!=0X18){S1CON=0XD5;return(0);}
        if(SendByte(suba))
          {
           if(S1STA!=0X28){S1CON=0XD5;return(0);}
           if(SendByte(s))
             {
              if(S1STA!=0X28){S1CON=0XD5;return(0);}
              S1CON=0XD5;                              //结束总线
              return(1);
             }
          }
       }
   }
}


bit r8563(uchar sla,uchar suba,uchar s[],uchar no)
{
 uchar i,a;
 SDA=1;
 SCL=1;
 if(GetBus())
   {
    if(SendByte(sla))
      {
       if(S1STA!=0X18){S1CON=0XD5;return(0);}
       if(SendByte(suba))
         {
          if(S1STA!=0X28){S1CON=0XD5;return(0);}
          S1CON=0XE5; /*重新启动总线*/ // STA=1;
          while(SI==0)
              {
               a++;
               if(a>TIMER)return(0);
              }
          if(SendByte(sla+1))
            {
             if(S1STA!=0X40){S1CON=0XD5;return(0);}
             for(i=0;i<no-1;i++)
                {
                 S1CON=0XC5;            //接收一字节数据并发送应答位,SI=0
                 while(SI==0)
                     {
                      a++;
                      if(a>TIMER)return(0);
                     }
                 if(S1STA!=0X50){S1CON=0XD5;return(0);}
                 s[i]=S1DAT;            //读取数据
                }
             S1CON=0XC1;                //接收最后一字节数据并发送非应答位,SI=0
             while(SI==0)
                 {
                  a++;
                  if(a>TIMER)return(0);
                 }
             s[no-1]=S1DAT;
             S1CON=0XD5; /*结束总线*/
             return(1);
            }
         }
      }
   }
}

//*******************************************************************************

void init_p8563()        //
{
 w_i2c(0xA2,0x00,0x00);
 w_i2c(0xA2,0x01,0x00);
}

///____________________________________________________________________________
void pcf8563()
{
 uchar i;
 static uchar data dtime_old[5];
 uchar data dtime_new[6];

 uchar year_high,year_low;
 uchar month_high,month_low;
 uchar day_high,day_low;
 uchar hour_high,hour_low;
 uchar minute_high,minute_low;

 r8563(0xA2,0X03,dtime_new,6);

 dtime_new[0]=dtime_new[0] & 0x7F;  //minute
 dtime_new[1]=dtime_new[1] & 0x3F;  //hour
 dtime_new[2]=dtime_new[2] & 0x3F;  //day
 dtime_new[3]=dtime_new[4] & 0x1F;  //month
 dtime_new[4]=dtime_new[5] & 0xFF;  //year

 if(dtime_old[0]!= dtime_new[0])    //minute
 {
  dtime_old[0]= dtime_new[0];
  dtime_old[1]= dtime_new[1];
  dtime_old[2]= dtime_new[2];
  dtime_old[3]= dtime_new[3];
  dtime_old[4]= dtime_new[4];

  minute_high=dtime_old[0]>>4;
  minute_low=dtime_old[0] & 0x0F;
  disp(1,13,3,26,numS[minute_high]);
  disp(1,13,3,27,numS[minute_low]);
  minute=minute_high*10+minute_low;

  hour_high=dtime_old[1]>>4;
  hour_low=dtime_old[1] & 0x0F;
  disp(1,13,3,23,numS[hour_high]);
  disp(1,13,3,24,numS[hour_low]);
  hour=hour_high*10+hour_low;

  day_high=dtime_old[2]>>4;       //day
  day_low=dtime_old[2] & 0x0F;
  disp(1,13,3,19,numS[day_high]);
  disp(1,13,3,20,numS[day_low]);
  day=day_high*10+day_low;

  month_high=dtime_old[3]>>4;
  month_low=dtime_old[3] & 0x0F;
  disp(1,13,3,16,numS[month_high]);
  disp(1,13,3,17,numS[month_low]);
  month=month_high*10+month_low;

  year_high=dtime_old[4]>>4;
  year_low=dtime_old[4] & 0x0F;
  disp(1,13,3,13,numS[year_high]);
  disp(1,13,3,14,numS[year_low]);
  year=year_high*10+year_low;

  S0BUF=30;
  while(!TI);
  TI=0;
  for(i=0;i<5;i++)
  {
   S0BUF=dtime_old[i];
   while(!TI);
   TI=0;
  }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -