资源描述:
《读写AT24C256按页操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Main.c#include"Include.h"uchardat[]={"654321987654321223"},dat1[19],s[1];uchardat2[]={"12345612345678923"};voiddelay1s(unsignedintz)//延时1ms{unsignedintx,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}voidmain(){uchari,addr=0xc0;lcd_init();while(1){WriteStr(0xa0,0x01,0xc0,dat);delay1
2、s(5);WriteStr(0xa0,0x02,0xc0,dat2);delay1s(5);for(i=0;i<18;i++){dat1[i]=ReadStr(0xa0,0x02,addr,s);delay1s(5);addr++;}addr=0xc0;lcd_show(0,0,dat1);lcd_show(1,0,dat1+7);}}Iic.c#include"Include.h"ucharshuju;/*voiddelay1(unsignedintz)//延时1ms{unsignedintx,y;for(x=z;x>0;x--)for(y
3、=110;y>0;y--);}*/voiddelay()//延时5us{_nop_();_nop_();_nop_();_nop_();}voidstar()//IIC起始函数{sda=1;delay();scl=1;delay();sda=0;delay();}voidstop()//IIC停止函数{sda=0;delay();scl=1;delay();sda=1;delay();}ucharack()//应答信号{/*ucharz=0;while((sda==1)&&(z<100))z++;scl=0;delay();*/unsigne
4、dchartemp;sda=1;//------------scl=1;//--------delay();//-----temp=sda;//----------------scl=0;delay();returntemp;}voidsend_ack(){sda=0;scl=0;//----------scl=1;//-----delay();scl=0;delay();}voidnack()//发送非应答{sda=1;scl=1;delay();scl=0;delay();}voidwrite_byte(uchardat)//写一位数据{
5、uchari,temp;for(i=0;i<8;i++){scl=0;delay();temp=dat&0x80;dat=dat<<1;if(temp==0x80)sda=1;elsesda=0;delay();scl=1;delay();scl=0;delay();}sda=1;delay();scl=1;delay();}unsignedcharread_byte()//读取一位数据{unsignedchari,temp;sda=1;//释放总线delay();for(i=0;i<8;i++){temp=temp<<1;//sda=1;/
6、/delay();scl=0;delay();if(sda==1)temp=temp
7、0x01;elsetemp=temp
8、0x00;scl=1;delay();}//scl=0;//delay();returntemp;}voidWriteStr(ucharsla,ucharHigh_Byte_Address,ucharLow_Byte_Address,uchar*s){unsignedchari;star();write_byte(sla);ack();write_byte(High_Byte_Address);ack();write_b
9、yte(Low_Byte_Address);ack();for(i=0;i<18;i++){write_byte(*s);ack();s++;}stop();}ucharReadStr(ucharsla,ucharHigh_Byte_Address,ucharLow_Byte_Address,uchar*s){uchari,s1;star();write_byte(sla);//写数据命令ack();write_byte(High_Byte_Address);ack();write_byte(Low_Byte_Address);ack();s
10、tar();write_byte(sla+1);//读数据命令ack();/*for(i=0;i<4;i++){*s=read_byte();send_ack();