PCF8563 的头文件做好了!感谢hanwl提供资料!
拿出来和大家共享一下!只需修改前面的宏定义就可以了!
只需调用GetPCF8563();函数就可以了,出口参数:年月日时分秒了,呵呵!
/************************************************************************
//ICC-AVR application builder : 2006-8-10 下午 02:12:10 // Target : M16
// Crystal: 8Mhz
时钟芯片PCF8563头文件
************************************************************************/
#define SDA_SET PORTC|=BIT(1); //时钟芯片数据接口PC1
#define SDA_CLR PORTC&=~BIT(1);
#define CLK_SET PORTC|=BIT(0); //时钟芯片时钟接口PC0
#define CLK_CLR PORTC&=~BIT(0);
#define SDA_DI_SET DDRC|=BIT(1);
#define SDA_DI_CLR DDRC&=~BIT(1);
#define CLK_DI_SET DDRC|=BIT(0);
#define CLK_DI_CLR DDRC&=~BIT(0);
#define CHECK_SDA PINC&BIT(1)
unsigned char miao,fen,shi,tian,yue,nian;
unsigned char old_sec,new_sec;
void I2C_Start (void)
{
SDA_DI_SET;
CLK_DI_SET;
CLK_SET;
SDA_SET;
delay_nus(2);
SDA_CLR;
delay_nus(2);
SDA_SET;
}
void I2C_Stop (void)
{
SDA_DI_SET;
CLK_DI_SET;
SDA_CLR;
CLK_SET;
delay_nus(2);
SDA_SET;
delay_nus(2);
}
unsigned char I2C_Ackn(void)
{
unsigned char errtime=255;
CLK_DI_SET;
SDA_DI_CLR;
delay_nus(2);
while(CHECK_SDA)
{
errtime--;
if (!errtime)
{
I2C_Stop();
return 0x00;
}
}
CLK_SET;
delay_nus(2);
CLK_CLR;
delay_nus(2);
return 0x01;
}
void Write_I2C_Byte(unsigned char byte)
{
unsigned char i;
SDA_DI_SET;
CLK_DI_SET;
for (i = 0; i < 8; i++)
{
CLK_CLR;
if((byte & 0x80))
{
SDA_SET;
}
else
{
SDA_CLR;
}
delay_nus(2);
CLK_SET;
asm("nop");
byte = byte << 1;
}
CLK_CLR;
if (I2C_Ackn()==0) delay_nus(5);
CLK_CLR;
}
unsigned char Read_I2C_Byte(void)
{
unsigned char i,buff = 0;
delay_nus(2);
CLK_DI_SET;;
SDA_DI_CLR;
for (i = 0; i < 8; i++)
{
CLK_CLR;
delay_nus(2);
CLK_SET;
delay_nus(2);
if ( CHECK_SDA) buff++;
buff = (buff << 1);
delay_nus(2);
}
SDA_DI_SET;
CLK_DI_SET;
SDA_CLR;
delay_nus(2);
CLK_SET;
delay_nus(2);
CLK_CLR;
return buff;
}
unsigned char rtc_read(unsigned char address)
{
unsigned char d;
I2C_Start();
Write_I2C_Byte(0xa2);
Write_I2C_Byte(address);
I2C_Start();
Write_I2C_Byte(0xa3);
d=Read_I2C_Byte();
d=d>>1;
I2C_Stop();
return d;
}
void rtc_write(unsigned char address,unsigned char data1)
{
I2C_Start();
Write_I2C_Byte(0xa2);
Write_I2C_Byte(address);
Write_I2C_Byte(data1);
I2C_Stop();
}
void rtc_start(void)
{
rtc_write(0,0);
}
void rtc_stop(void)
{
rtc_write(0,0x20);
}
void SetPCF8563(unsigned char adds,unsigned char data)
{
CLI();
rtc_stop();
rtc_write(adds,data);
rtc_start();
SEI();
}
void set8563(void) //设置时间!
{ //这里就自己弄吧!我就偷懒了,呵呵!
SetPCF8563(8,0x06); //设置年
SetPCF8563(7,0x08); //设置月
SetPCF8563(5,0x09); //设置日
SetPCF8563(4,0x23); //设置时
SetPCF8563(3,0x59); //设置分
SetPCF8563(2,0x49); //设置秒
}
void GetPCF8563() //只需调用这个函数就可以了!(设置时间除外哦!)
{
CLI(); // 出口参数就是这些了
miao=(rtc_read(2)&0x7f); //秒
fen=(rtc_read(3)&0x7f); //分
shi=(rtc_read(4)&0x3f); //时
tian=(rtc_read(5)&0x3f);
yue=(rtc_read(7)&0x1f);
nian=(rtc_read(8)); //年
SEI();
}
|