《电子技术应用》
您所在的位置:首页 > 嵌入式技术 > 解决方案 > 基于AT89C52以及DS1302的电子时钟C程序

基于AT89C52以及DS1302的电子时钟C程序

2015-11-04
关键词: DS1302 AT89C52

说明:此程序为博主参考其他程序按键一为时间与日期切换键;按键二为时间或日期调整键,即当显示时间时,每按一次依次显示小时、分钟、秒;当显示日期时,每按一次依次显示年、月、日;按键三和按键四依次为加键和减键,每按一次当松开时相应的变化一次。

#include <reg52.h>
#define uchar unsigned char  
#define uint unsigned int

sbit T_CLK = P1^0; //DS1302管脚定义
sbit T_IO  = P1^1;  
sbit T_RST = P1^2;

sbit KEY_1 = P1^3; //按键管脚定义
sbit KEY_2 = P1^4;
sbit KEY_3 = P1^5; 
sbit KEY_4 = P1^6;

sbit ACC0  = ACC^0;
sbit ACC7  = ACC^7;

uchar DUAN_CODE[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X0};  //共阴极数码管段码表
uchar BIT_CODE[]={0XFE,0XFD,0XFB,0XF7,0XEF,0XDF};  //数码管位选
uchar second,minute,hour,day,month,year,flag,keyvalue,value=0;
uchar Time[6]={0X0,0X0,0X0,0X0,0X0,0X0};
bit setsecond,setminute,sethour,setday,setmonth,setyear,distime=1,disdate=0;
void Delay(unsigned int i)
{
  unsigned int j;
   for(j=0;j<i;j++);
}

unsigned char BCD2HEX(unsigned char BCDChar)    
{
   unsigned char temp;
   temp=(BCDChar/16*10+BCDChar);
   return temp;
}
 
unsigned char HEX2BCD(unsigned char HEXChar)
{
   unsigned char temp;
   temp=(HEXChar/10*16+HEXChar);
   return temp;
}
 
void WriteByteDS1302(unsigned char Data)
{
   unsigned char i;
   ACC = Data;
   for(i=8; i>0; i--)
   {
       T_IO = ACC0;        
       T_CLK = 1;
       T_CLK = 0;
       ACC = ACC >> 1;
   }
}
 
unsigned char ReadByteDS1302(void)
{
   unsigned char i;
   for(i=8; i>0; i--)
   {
       ACC = ACC >>1;      
       ACC7 = T_IO;
       T_CLK = 1;
       T_CLK = 0;
   }
   return(ACC);
}
 
void WriteDS1302(unsigned char Addr,unsigned char Data)
{
   T_RST = 0;
   T_CLK = 0;
   T_RST = 1;
   WriteByteDS1302(Addr);   
   WriteByteDS1302(Data); 
   T_CLK = 1;
   T_RST = 0;
}
 
unsigned char ReadDS1302(unsigned char Addr)
{
   unsigned char Data;
   T_RST = 0;
   T_CLK = 0;
   T_RST = 1;
   WriteByteDS1302(Addr);         
   Data = ReadByteDS1302();    
   T_CLK = 1;
   T_RST = 0;
   return(Data);
}
 
void DS1302_INI()  //设置ds1302函数,初始化时间和日期
{
 WriteDS1302(0x8e,0x00);  //关闭写保护,允许写操作
 WriteDS1302(0x80,0x51);  //秒
 WriteDS1302(0x82,0x59);  //分
 WriteDS1302(0x84,0x12);  //时
 WriteDS1302(0x86,0x20);  //日
 WriteDS1302(0x88,0x05);  //月
 WriteDS1302(0x8c,0x12);  //年
 WriteDS1302(0x90,0xa5);  //充电
 WriteDS1302(0xc0,0xf0);  //判断是否初始化一次标识写入
 WriteDS1302(0x8e,0x80);  //打开写保护,禁止写操作
}

