#include<reg51.h>
#include<intrins.h>
#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 err
time=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<count;i++)
{
buff[i]=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 setal
ARM(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里,但就是不知道为啥读不出,求解答,谢谢!
-
-
一周热门 更多>