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

📄 ds1302op.c

📁 MSP430f149完成对DS1302时钟芯片的驱动代码
💻 C
字号:
#include "DS1302OP.h"
uchar test=0xFF;

void delay(uchar timeset)
{
  while(timeset--){};
  
}


void WriteTo1302(uchar nAddr,uchar nval)
{
  uchar i;
  DataDir_Out
  DS_RST_0
     delay(1);
       DS_Clock_0
           delay(1);
              DS_RST_1
                  delay(1);
   for(i=0;i<8;i++)
  {
      if(nAddr & 0x01)
     {
       DS_Data_1
     }
     else DS_Data_0
     delay(1);
        DS_Clock_1 
          delay(1);
             DS_Clock_0
                  delay(1);
                     nAddr>>=1;
  }  
     for(i=0;i<8;i++)
     {
       if(nval&0x01)
       {
         DS_Data_1
       }
    else DS_Data_0
     delay(1);
       DS_Clock_1 
         delay(1);
           DS_Clock_0
             delay(1);
                 nval>>=1;
       }
   DS_RST_0
      delay(1);
   //DataDir_In         ?????????

}
    


uchar ReadFrom1302(uchar nAddr)
{ 
  uchar ndata=0,i;
  DataDir_Out
  DS_RST_0
    delay(1);
      DS_Clock_0
         delay(1);
            DS_RST_1
               delay(1);
  for(i=0;i<8;i++)
   {
      if(nAddr & 0x01)
     {
       DS_Data_1
     }
     else DS_Data_0
     delay(1);
        DS_Clock_1 
          delay(1);
             DS_Clock_0
                  delay(1);
                     nAddr>>=1;
     }  
  DataDir_In         
  for(i=0;i<8;i++)
  {

   if(Ds_Data_In)
     {
       ndata |=(0x01<<i);
     } 
   DS_Clock_1
      delay(1);
         DS_Clock_0
            delay(1);            
  }
  
  DS_RST_0
     delay(1);
  return(ndata);          
}




void BurstWriteTime(char *PClock)
{
  char i;
  WriteTo1302(0x8e,0x00);
  DS_RST_0
    DS_Clock_0
       DS_RST_1
  WriteByte(0xbe);//时钟多字节写命令
  for(i=8;i>0;i--)
  {
   WriteByte(*PClock);
   PClock++;
  }
  DS_Clock_1
  DS_RST_0
    
  }

void BurstReadTime(char *PClock)
{
  char i;
  DS_RST_0
    DS_Clock_0
      DS_RST_1
        
  WriteByte(0xbe);//时钟多字节读命令  
  for(i=8;i>0;i--)
  {
   *PClock = ReadByte();
   PClock++;
  }
  DS_Clock_1
  DS_RST_0      
  
}



void SetTimer(char *pSetTimer)
{
  WriteTo1302(DS_Year_WR,   *(pSetTimer+0));
  WriteTo1302(DS_Month_WR,  *(pSetTimer+1));
  WriteTo1302(DS_Date_WR,   *(pSetTimer+2));
  WriteTo1302(DS_Hour_WR,   *(pSetTimer+3));
  WriteTo1302(DS_Minute_WR ,*(pSetTimer+4));
  WriteTo1302(DS_Second_WR, *(pSetTimer+5));
}



void GetTimer(char *pGetTimer)
{
  *(pGetTimer+0)= ReadFrom1302(DS_Year_RD);
  *(pGetTimer+1)= ReadFrom1302(DS_Month_RD);
  *(pGetTimer+2)= ReadFrom1302(DS_Date_RD);
  *(pGetTimer+3)= ReadFrom1302(DS_Hour_RD);
  *(pGetTimer+4)= ReadFrom1302(DS_Minute_RD);
  *(pGetTimer+5)= ReadFrom1302(DS_Second_RD);
  
} 



//使能时钟振荡器
void OscEnable()
{
 WriteTo1302(DS_Second_WR,0x00);  
}




//使用24小时计时
void Osc24()
{

  WriteTo1302(DS_Hour_WR, 0x00);
}


void ChargeEnable()
{
 WriteTo1302(DS_Charger_WR, 0xa5);//涓流充电
}

void DsInit()
{  
   DS_RST_0
   delay(1);
   DS_Clock_0
   delay(1);
   DS_RST_1
   delay(1);
   OscEnable();           //时钟停止位使能
   WriteTo1302(DS_Control_WR,0x00);//允许写入单个数据字节
   ChargeEnable();       //充电使能
   Osc24();
   
}








void init(void)
{
   //设置系统时钟 
   unsigned int i;
  WDTCTL = WDTPW + WDTHOLD;      //   停止看门狗
  BCSCTL1 &= ~XT2OFF;            //   XT2振荡器开启
  _BIC_SR(OSCOFF);              //  复位OSCOFF
  do
  {IFG1 &= ~OFIFG;
  for(i=0xFF;i>0;i--);          
  } 
  while((IFG1 & OFIFG)!= 0) ;    //清除晶振失效中断标志位
  BCSCTL2 = SELM1 + SELS;        //设置MCLK=SMCLK=XT2
  
  Port_Init
  DsInit();


}
void main(void)
{ 
  char Set_time_Buffer[6]= {0x07,0x10,0x31,0x23,0x59,0x50};//年,月,日,时,分,秒
  char Get_time_Buffer[6]= {0x88,0x88,0x88,0x88,0x88,0x88};
   init();
   SetTimer(Set_time_Buffer);
   for(;;)
   {
   GetTimer(Get_time_Buffer);
   year0 = Get_time_Buffer[0]& 0x0F;
   year1 = Get_time_Buffer[0]>>4;
   month0 = Get_time_Buffer[1]& 0x0F;
   month1 = (Get_time_Buffer[1]>>4) & 0x01;
   date0 = Get_time_Buffer[2]& 0x0F;
   date1 = (Get_time_Buffer[2]>>4)& 0x03;
   hour0 = Get_time_Buffer[3]& 0x0F;
   hour1 = (Get_time_Buffer[3]>>4)& 0x03;
   minute0 = Get_time_Buffer[4]& 0x0F;
   minute1 = (Get_time_Buffer[4]>>4)& 0x07;
   second0 = Get_time_Buffer[5]& 0x0F;
   second1 = (Get_time_Buffer[5]>>4)& 0x07;
   _NOP();
   }
   



}






⌨️ 快捷键说明

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