void ReadTime()
{
 second  = BCD2HEX(Time[0]=ReadDS1302(0x81));
 minute  = BCD2HEX(Time[1]=ReadDS1302(0x83));
 hour    = BCD2HEX(Time[2]=ReadDS1302(0x85));
 day     = BCD2HEX(Time[3]=ReadDS1302(0x87));
 month   = BCD2HEX(Time[4]=ReadDS1302(0x89));
 year    = BCD2HEX(Time[5]=ReadDS1302(0x8d));
 
}
 
void Set(unsigned char sel,unsigned char selby)
{
 unsigned char address,item;
 unsigned char max,min;
 if(sel==0) {address=0x80; max=59;min=0;}   //秒
 if(sel==1) {address=0x82; max=59;min=0;}   //分钟
 if(sel==2) {address=0x84; max=23;min=0;}   //小时
 if(month==2)
 {
  if(flag==1)
  {
   if(sel==3) {address=0x86; max=29;min=1;}
  }
  else
  {
   if(sel==3) {address=0x86; max=28;min=1;}
  }
 }
 else
 {
  if(month==1|month==3|month==5|month==7|month==8|month==10|month==12)
  {   
   if(sel==3) {address=0x86; max=31;min=1;}
  }
  if(month==4|month==6|month==9|month==11)
  {   
   if(sel==3) {address=0x86; max=30;min=1;}
  }
 }
 if(sel==4) {address=0x88; max=12;min=1;}   //月
   if(sel==5) {address=0x8c; max=99;min=0;}   //年
 item=ReadDS1302(address+1)/16*10+ReadDS1302(address+1);
   if (selby==0) item++; else item--;
   if(item>max) item=min;  
   if(item<min) item=max;
   WriteDS1302(0x8e,0x00);
   WriteDS1302(address,item/10*16+item);
   WriteDS1302(0x90,0xa5);
   WriteDS1302(0x8e,0x80); 
}
void Display()
{
 uchar i=0;

 for(i=0;i<6;i++)
 {
  P0=DUAN_CODE[Time[i]];  
  P2=BIT_CODE[i];
  Delay(5);
  P2=0xff;  
 } 
}
void DisplayMode()
{
 if(distime==1)
 {
    if(KEY_2==0)
    {
        Delay(10);  //延时去抖
        if(KEY_2==0)
        {
    while(KEY_2==0);
            value++;
    if(value>=4)value=0;
        }
  }
  if(value==0)
  {
   sethour=0;
   setminute=0;
   setsecond=0;

   setyear=0;
   setmonth=0;
   setday=0;

   Time[0]=hour/10;
   Time[1]=hour;
   Time[2]=minute/10;
   Time[3]=minute;
   Time[4]=second/10;
   Time[5]=second;
  }
  if(value==1)
  {
   sethour=1;
   setminute=0;
   setsecond=0;

   setyear=0;
   setmonth=0;
   setday=0;
   Time[0]=hour/10;
   Time[1]=hour;
   Time[2]=10;
   Time[3]=10;
   Time[4]=10;
   Time[5]=10;
  }
  if(value==2)
  {
   sethour=0;
   setminute=1;
   setsecond=0;

   setyear=0;
   setmonth=0;
   setday=0;

   Time[0]=10;
   Time[1]=10;
   Time[2]=minute/10;
   Time[3]=minute;
   Time[4]=10;
   Time[5]=10;
  }
  if(value==3)
  {
   sethour=0;
   setminute=0;
   setsecond=1;

   setyear=0;
   setmonth=0;
   setday=0;

   Time[0]=10;
   Time[1]=10;
   Time[2]=10;
   Time[3]=10;
   Time[4]=second/10;
   Time[5]=second;
  }
 }
 if(disdate==1)
 {
    if(KEY_2==0)
    {
        Delay(10);  //延时去抖
        if(KEY_2==0)
        {
    while(KEY_2==0);
            value++;
    if(value>=4)value=0;
        }
  }
  if(value==0)
  {
   sethour=0;
   setminute=0;
   setsecond=0;

   setyear=0;
   setmonth=0;
   setday=0;

   Time[0]=year/10;
   Time[1]=year;
   Time[2]=month/10;
   Time[3]=month;
   Time[4]=day/10;
   Time[5]=day;
  }
  if(value==1)
  {
   sethour=0;
   setminute=0;
   setsecond=0;

   setyear=1;
   setmonth=0;
   setday=0;
   Time[0]=year/10;
   Time[1]=year;
   Time[2]=10;
   Time[3]=10;
   Time[4]=10;
   Time[5]=10;
  }
  if(value==2)
  {
   sethour=0;
   setminute=0;
   setsecond=0;

   setyear=0;
   setmonth=1;
   setday=0;

   Time[0]=10;
   Time[1]=10;
   Time[2]=month/10;
   Time[3]=month;
   Time[4]=10;
   Time[5]=10;
  }
  if(value==3)
  {
   sethour=0;
   setminute=0;
   setsecond=0;

   setyear=0;
   setmonth=0;
   setday=1;

   Time[0]=10;
   Time[1]=10;
   Time[2]=10;
   Time[3]=10;
   Time[4]=day/10;
   Time[5]=day;
  }
 }
}
void KeyCheck()
{
   if(KEY_1==0)
   {
       Delay(5);
       if(KEY_1==0)
       {
          keyvalue=1;
       }
       while(KEY_1==0)Display();
   }
}
void KeyCheckADDorPLUS()
{
   if(KEY_3==0)
   {
       Delay(5);
       if(KEY_3==0)
       {
   while(KEY_3==0);
           if(setsecond==1)Set(0,0);
   else if(setminute==1)Set(1,0);
   else if(sethour==1)Set(2,0);
   else if(setday==1)Set(3,0);
   else if(setmonth==1)Set(4,0);
   else if(setyear==1)Set(5,0);
       }

   }
   if(KEY_4==0)
   {
       Delay(5);
       if(KEY_4==0)
       {
   while(KEY_4==0);
           if(setsecond==1)Set(0,1);
   else if(setminute==1)Set(1,1);
   else if(sethour==1)Set(2,1);
   else if(setday==1)Set(3,1);
   else if(setmonth==1)Set(4,1);
   else if(setyear==1)Set(5,1);
       }

   }
}
void ChangeDisplay()  
{
 
 if(keyvalue==1)
   {
  if(distime)
  {
   distime=0;
   disdate=1;
   }
  else if(disdate)
  {
   distime=1;
   disdate=0;
  }
 }
 keyvalue=0;
}
void LeapYear(void)
{
 if((year%4==0&&year0!=0)||(year0==0&&year@0==0))
 {
  flag=1;
 }
 else
 {
  flag=0;
 }
 if(flag==1&month==2&day==29&hour==0&minute==0&second==1)
 {
   WriteDS1302(0x8e,0x00);//允许写操作
   WriteDS1302(0x88,0x03);
   WriteDS1302(0x86,0x01);
   WriteDS1302(0x90,0xa5);//充电 
   WriteDS1302(0x8e,0x80);//禁止写操作
 }
 if(flag==0&month==2&day==28&hour==0&minute==0&second==1)
 {
   WriteDS1302(0x8e,0x00);//允许写操作
   WriteDS1302(0x88,0x03);
   WriteDS1302(0x86,0x01);
   WriteDS1302(0x90,0xa5);//充电 
   WriteDS1302(0x8e,0x80);//禁止写操作
 }
}
void main()
{
 DS1302_INI();
 while(1)
 {
  KeyCheck();
  ChangeDisplay();
  ReadTime();
  DisplayMode();
  Display();
  KeyCheckADDorPLUS();
  DisplayMode();
  LeapYear(); //闰年修正
 } 
}

6007ef94hbf9b5a38c83c&690.jpg

本站内容除特别声明的原创文章之外,转载内容只为传递更多信息,并不代表本网站赞同其观点。转载的所有的文章、图片、音/视频文件等资料的版权归版权所有权人所有。本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如涉及作品内容、版权和其它问题,请及时通过电子邮件或电话通知我们,以便迅速采取适当措施,避免给双方造成不必要的经济损失。联系电话:010-82306118;邮箱:aet@chinaaet.com。