HT1382时钟芯片代码

#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

猜你喜欢

转载自blog.csdn.net/return_oops/article/details/80966139