码迷,mamicode.com
首页 > 其他好文 > 详细

硬件时钟--ht1381时钟芯片驱动

时间:2014-08-21 19:28:35      阅读:652      评论:0      收藏:0      [点我收藏+]

标签:ht1381   硬件时钟   单片机时钟   系统时钟   

ht1381时钟芯片驱动



#include "global.h"

//时钟芯片ht1381各引脚定义
#define HT1381_CS_GPIO_PORT            GPIOD
#define HT1381_CS_GPIO_PIN             GPIO_PIN_3
#define HT1381CsHigh()                 (HT1381_CS_GPIO_PORT->ODR |= (u8)(HT1381_CS_GPIO_PIN))
#define HT1381CsLow()                  (HT1381_CS_GPIO_PORT->ODR &= (u8)(~HT1381_CS_GPIO_PIN))

#define HT1381_DATA_GPIO_PORT          GPIOD
#define HT1381_DATA_GPIO_PIN           GPIO_PIN_2
#define HT1381DataHigh()               (HT1381_DATA_GPIO_PORT->ODR |= (u8)(HT1381_DATA_GPIO_PIN))
#define HT1381DataLow()                (HT1381_DATA_GPIO_PORT->ODR &= (u8)(~HT1381_DATA_GPIO_PIN))
#define HT1381DataOut()                GPIO_Init(HT1381_DATA_GPIO_PORT,HT1381_DATA_GPIO_PIN,GPIO_MODE_OUT_PP_HIGH_FAST); 
#define HT1381DataIn()                 GPIO_Init(HT1381_DATA_GPIO_PORT,HT1381_DATA_GPIO_PIN,GPIO_MODE_IN_PU_NO_IT);
#define HT1381DataStatus               (HT1381_DATA_GPIO_PORT->IDR & HT1381_DATA_GPIO_PIN)

#define HT1381_CLK_GPIO_PORT           GPIOD
#define HT1381_CLK_GPIO_PIN            GPIO_PIN_0
#define HT1381ClkHigh()                (HT1381_CLK_GPIO_PORT->ODR |= (u8)(HT1381_CLK_GPIO_PIN))
#define HT1381ClkLow()                 (HT1381_CLK_GPIO_PORT->ODR &= (u8)(~HT1381_CLK_GPIO_PIN))

extern void Int_HT1381(void);

ICTimerTab_t	ICTimerTab;
ICTimerTab_t	ICTimerTabBuf;

