📄 pcf8563.c
字号:
/*****************************************************
This program was produced by the
ICC7.14 Professional
Project :SHS07-I
Version : Vt-1.0.01
Date : 2008-5-29
Author : 应志强
Company : DUCHY
Comments: 测试ok
//完成实时时钟的读取和配置
***************************************************************************************************/
#include "head.h"
/*************************************************
* 读某个寄存器的值
*************************************************/
unsigned char rtc_read(unsigned char address)
{
unsigned char d;
/*
#ifdef Check
start_R:
I2C_start();
if(!(I2C_Send_Byte(0xa2)))
goto start_R;
if(!(I2C_Send_Byte(address)))
goto start_R;
I2C_start();
if(!(I2C_Send_Byte(0xa3)))
goto start_R;
#else
*/
I2C_start();
I2C_Send_Byte(PCF865_W_ADDR);
I2C_Send_Byte(address);
I2C_start();
I2C_Send_Byte(PCF865_R_ADDR);
//#endif
d=I2C_Receive_Byte();
I2C_stop();
//Tx_char(d);
return d;
}
/*************************************************
* 写某个寄存器的值
*************************************************/
void rtc_write(unsigned char address,unsigned char data)
{
/*
#ifdef Check
start_W:
I2C_start();
if(!(I2C_Send_Byte(0xa2)))
goto start_W;
if(!(I2C_Send_Byte(address)))
goto start_W;
if(!(I2C_Send_Byte(data)))
goto start_W;
#else
*/
I2C_start();
I2C_Send_Byte(PCF865_W_ADDR);
I2C_Send_Byte(address);
I2C_Send_Byte(data);
// #endif
I2C_stop();
}
/*************************************************
* 时钟停止
*************************************************/
void rtc_stop(void)
{
rtc_write(state1,stop_time);//
}
/*************************************************
* 时钟运行
*************************************************/
void rtc_start(void)
{
rtc_write(state1,start_time);//
}
#if 1
/*************************************************
* 读取当前时间
*************************************************/
void Get_Time(void)
{
unsigned char i;
rtc_stop();
rtc_start();
i= rtc_read(2);
#if 0
while(i&0x80)
{
rtc_stop();
delay_mS(300);
rtc_start();
delay_mS(300);
i= rtc_read(2);
}
#endif
i=bcd2bin(i&0x7F);
if(i<60)
{
Rel_time[0]=2*i;
}
rtc_stop();
rtc_start();
i= rtc_read(3);
i=bcd2bin(i&0x7F);
if(i<60)
{
Rel_time[1]=i;
}
rtc_stop();
rtc_start();
i= rtc_read(4);
i=bcd2bin(i&0x3F);
if(i<24)
{
Rel_time[2]=i;
}
rtc_stop();
rtc_start();
i= rtc_read(5);
i=bcd2bin(i&0x3F);
if(i<GetMonthDay(Rel_time[2]))
{
Rel_time[3]=i;
}
rtc_stop();
rtc_start();
i= rtc_read(7);
if(i&0x80)
{
Rel_time[6]=20;
}
else
{
Rel_time[6]=20;
}
i=bcd2bin(i&0x1F);
if(i<13)
{
Rel_time[4]=i;
}
rtc_stop();
rtc_start();
delay_mS(100);
i= rtc_read(8);
i=bcd2bin(i);
if(i<100)
{
Rel_time[5]=bcd2bin(i);
}
rtc_stop();
rtc_start();
i= rtc_read(6);
i=i&0x07;
if(i<7)
{
Rel_time[8]=i;
}
}
/*************************************************
* 时钟初始化
*************************************************/
void rtc_init(void)
{
Rtc_Star:
init_I2C();
delay_mS(10);
rtc_stop();
rtc_write(0x0d,0x83);//输出秒脉冲
rtc_start();
delay_mS(10);
#if 1
if(rtc_read(0x00)&0xA0)
{
goto Rtc_Star;
}
#endif
rtc_stop();
rtc_write(0x0d,0x83);//输出秒脉冲
rtc_start();
delay_mS(10);
Get_Time();
}
/*************************************************
* 保存当前时间
*************************************************/
void Rtc_Save(void)
{
unsigned char i=20;
rtc_stop();
rtc_write(6, (Rel_time[8]));
delay_mS(i);
//rtc_write(2, Rel_time[0]);
delay_mS(i);
rtc_write(3, H_to_D(Rel_time[1]));
delay_mS(i);
rtc_write(4, H_to_D(Rel_time[2]));
delay_mS(i);
rtc_write(5, H_to_D(Rel_time[3]));
delay_mS(i);
rtc_write(7, H_to_D(Rel_time[4]));//
rtc_start();
delay_mS(3*i);
rtc_stop();
rtc_write(8, H_to_D(Rel_time[5]));
rtc_start();
}
/*************************************************
* 时钟读取
*************************************************/
#if 0
unsigned char rtc_read_bcd(unsigned char addr,unsigned char mask)
{
unsigned char i;
i=rtc_read(addr) ;
Tx_char(i);
i=bcd2bin(i & mask);
Tx_char(i);
return i;
}
void rtc_write_bcd(unsigned char addr,unsigned char data)
{
rtc_write(addr,H_to_D(data));
Tx_char(H_to_D(data));
}
unsigned char rtc_get_time(unsigned char *hour,unsigned char *min,unsigned char *sec)
{
unsigned char d;
d=rtc_read(2);
*sec=bcd2bin(d & 0x7f);
(d & 0x80) ? (d=0): ( d=1);
*min=rtc_read_bcd(3,0x7f);
*hour=rtc_read_bcd(4,0x3f);
return d;
}
/*
void rtc_set_time(unsigned char hour,unsigned char min,unsigned char sec)
{
rtc_stop();
rtc_write_bcd(0x02,sec);
rtc_write_bcd(0x03,min);
rtc_write_bcd(0x04,hour);
rtc_start();
}
*/
unsigned char rtc_get_date(unsigned char *date,unsigned char *month,unsigned char *year)
{
unsigned char mc;
*date=rtc_read_bcd(5,0x3f);
mc=rtc_read(7);
*month=bcd2bin(mc & 0x1f);
(mc & 0x80) ? (mc = 1): ( mc =0);
*year=rtc_read_bcd(8,0xff);
return mc;
}
void rtc_set_date(unsigned char date,unsigned char month,unsigned int year)
{
unsigned char mc;
mc=H_to_D(month);
if(year>=2000)
{
mc|=0x80;
}
rtc_stop();
rtc_write_bcd(5,date);
rtc_stop();
rtc_write(7,mc);
rtc_stop();
rtc_write_bcd(8,year%100);
rtc_start();
}
void rtc_alarm_off(void)
{
rtc_write(9,rtc_read(9) | 0x80);
rtc_write(10,rtc_read(10) | 0x80);
rtc_write(11,rtc_read(11) | 0x80);
rtc_write(12,0x80);
}
void rtc_alarm_on(void)
{
unsigned char d;
rtc_write(9,rtc_read(9) & 0x7f);
rtc_write(10,rtc_read(10) & 0x3f);
d=rtc_read(11) & 0x3f;
if (d==0) d|=0x80;
rtc_write(11,d);
}
void rtc_write_alarm(unsigned char addr,unsigned char data)
{
rtc_write(addr,H_to_D(data) | 0x80);
}
void rtc_set_alarm(unsigned char date,unsigned char hour,unsigned char min)
{
rtc_write_alarm(9,min);
rtc_write_alarm(10,hour);
rtc_write_alarm(11,date);
rtc_write(12,0x80);
}
void rtc_get_alarm(unsigned char *date,unsigned char *hour,unsigned char *min)
{
*min=rtc_read_bcd(9,0x7f);
*hour=rtc_read_bcd(10,0x3f);
*date=rtc_read_bcd(11,0x3f);
}
void rtc_set_timer(unsigned char val)
{
rtc_write(0xf,val);
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -