最近学习IIC。。大家看看写的程序有问题吗?为什么仿真不行呢? #include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit lcden=P3^2; sbit lcdrs=P3^0; sbit lcdrw=P3^1; sbit sda=P2^6; sbit scl=P2^5; uchar a; uchar time[]={0,0,0,0,0,0,0,0}; void delay1(uint z) { uint x,y; for(x=z;x>0;x--) for(y=100;y>0;y--); } void write_com(uchar com) { lcdrs=0; P0=com; delay1(5); lcden=1; delay1(5); lcden=0; } void write_date(uchar date) { lcdrs=1; P0=date; delay1(5); lcden=1; delay1(5); lcden=0;
} void init() { lcden=0; lcdrw=0; write_com(0x38); write_com(0x0e); write_com(0x06); write_com(0x01); write_com(0x80);
} void delay() { ;; } void start() //开始信号 { sda=1; delay(); scl=1; delay(); sda=0; delay(); }
void stop() //停止 { sda=0; delay(); scl=1; delay(); sda=1; delay(); }
void respons() //应答 { uchar i; scl=1; delay(); while((sda==1)&&(i<250))i++; scl=0; delay(); } void norespons() { sda=1; delay(); scl=1; delay(); scl=0; } void initpcf() { sda=1; delay(); scl=1; delay(); }
void write_byte(uchar date) { uchar i,temp; temp=date;
for(i=0;i<8;i++) { temp=temp<<1; scl=0; delay(); sda=CY; delay(); scl=1; delay(); // scl=0; // delay(); } scl=0; delay(); sda=1; delay(); }
uchar read_byte() { uchar i=8; uchar ddate; sda=1; while(i--) { ddate<<=1; scl=0; delay(); scl=1; delay(); ddate|=sda; } scl=0; return ddate; }
void delay2(uchar x) { uchar a,b; for(a=x;a>0;a--) for(b=100;b>0;b--); }
void write_add(uchar address,uchar date) { start(); write_byte(0xa2); respons(); write_byte(address); respons(); write_byte(date); respons(); stop(); }
void getpcf8563(uchar address,uchar count,uchar *buff) { uchar i; start(); write_byte(0xa2); respons(); write_byte(address); respons(); start(); write_byte(0xa3); respons(); for(i=0;i<=count;i++) { buff=read_byte(); if(i!=count-1) respons(); } norespons(); stop(); } void time_display() { uchar temp[3]; getpcf8563(0x02,3,temp); time[0]=(temp[2]>>4)+'0'; time[1]=(temp[2]&0x0f)+'0'; time[3]=(temp[1]>>4)+'0'; time[4]=(temp[1]&0x0f)+'0'; time[6]=(temp[0]>>4)+'0'; time[7]=(temp[0]&0x0f)+'0'; } void main() { uint b,c; b=0; init(); initpcf(); while(1) { time_display(); for(c=0;c<8;c++) { write_date(time); b++; delay1(200); } write_com(0x80); b=0; } } |