51读写时钟芯片DS1302的程序

时间:2024-02-22 08:48:30
51单片机读写DS1302的程序,希望对初学者有帮助

#include <reg51.h>
#include <intrins.h>

#define REG_SEC_ADD 0x80     //秒寄存器地址
#define REG_MIN_ADD 0x82     //分寄存器地址
#define REG_HR_ADD 0x84      //时寄存器地址
#define REG_DATE_ADD 0x86    //日期寄存器地址
#define REG_MONTH_ADD 0x88   //月份寄存器地址
#define REG_DAY_ADD 0x8A     //星期寄存器地址
#define REG_YEAR_ADD 0x8C    //年寄存器地址
#define REG_CONTROL_ADD 0x8E //控制寄存器

#define REG_TRICKLE_ADD 0x90 //慢速充电积存器
#define REG_CLOCK_BURST_ADD 0xBE //多字节方式
#define REG_BASE_RAM 0xC0; //RAM的基地址

sbit DS_CLK=P1^6;  //串行时钟
sbit DS_IO=P1^7;   //数据输入/输出
sbit DS_RST=P1^5;  //复位

void DS_Write_Byte(unsigned char tdata)
{  unsigned char i;
  
   for( i=0;i<8;i++ )
   { DS_CLK = 0;
     if( tdata&0x01 )
    DS_IO = 1;
     else
    DS_IO = 0;
     tdata = tdata>>1;
   
 DS_CLK = 1;   
    _nop_();  _nop_();  
   }
}
unsigned char DS_READ_Byte(void)
{  unsigned char i, tData;

   DS_IO=1;
   for( i=0;i<8;i++ )
   { DS_CLK = 1;
     _nop_();  _nop_();
     DS_CLK = 0;   
  _nop_();_nop_();
     tData = tData>>1;
     if(DS_IO) tData = tData|0x80 ;
   }
   return (tData);
}
unsigned char DS_Read( unsigned char CMD )
{ unsigned char dd=0;

  DS_RST = 0;
  _nop_();  
  DS_CLK = 0;
  _nop_();
  DS_RST = 1; 
  DS_Write_Byte(CMD); // Write Command Byte
  dd = DS_READ_Byte();// Read Data  
  DS_RST=0;
  DS_CLK=1;
  return(dd);
}
void DS_Write(unsigned char COMD,unsigned char Data)
{
  DS_RST = 0;
  _nop_();
  DS_CLK = 0;
  _nop_();
  DS_RST = 1; // Enable  
  DS_Write_Byte( COMD ); // Write Command
  DS_Write_Byte( Data ); // Write Data
  DS_RST = 0;
  DS_CLK = 1;
}

//写时钟时间到DS1302
void DS_SetClock(unsigned char tdata[7])
{
  DS_Write(REG_CONTROL_ADD,0); // Disabled Write Protect
  DS_Write(REG_SEC_ADD,0x80); // Disabled Clock

  DS_Write(REG_DAY_ADD,tdata[6]);   //Week Day
  DS_Write(REG_YEAR_ADD,tdata[5]);  //Year
  DS_Write(REG_MONTH_ADD,tdata[4]); //Month
  DS_Write(REG_DATE_ADD,tdata[3]);  //Date
  DS_Write(REG_HR_ADD,tdata[2]);    //Hour(24)
  DS_Write(REG_MIN_ADD,tdata[1]);   //Minute
  DS_Write(REG_SEC_ADD,tdata[0]&0x7F); //Second
                                     //MSB:0 Enabled Clock 
  DS_Write(REG_CONTROL_ADD,0x80);    //Enabled Write Protect
}


//从DS1302读时钟和时间
void DS_ReadClock(unsigned char tdata[7])
{
  tdata[6]=DS_Read(REG_DAY_ADD+1); // Week
  tdata[5]=DS_Read(REG_YEAR_ADD+1); // Year
  tdata[4]=DS_Read(REG_MONTH_ADD+1); // Month
  tdata[3]=DS_Read(REG_DATE_ADD+1); // Date
  tdata[2]=DS_Read(REG_HR_ADD+1); // Hour
  tdata[1]=DS_Read(REG_MIN_ADD+1)&0x7F; // Minute
  tdata[0]=DS_Read(REG_SEC_ADD+1)&0x7F; // Second
}

void DS_WriteRAM()
{ unsigned char i;
  DS_Write(REG_CONTROL_ADD,0); // Disabled Write Protect 
  //add范围 0xc1-0xff 中的偶数
  for(i=0xc0;i<=0xfd; i=i+2) DS_Write(i,i-10);//写测试值

  DS_Write(REG_CONTROL_ADD,0x80);    //Enabled Write Protect
}

unsigned char  DS_RedaRAM(unsigned char add)
{  //add范围 0xc1-0xff 中的奇数
   unsigned char i;
   i=DS_Read(add);
   return(i);
}

void delayms(unsigned char ms) 
// 延时子程序
{ unsigned char i;
 while(ms--)
    for(i = 0; i < 120; i++);
}

void main()

{ 

  unsigned char SetData[7],RecData[7],itemp;

  //初始化时间为  07-01-08 18:09:03   星期一
   SetData[6]=0x01;//Week
   SetData[5]=0x07;//Year
   SetData[4]=0x01;//month
   SetData[3]=0x08;//date
   SetData[2]=0x18;//Hour
   SetData[1]=0x09;//Minute 
   SetData[0]=0x03;//Second    
 
   DS_SetClock(SetData);

  DS_WriteRAM();
   while(1)
   {  
       delayms(200);

      itemp= DS_RedaRAM(0xe7);
      DS_ReadClock(RecData);

   }

}