|
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define byte unsigned char
#define word unsigned int
#define bool bit
#define true 1
#define false 0
#define somenop();_nop_();_nop_();_nop_();_nop_(); //
uchar systemerror;
uchar BCD_hex(uchar x);
sbit SCL=P2^7;
sbit SDA=P2^6;
sbit lcden=P2^0;
sbit lcdrs=P2^1;
char miao,shi,fen,year,month,day,week;
uchar table2[]="0123456789";
//IIC總線開始
void delay1()//延時
{
;;;;
}
void delay(uint z) // 延時
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 寫命令操作
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) // 寫數據操作
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,char date) // 液晶顯示
{
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(table2[shi]);
write_date(table2[ge]);
}
void IICstart(void )
{
EA=0;
SDA=1;
SCL=1;
somenop();
SDA=0;
somenop();
SCL=0;
}
//IIC總線停止
void IICstop(void)
{
SCL=0;
SDA=0;
somenop();
SCL=1;
somenop();
SDA=1;
EA=1;
}
//IIC等待從器件接收方的應答
bool waitack(void)
{
uchar errtime=255; //因故障接收方無ACK,超時值為255
SDA=1;
somenop();
SCL=1;
somenop();
while(SDA)
{
errtime--;
if(!errtime)
{
IICstop();
systemerror=0x11;
return false;
}
}
SCL=0;
return true;
}
void sendack(void)
{
SDA=0;
somenop();
SCL=1;
somenop();
SCL=0;
}
void sendnotack(void)
{
SDA=1;
somenop();
SCL=1;
somenop();
SCL=0;
}
void IICsendbyte(byte ch)
{
uchar i=8;
while(i--)
{
SCL=0;
_nop_();
SDA=(bit)(ch & 0x80);
ch<<=1;
somenop();
SCL=1;
somenop();
}
SCL=0;
}
byte IICreceivebyte(void)
{
uchar i=8;
byte ddata=0;
SDA=1;
while(i--)
{
ddata<<=1;
SCL=0;
somenop();
SCL=1;
somenop();
ddata|=SDA;
}
SCL=0;
return ddata;
}
void getpcf8563(uchar firsttype,uchar count,uchar *buff)
{
uchar i;
IICstart();
IICsendbyte(0xa2);
waitack();
IICsendbyte(firsttype);
waitack();
IICstart();
IICstart();
IICsendbyte(0xa3);
waitack();
for(i=0;i
{
buff=IICreceivebyte();
if(i!=count-1)
sendack();
}
sendnotack();
IICstop();
}
void setpcf8563(uchar timetype,uchar value)
{
IICstart();
IICsendbyte(0xa2);
waitack();
IICsendbyte(timetype);
waitack();
IICsendbyte(value);
waitack();
IICstop();
}
void setalarm(uchar alarmtype,uchar count) //設置鬧鐘
{
setpcf8563(0x01,0x02);
setpcf8563(alarmtype,count);
}
void cleanalam(void) //清除鬧鐘
{
setpcf8563(0x01,0x00);
setpcf8563(0x09,0x80);
setpcf8563(0x0a,0x80);
setpcf8563(0x0b,0x80);
setpcf8563(0x0c,0x80);
}
uchar read1380(uchar command) //返回時間
{
uchar time;
getpcf8563(command,1,&time);
return time;
}
void write1380(uchar command,uchar time)
{
setpcf8563(command,time);
}
/* void time_display(uchar x0,uchar y0,bit type)
{
uchar time[]="00:00:00";
uchar con[3];
uchar time_type;
getpcf8563(0x02,3,con);
time[0]=(con[2]>>4)+'0';
time[1]=(con[2]&0x0f)+'0';
time[3]=(con[1]>>4)+'0';
time[4]=(con[1]&0x0f)+'0';
time[6]=(con[0]>>4)+'0';
time[7]=(con[0]&0x0f)+'0';
time[8]=0;
if(type==1)
{
time_type=0xff;
}
else
{
time_type=0;
}
// dipchar(x0,y0,F57,1,time_type,time);
} */
uchar BCD_hex(uchar x)
{
uchar tiger;
tiger=(x>>4)*10+((x&0x0f)*1);
return tiger;
}
void init()
{
lcden=0;
write_com(0x38); // 設置16x2顯示,5x7點陣,8位數據接口
write_com(0x0c); // 設置開顯示,顯示光標 ,光標閃爍 //慣用oxoc,開顯示,不顯示光標更別提閃爍
write_com(0x06); // 寫一個字符后地址指針加1
write_com(0x01); // 顯示清0 , 數據指針清0; 如若沒有清零則一開始有亂碼現象
write_com(0x80); // 命令先將數據指針定位到第一行第一個字處
}
void main()
{
init();
while(1)
{
fen=read1380(0x03);
shi=read1380(0x04);
miao=read1380(0x02);
write_com(0x80); // 將 時間用液晶顯示出去
write_sfm(0x04,BCD_hex(shi));
write_sfm(0x07,BCD_hex(fen));
write_sfm(0x0a,BCD_hex(miao));
delay(500);
}
}
想先把PCF8563里的時間讀出在LCD里,但就是不知道為啥讀不出,求解答,謝謝!
|
|