void Delay_us(u8 data)
{
   for(u8 i = 0; i <data;i++)
   {
     nop();
   }
}
/********************************************************************
往HT1381写入1Byte数据
********************************************************************/
void Send_HT1380_1Byte(unsigned char tData)
{
   unsigned char i;
   HT1381DataOut();
   for(i=0;i<8;i++)
   {
      if((tData&0x01) != 0)
      {
         HT1381DataHigh();
      }
      else
      {
         HT1381DataLow();
      }
      tData >>= 1;
      Delay_us(8);
      HT1381ClkHigh();
      Delay_us(5);
      HT1381ClkLow();
      Delay_us(5);
   }
}
/********************************************************************
往HT1381读取1Byte数据
********************************************************************/
unsigned char Read_HT1380_1Byte(void)
{
   unsigned char i,tData;
   HT1381DataIn();
   for(i=0;i<8;i++)
   {
      HT1381ClkHigh();
      Delay_us(8);
      tData >>= 1;
      if(!HT1381DataStatus)
      {
         tData &= 0x7f;
      }
      else
      {
         tData |= 0x80;
      }
      HT1381ClkLow();
      Delay_us(5);
   }
   HT1381DataOut();
   return tData;
}
/********************************************************************
* 名称: 往HT1381写入数据
* 说明: 先写地址,后写命令/数据
* 调用: Send_HT1380_1Byte
* 输入: sAddr:HT1381地址,sData:要写的数据
********************************************************************/
void Write_HT1381Addr_1Byte(unsigned char sAddr,unsigned char sData)
{
   HT1381CsLow();
   Delay_us(5);
   HT1381ClkLow();
   Delay_us(5);
   HT1381CsHigh();
   Send_HT1380_1Byte(sAddr);	//写地址,命令
   Send_HT1380_1Byte(sData);	//写1Byte数据
   HT1381ClkHigh();
   Delay_us(5);
   HT1381CsLow();
}
/********************************************************************
* 名称: 读取HT1381某地址的数据
* 说明: 先写地址,后写命令/数据
* 调用: Send_HT1380_1Byte,Read_HT1380_1Byte
* 输入: sAddr:HT1381地址
* 返回值: sData:读取的数据
********************************************************************/
unsigned char Read_HT1381Addr_1Byte(unsigned char sAddr)
{
   unsigned char tData;
   HT1381CsLow();
   Delay_us(5);
   HT1381ClkLow();
   Delay_us(5);
   HT1381CsHigh();
   Send_HT1380_1Byte(sAddr);	//写地址,命令
   tData = Read_HT1380_1Byte();	//写1Byte数据
   HT1381ClkHigh();
   Delay_us(5);
   HT1381CsLow();
   return tData;
}
/********************************************************************
* 名称: 读取HT1381当前时间
* 调用: Read_HT1381Addr_1Byte
* 输入: 保存当前时间地址+当前时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
********************************************************************/
void Read_HT1381_NowTimer(void)
{
   unsigned char i;
   unsigned char sAddr;
   unsigned char *p;
   sAddr = 0x81;
   p =(unsigned char*)&ICTimerTabBuf;
   for(i=0;i<7;i++)
   {
      *p = Read_HT1381Addr_1Byte(sAddr);
      p++;
      sAddr += 2;
   }
}
/********************************************************************
* 名称: 初始化HT1381
* 调用: Write_HT1381Addr_1Byte
* 输入: 初始时间地址+初始时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
********************************************************************/
void Int_HT1381(void)
{
   unsigned char i;
   unsigned char sAddr;
   unsigned char *p;
   sAddr = 0x80;
   p =(unsigned char*)&ICTimerTabBuf;
   Write_HT1381Addr_1Byte(0x8e,0x00);		//控制命令,WP=0,写操作?
   for(i=0;i<7;i++)
   {
      Write_HT1381Addr_1Byte(sAddr,*p);	//秒,分,时,日,月,星期,年
      p++;
      sAddr += 2;
   }
   Write_HT1381Addr_1Byte(0x8e,0x80);		//控制命令,WP=1,写保护?
}
/********************************************************************
上电初始化日期时间
********************************************************************/
void Int_Timer(void)
{	//上电读取时钟芯片是否掉电
   Read_HT1381_NowTimer();
   if((ICTimerTabBuf.TimerSec&0x80) != 0)
   {
      Read_HT1381_NowTimer();
      if((ICTimerTabBuf.TimerSec&0x80) != 0)
      {
         ICTimerTabBuf.TimerSec = 0x20;
         ICTimerTabBuf.TimerMin = 0x57;
         ICTimerTabBuf.TimerHour = 0x18;
         ICTimerTabBuf.TimerDay = 0x11;
         ICTimerTabBuf.TimerMonth = 0x07;
         ICTimerTabBuf.TimerWeek = 0x04;
         ICTimerTabBuf.TimerYear = 0x13;
         Int_HT1381();		//初始化HT1381
      }
   }
   ICTimerTab.TimerSec = (ICTimerTabBuf.TimerSec/16) * 10 +  (ICTimerTabBuf.TimerSec%16);
   ICTimerTab.TimerMin = (ICTimerTabBuf.TimerMin/16) * 10 +  (ICTimerTabBuf.TimerMin%16);
   ICTimerTab.TimerHour = (ICTimerTabBuf.TimerHour/16) * 10 +  (ICTimerTabBuf.TimerHour%16);
   ICTimerTab.TimerDay = (ICTimerTabBuf.TimerDay/16) * 10 +  (ICTimerTabBuf.TimerDay%16);
   ICTimerTab.TimerMonth = (ICTimerTabBuf.TimerMonth/16) * 10 +  (ICTimerTabBuf.TimerMonth%16);
   ICTimerTab.TimerWeek = (ICTimerTabBuf.TimerWeek/16) * 10 +  (ICTimerTabBuf.TimerWeek%16);
   ICTimerTab.TimerYear = (ICTimerTabBuf.TimerYear/16) * 10 +  (ICTimerTabBuf.TimerYear%16);
}


硬件时钟--ht1381时钟芯片驱动,布布扣,bubuko.com

硬件时钟--ht1381时钟芯片驱动

标签:ht1381   硬件时钟   单片机时钟   系统时钟   

原文地址:http://blog.csdn.net/a656343072/article/details/38733907

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!