PCF8563实时时钟模块功能实现

1.概述

PCF8563是为低功耗而优化的CMOS实时时钟/日历。提供了可编程时钟输出、中断输出和低电压检测器。所有地址和数据都是通过两路双向IIC总线串行传输的。最大总线速度为400kbits/s。内置的字地址寄存器在每个写入或读取数据字节后自动递增。

2.特点

■ Provides year, month, day, weekday, hours, minutes and seconds based on 32.768 kHz quartz crystal
■ Century flag
■ Wide operating supply voltage range: 1.0 to 5.5 V
■ Low back-up current; typical 0.25 µA at VDD = 3.0 V and Tamb = 25 °C ■ 400 kHz two-wire I2C-bus interface (at VDD = 1.8 to 5.5 V)
■ Programmable clock output for peripheral devices: 32.768 kHz, 1024 Hz,32 Hz and 1 Hz
■ Alarm and timer functions
■ Voltage-low detector
■ Integrated oscillator capacitor
■ Internal power-on reset
■ I2C-bus slave address: read A3H; write A2H
■ Open drain interrupt pin.

3.硬件描述

电路结构框图
PCF8563实时时钟模块功能实现_第1张图片
引脚排列顺序

PCF8563实时时钟模块功能实现_第2张图片

4.软件实现

1.启动/关闭闹钟

/*****************************************************************************
***@name:启动、关闭时钟    
***@input:0:关闭时钟;1:启动时钟
*****************************************************************************/
void start_stop(uint8_t status)
{
	if(status == 0)
	{
	    iic_send_add_byte(0x00,0x20);//关闭时钟
	}
	else if(status == 1)
	{
		iic_send_add_byte(0x00,0x00);//开启时钟
	}
}

2.BCD/十进制相互转换

/******************BCD转十进制***************************/
uint8_t bcd_dec(uint8_t bat)
{
	uint8_t temp1,temp2,tol;
	temp1=bat&0x0f;
	temp2=(bat&0xf0)>>4;
	tol=temp2*10+temp1;
	return tol; 
}
/******************十进制转BCD***************************/
uint8_t dec_bcd(uint8_t bat)
{
	return ((bat%10) & 0x0F) | (((bat/10) << 4) & 0xF0);; 
}
/***

3.初始化当前时间

void time_init(uint8_t year,uint8_t moom,uint8_t dat,uint8_t hour,uint8_t min,uint8_t sec)
{
	sec = dec_bcd(sec);
	min = dec_bcd(min);
	hour = dec_bcd(hour);
	dat = dec_bcd(dat);
	moom = dec_bcd(moom);
	year = dec_bcd(year);
	
     iic_send_add_byte(0x02,sec);         
     iic_send_add_byte(0x03,min);          
     iic_send_add_byte(0x04,hour);                    
     iic_send_add_byte(0x05,dat);         
     iic_send_add_byte(0x07,moom);         
     iic_send_add_byte(0x08,year);         
}

4.获取当前时间并转换

void get_time(void)
{
		 date.sec=0x7f&iic_rec_add_byte(0x02);    //读取秒
         date.min=0x7f&iic_rec_add_byte(0x03);   //读取分钟
         date.hour=0x3f&iic_rec_add_byte(0x04);  //读取小时
         date.dat=0x3f&iic_rec_add_byte(0x05);   //读取天数
         date.moom=0x1f&iic_rec_add_byte(0x07);  //读取月
         date.year=0xff&iic_rec_add_byte(0x08);    //读取年
         date.sec1=bcd_dec(date.sec);                   //将读取的BCD码秒转换成十进制秒以便运算
         date.min1=bcd_dec(date.min);
         date.hour1=bcd_dec(date.hour);
         date.dat1=bcd_dec(date.dat);
         date.moom1=bcd_dec(date.moom);
         date.year1=bcd_dec(date.year);
		 //printf("20%d年%d月%d日%d时%d分%d秒\r\n",date.year1,date.moom1,date.dat1,date.hour1,date.min1,date.sec1);	
}

5.设置闹钟

void P8563_alarm(uint8_t day,uint8_t hour,uint8_t min)
{
		day = dec_bcd(day);
		hour = dec_bcd(hour);
		min = dec_bcd(min);	
	    iic_send_add_byte(0xc,0x1);   //星期报警
	    
	    iic_send_add_byte(0xb,day); /*日期报警*/	
        iic_send_add_byte(0xa,hour); /*小时报警*/	
	    iic_send_add_byte(0x9,min); /*分钟报警*/
	
     	iic_send_add_byte(0x01,0x12); /*报警有效*/ //使能报警器和定时器发出中断请求
 
}

6.清除闹钟中断

/******************清除中断拉高电平***************************/
void clear_alarm(void)
{
    iic_send_add_byte(0x01,0x02);//清中断标志
}
/******************关闭闹钟中断***************************/
void close_alarm(void)
{
	iic_send_add_byte(0x01,0x00);//禁止中断
}

5.总结

该模块通过IIC与单片机进行通信,单片机只需往对应的寄存器发送数据即可实现所需要的功能,适合任意一款具备IIC通信外设的8位/32位单片机。

你可能感兴趣的:(单片机,stm32,arm,传感器,51单片机)