|
大家好啊,我用LPC1114写PCF8563,不知为何一直读不出时间来,代码呈上
********************************************************/
/*内部函数,PCF8563 开始*/
void Start(void)
{
SDA_ON; //SET
delay_us(2);
SCL_ON;
delay_us(2);
SDA_OFF;
delay_us(2);
SCL_OFF;
return;
}
/*内部函数,PCF8563 结束*/
void Stop(void)
{
SDA_OFF;
SCL_OFF;
delay_us(2);
SCL_ON;
delay_us(2);
SDA_ON;
delay_us(2);
return;
}
/*内部函数,输出ACK=0*/
void WriteACK(void)
{
SDA_OFF;
delay_us(2);
SCL_ON;
delay_us(2);
SCL_OFF;
return;
}
/*内部函数,输出ACK=1*/
void writenoack(void)
{
SDA_ON;
delay_us(2);
SCL_ON;
delay_us(2);
SCL_OFF;
return;
}
// GPIO0->DIR |=(1<<9); //P0.9 (P09 28) DS_DA OUTPUT PCF8563 SCLK
// GPIO0->DIR |=(1<<10); //P0.10(P010 29) DS_RST OUTPUT PCF8563 SDA
/*内部函数,等待ACK*/
void WaitACK(void)
{ unsigned char errtime=20;
SDA_ON;
delay_us(2); /*读ACK*/
SCL_ON;
delay_us(2);
GPIO0->DIR&=~(1<<10);
delay_us(2);
while((GPIO0->DATA&(1<<10))==(1<<10))
{
if(errtime>0) {errtime--;}
else{LED_SHOW=0X55;}
//delay_us(2);
//if(!errtime) Stop();
}
SCL_OFF;
GPIO0->DIR |=(1<<10);
delay_us(2);
return;
}
// GPIO0->DIR |=(1<<9); //P0.9 (P09 28) DS_DA OUTPUT PCF8563 SCLK
// GPIO0->DIR |=(1<<10); //P0.10(P010 29) DS_RST OUTPUT PCF8563 SDA
/**内部函数.输出数据字节 入口:B=数据*/
void writebyte(unsigned char wdata)
{
unsigned char i;
SCL_OFF;
for(i=8;i>0;i--)
{
if(wdata&0x80!=0) {SDA_ON;}
else {SDA_OFF;}
wdata<<=1;
SCL_ON;
delay_us(2);
SCL_OFF;
}
WaitACK();
return;
}
// GPIO0->DIR |=(1<<9); //P0.9 (P09 28) DS_DA OUTPUT PCF8563 SCLK
// GPIO0->DIR |=(1<<10); //P0.10(P010 29) DS_RST OUTPUT PCF8563 SDA
/*内部函数.输入数据 出口:B*/
unsigned char Readbyte(void)
{
unsigned char i,bytedata;
//SDA_ON;
GPIO0->DIR&=~(1<<10);
delay_us(2);
for(i=0;i<8;i++)
{
SCL_ON;
bytedata<<=1;
if((GPIO0->DATA&(1<<10))==(1<<10)) {bytedata|=0X01;}
SCL_OFF;
delay_us(2);
}
GPIO0->DIR |=(1<<10);
delay_us(2);
return(bytedata);
}
/**输出数据->pcf8563*/
void writeData(unsigned char address,unsigned char mdata)
{
Start();
writebyte(0xa2); /*写命令*/
writebyte(address); /*写地址*/
writebyte(mdata); /*写数据*/
Stop();
return;
}
/*输入数据<-pcf8563*/
unsigned char ReadData(unsigned char address) /*单字节*/
{
unsigned char rdata;
Start();
writebyte(0xa2); /*写命令*/
writebyte(address); /*写地址*/
Start();
writebyte(0xa3); /*读命令*/
rdata=Readbyte();
writenoack();
Stop();
return(rdata);
}
/*
void ReadData1(unsigned char address,unsigned char count,unsigned char *buff) //多字节
{
unsigned char i;
Start();
writebyte(0xa2); //写命令
writebyte(address); //写地址
Start();
writebyte(0xa3); //读命令
for(i=0;i<count;i++)
{
time_get[i]=Readbyte();
if(i<count-1) WriteACK();
}
writenoack();
Stop();
return;
}
*/
void ReadData1(unsigned char address) //多字节
{
unsigned char i;
Start();
writebyte(0xa2); //写命令
writebyte(0X02); //写地址
LED_SHOW=0X01;
Start();
writebyte(0xa3); //读命令
for(i=0;i<6;i++)
{
time[i]=Readbyte();
WriteACK();
}
time[6]=Readbyte();
writenoack();
Stop();
return;
}
/*内部函数,读入时间到内部缓冲区*/
void P8563_Read(void)
{
ReadData1(0x02); //02-08
time_buf[0]=time[6]>>4&0x0F; //08H 年
time_buf[1]=time[6]&0x0F;
//if(time_buf[0]==0) {time_buf[0]=20;}
time_buf[2]=time[5]>>4&0x01; //07H 月
time_buf[3]=time[5]&0x0F;
//if(time_buf[2]==0) {time_buf[2]=20;}
time_buf[4]=time[4]&0x07; //06H 星期
time_buf[5]=time[3]>>4&0x03; //05H 日
time_buf[6]=time[3]&0x0F;
//if(time_buf[5]==0) {time_buf[5]=20;}
time_buf[7]=time[2]>>4&0x03; //04H 时
time_buf[8]=time[2]&0x0F;
//if(time_buf[7]==0) {time_buf[7]=20;}
time_buf[9]=time[1]>>4&0x03; //03H 分
time_buf[10]=time[1]&0x0F;
//if(time_buf[9]==0) {time_buf[9]=20;}
time_buf[11]=time[0]>>4&0x07; //02H 秒
time_buf[12]=time[0]&0x0F;
//if(time_buf[11]==0) {time_buf[11]=20;}
/*
time_buf[0]=time[0]&0x7f; //秒
time_buf[1]=time[1]&0x7f; //分
time_buf[2]=time[2]&0x3f; //小时
time_buf[3]=time[4]&0x07; //星期
*/
return;
}
/**写时间修改值*/
void P8563_settime(void)
{
unsigned char i;
time_buf[0]=1;time_buf[1]=5; //08H 年 //2015 1 1-11:59:00 星期四
time_buf[2]=0;time_buf[3]=1; //07H 月
time_buf[4]=4; //06H 星期四
time_buf[5]=0;time_buf[6]=1; //05H 日
time_buf[7]=1;time_buf[8]=1; //04H 时
time_buf[9]=5;time_buf[10]=9; //03H 分
time_buf[11]=0;time_buf[12]=0; //02H 秒
i=time_buf[11]*10+time_buf[12]; writeData(2,i); //02H 秒
i=time_buf[9]*10+time_buf[10]; writeData(3,i); //03H 分
i=time_buf[7]*10+time_buf[8]; writeData(4,i); //04H 时
i=time_buf[5]*10+time_buf[6]; writeData(5,i); //05H 日
i=time_buf[4]; writeData(6,i); //06H 星期四
i=time_buf[2]*10+time_buf[3]; writeData(7,i); //07H 月
i=time_buf[0]*10+time_buf[1]; writeData(8,i); //08H 年
return;
}
//volatile unsigned char time_buf[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
/* 0十年 年 2 月 4星期 5 日 7 小时 9 分钟 11 秒*/
//writeData(ADDRESS,DATA);
//Time and date registers
//02H: VL_seconds (0-59)
//03H: Minutes (0-59)
//04H: Hours (0-23)
//05H: Days (1-31)
//06H: Weekdays (0-6)
//07H: Century_months (1-12)
//08H: Years (0-99)
/**P8563的初始化-----外部调用*/
void P8563_init(void)
{
//unsigned char i;
if((ReadData(0x0a)&0x3f)!=0x8) //检查是否第一次启动,是则初始化时间
{
P8563_settime();
}
//else
// {
// }
return;
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|