HT1382時鐘晶片程式碼
阿新 • • 發佈:2018-11-06
#include "HT1382.h" #include "IIC.h" ////////////////////////////////////////////////////////////////////////////////// /* 01H 秒 02H 分 03H 時 04H 日 05H 月 06H 星期 07H 年 Address Table MSB LSB 1 1 0 1 0 0 0 0/1 =0xD0(W)/0xD1(R) */ ///////////////////////////////////////////////////////////////////////////////// Sys_Clock BCD_Time; Sys_Clock RTC_Time; void RTC_BCD2DEC(void) //將時間的BCD碼轉換成十進位制 讀時間 { RTC_Time.Year=(BCD_Time.Year>>4)*10+(BCD_Time.Year&0x0F); RTC_Time.Month=(BCD_Time.Month>>4)*10+(BCD_Time.Month&0x0F); RTC_Time.Date=(BCD_Time.Date>>4)*10+(BCD_Time.Date&0x0F); RTC_Time.Hour=((BCD_Time.Hour>>4)&0x07)*10+(BCD_Time.Hour&0x0F); RTC_Time.Minute=(BCD_Time.Minute>>4)*10+(BCD_Time.Minute&0x0F); RTC_Time.Second=((BCD_Time.Second>>4)&0x07)*10+(BCD_Time.Second&0x0F); } void RTC_DEC2BCD(void) //將時間的十進位制轉換成BCD碼 設時間 { RTC_Time.Year=18; RTC_Time.Month=7; RTC_Time.Date=7; RTC_Time.Hour=13; RTC_Time.Minute=33; RTC_Time.Second=0; BCD_Time.Year=((RTC_Time.Year/10)<<4)+(RTC_Time.Year%10); BCD_Time.Month=((RTC_Time.Month/10)<<4)+(RTC_Time.Month%10); BCD_Time.Date=((RTC_Time.Date/10)<<4)+(RTC_Time.Date%10); BCD_Time.Hour=(((RTC_Time.Hour/10)<<4)|0x80)+(RTC_Time.Hour%10); //24小時計時 BCD_Time.Minute=((RTC_Time.Minute/10)<<4)+(RTC_Time.Minute%10); BCD_Time.Second=((RTC_Time.Second/10)<<4)+(RTC_Time.Second%10); } /********************************* 函式名稱:時間設定 傳遞引數: 返回引數: 說 明:設定時間前需對RTC結構體賦值 ********************************/ void SetTime(void) { RTC_DEC2BCD(); I2C_WriteByte(HT1382_WP_Reg ,0x00,HT1382_ADDR); //關閉防寫 I2C_WriteByte(HT1382_SEC_Reg ,0x80,HT1382_ADDR); //暫停 時鐘晶振 I2C_WriteByte(HT1382_YEAR_Reg ,BCD_Time.Year,HT1382_ADDR); //年 I2C_WriteByte(HT1382_MONTH_Reg,BCD_Time.Month,HT1382_ADDR); //月 I2C_WriteByte(HT1382_DATE_Reg ,BCD_Time.Date,HT1382_ADDR); //日 I2C_WriteByte(HT1382_HR_Reg ,BCD_Time.Hour,HT1382_ADDR); //時 I2C_WriteByte(HT1382_MIN_Reg ,BCD_Time.Minute,HT1382_ADDR); //分 I2C_WriteByte(HT1382_SEC_Reg ,BCD_Time.Second,HT1382_ADDR); //秒 I2C_WriteByte(HT1382_SEC_Reg ,0x00,HT1382_ADDR); //開晶振! I2C_WriteByte(HT1382_WP_Reg ,0x80,HT1382_ADDR); //開啟防寫 } /********************************* 函式名稱:讀時間 傳遞引數: 返回引數: 說 明: ********************************/ void ReadTime(void) { BCD_Time.Year = I2C_ReadByte(HT1382_YEAR_Reg,HT1382_ADDR,1); //年 BCD_Time.Month =0x1F & I2C_ReadByte(HT1382_MONTH_Reg,HT1382_ADDR,1); //月 BCD_Time.Date =0x3F & I2C_ReadByte(HT1382_DATE_Reg,HT1382_ADDR,1); //日 BCD_Time.Hour =0x3F & I2C_ReadByte(HT1382_HR_Reg,HT1382_ADDR,1); //時 BCD_Time.Minute =0x7F & I2C_ReadByte(HT1382_MIN_Reg,HT1382_ADDR,1); //分 BCD_Time.Second =0x7F & I2C_ReadByte(HT1382_SEC_Reg,HT1382_ADDR,1); //秒 RTC_BCD2DEC(); } //HT1382初始化 void HT1382_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//外部中斷,需要使能AFIO時鐘 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //1Hz-->PC0 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入 IPU // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStructure); // GPIO_SetBits(GPIOC,GPIO_Pin_0); GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0); /* PC0 as 1Hz input */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能1Hz所在的外部中斷通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //搶佔優先順序2 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中斷通道 NVIC_Init(&NVIC_InitStructure); //根據NVIC_InitStruct中指定的引數初始化外設NVIC暫存器 I2C_WriteByte(HT1382_WP_Reg ,0x00,HT1382_ADDR); //關閉防寫 I2C_WriteByte(HT1382_CFG_Reg,0x1A,HT1382_ADDR); //1HZ輸出 I2C_WriteByte(HT1382_WP_Reg ,0x80,HT1382_ADDR); //開啟防寫 ReadTime(); } void EXTI0_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line0) != RESET) //PC0上的1Hz { //ReadTime(); EXTI_ClearITPendingBit(EXTI_Line0); } }
標頭檔案
#ifndef __HT1382_H #define __HT1382_H #include "sys.h" //HT1382_CFG_Reg // D7 D6 D5 D4 D3 D2 D1 D0 // IME AE LPM OEOBM FO3 FO2 FO1 FO0 //IME 中斷模式使能位 0:Disable 1:Enable //AE 鬧鈴使能 0:Disable 1:Enable //LPM 低功耗模式使能位 0:Disable 1:Enable //OEOBM 電池模式下使能方波輸出 0:Enable 1:Disable //FO3 FO2 FO1 FO0 // 1 0 1 0 1Hz輸出 =0x0A #define HT1382_ADDR 0xD0 #define HT1382_WP_Reg 0x07 #define HT1382_SEC_Reg 0x00 #define HT1382_MIN_Reg 0x01 #define HT1382_HR_Reg 0x02 #define HT1382_DATE_Reg 0x03 #define HT1382_MONTH_Reg 0x04 #define HT1382_YEAR_Reg 0x06 #define HT1382_CFG_Reg 0x09 void HT1382_Init(void); void SetTime(void); void ReadTime(void); typedef struct{ u8 Year; u8 Month; u8 Date; u8 Hour; u8 Minute; u8 Second; }Sys_Clock; extern Sys_Clock BCD_Time; extern Sys_Clock RTC_Time; #endif