📄 pcf8563.c
字号:
/*
Name: ***write_pcf8563***read_pcf8563***
Author:zhouxianfa.
Date:2004-7-16.
Model_write:start+SLA_W+address+(data0...dataN)+stop.
Model_read:start+SLA_W+address+repeat_start+SLA_R+(RECEIVE data0...dataN)+stop.
Known:SLA_W=0a2h,address=写入PCF8563数据串的起始地址(读取数据的起始地址).
SLA_W是所选器件的系统地址,SLA_R=SLA_W+1.
ACK在发送数据时不理会,系统自处理.
write_pcf8563时,ACK信号不必理会.
read_pcf8563 时,在收到ST的数据后,前n-1个数据发送ACK(TWEA=1),n数据发送NACK(TWEA=0).
For PCF8563:
00h:控制/状态寄存器1. bit7=test,bit5=stop,bit3=testC.
01h:控制/状态寄存器2. bit4=Ti/Tp,bit3=AF,bit2=TF,bit1=Aie,bit0=Tie.
02h:second. VL+second_value.
03h:minute. (bit6...bit0)=minute_value.
04h:hour. (bit6...bit0)=hour_value.
05h:data. (bit6...bit0)=data_value.
06h:day. (bit2...bit0)=day_value.
07h:month/century. bit7=C,(bit4...bit0)=(01~12)BCD
08h:year. (bit7...bit0)=year_value.
09h:warring_minute. bit7=AE,(bit6...bit0)=warring_value.
0ah:warring_hour. bit7=AE,(bit5...bit0)=warring_value.
0bh:warring_data. bit7=AE,(bit5...bit0)=warring_value.
0ch:warring_day. bit7=AE,bit2...bit0=warring_value.
0dh:CLKOUT输出寄存器. bit7=FE,bit1=FD1,bit0=FD0.
0eh:定时器控制寄存器. bit7=TE,bit1=TD1,bit0=TD0.
0fh:定时器倒数数值寄存器. 定时器倒计数数值(二进制).
(attention:The values are BCD.)
*/
//send (00h---08h),启动时钟|设定时间2004-7-Friday-16-14-23-15.
unsigned char PCF_register[16]={0,0,15,23,14,16,5,7,04};
void write_pcf8563(unsigned char address,unsigned char number)
{unsigned char i;
//START
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //发送START
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x08) goto error; //TWSR=0x08出错处理.
//SLA_W
TWDR=0xa2; //装入SLA_W.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x18) goto error; //TWSR=0x18出错处理.
//address
TWDR=address; //装入数据起始地址address.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x28) goto error; //TWSR=0x28出错处理.
//datas
for (i=0;i<number;i++)
{ TWDR=PCF_register[i]; //装入数据i.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x28) goto error; //TWSR=0x28出错处理.
}
//STOP
error: //如果接收的应答信号不对,立即释放总线.
TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN); //发送STOP.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
}
void read_pcf8563(unsigned char address,unsigned char number)
{unsigned char i;
//START
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //发送START
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x08) goto error; //TWSR=0x08出错处理.
//SLA_W
TWDR=0xa2; //装入SLA_W.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x18) goto error; //TWSR=0x18出错处理.
//address
TWDR=address; //装入数据起始地址address.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x28) goto error; //TWSR=0x28出错处理.
//START
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //发送重复START
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x10) goto error; //TWSR=0x10出错处理.
//SLA_W
TWDR=0xa2+1; //装入SLA_R,(setb SLA_W.0),进入读模式.
TWCR&=~(1<<7); //TWINT=0,启动发送.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
if ((TWSR&0xf8)==0x40) goto error; //TWSR=0x40出错处理.
//datas
TWCR &=(1<<TWEA); //前n-1个发送ACK
for (i=0;i<number-1;i++)
{ while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
PCF_register[i]=TWDR; //装入数据(0...n-1).
if ((TWSR&0xf8)==0x50) goto error; //TWSR=0x50出错处理.
TWCR=(1<<TWINT); //发送ACK.
}
TWCR &=(1<<TWEA); //最后一个发送NACK.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
PCF_register[i]=TWDR; //装入数据n.
if ((TWSR&0xf8)==0x50) goto error; //TWSR=0x50出错处理.
TWCR|=(1<<TWINT); //发送 NACK.
//STOP
error: //如果接收的应答信号不对,立即释放总线.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN); //发送STOP,End the process.
while (!(TWCR&(1<<7))) ; //wait_for_TWINT.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -