首页 > 代码库 > 硬件时钟--ht1381时钟芯片驱动

硬件时钟--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 = http://www.mamicode.com/Read_HT1380_1Byte();	//写1Byte数据>