AT24C08的读、写程序.doc

上传人:飞****9 文档编号:135968387 上传时间:2020-06-21 格式:DOC 页数:5 大小:22.50KB
返回 下载 相关 举报
AT24C08的读、写程序.doc_第1页
第1页 / 共5页
AT24C08的读、写程序.doc_第2页
第2页 / 共5页
AT24C08的读、写程序.doc_第3页
第3页 / 共5页
AT24C08的读、写程序.doc_第4页
第4页 / 共5页
AT24C08的读、写程序.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《AT24C08的读、写程序.doc》由会员分享,可在线阅读,更多相关《AT24C08的读、写程序.doc(5页珍藏版)》请在金锄头文库上搜索。

1、#include / 对AT24C08的读、写/ extern void DelayMs(unsigned int);/ extern void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);/ extern void WriteI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);/*/#define WriteDeviceAddress 0xa0/写器件地址#define Read

2、DviceAddress 0xa1/读器件地址/*/sbit SCL=P10;/I2C时钟线SCLsbit SDA=P11;/I2C数据线SDAsbit DOG=P37;/程序运行标志及数据读写正确标志/*/void DelayMs(unsigned int number)/延时子程序,延时大小取决于工作频率和number值unsigned char temp;for(;number!=0;number-,DOG=!DOG)/循环,DOG用于输出状态信号for(temp=112;temp!=0;temp-)/空循环/*/void Start()/起始信号子程序 SDA=1; DelayMs(1

3、);/延时 SCL=1; DelayMs(1); SDA=0; DelayMs(1); SCL=0; DelayMs(1);/*/void Stop()/终止信号子程序 SCL=0; DelayMs(1); SDA=0; DelayMs(1); SCL=1; DelayMs(1); SDA=1; DelayMs(1);/*/void Ack()/发送应答位子程序 SDA=0; DelayMs(1); SCL=1; DelayMs(1); SCL=0; DelayMs(1); SDA=1; DelayMs(1);/*/void NoAck()/发送非应答位子程序 SDA=1; DelayMs(1

4、); SCL=1; DelayMs(1); SCL=0; DelayMs(1);/*/bit TestAck()/应答位检查子程序 bit ErrorBit; SDA=1; DelayMs(1); SCL=1; DelayMs(1); ErrorBit=SDA;/读入数据线上的应答状态 DelayMs(1); SCL=0; DelayMs(1); return(ErrorBit);/返回应答状态,0为正常应答信号,1为非应答信号/*/bit Write8Bit(unsigned char input)/写一个字节数据子程序/input为待发送的数据 unsigned char temp; fo

5、r(temp=8;temp!=0;temp-)/循环移位,逐位发送数据 SDA=(bit)(input&0x80);/取数据的最高位 DelayMs(1); SCL=1; DelayMs(1); SCL=0; DelayMs(1); input=input1;/左移一位 return 1;/*/void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) /写n个字节数据子程序 Start();/启动 Write8Bit(WriteDeviceAddress);/写写器件的寻址地址 TestA

6、ck();/应答检查 Write8Bit(RomAddress);/写入I2C器件内部的数据存储首地址 TestAck();/应答检查 for(;number!=0;number-)/循环,逐个字节发送 Write8Bit(*Wdata);/写一个字节 TestAck();/应答检查 Wdata+;/指针增加,指向下一个数据 Stop();/停止 DelayMs(10);/*/unsigned char Read8Bit()/读一个字节数据子程序 unsigned char temp,rbyte=0; for(temp=8;temp!=0;temp-)/循环,逐位读入字节数据 SCL=1; D

7、elayMs(1); rbyte=rbyte1;/左移一位 DelayMs(1); rbyte=rbyte|(unsigned char)(SDA);/数据线SDA上的数据存入rbyte的最低位 SCL=0; DelayMs(1); return(rbyte);/返回读入的字节数据/*/void ReadI2C(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) /读n个字节数据子程序 Start();/启动 Write8Bit(WriteDeviceAddress);/写写器件的寻址地址 TestAc

8、k();/应答检查 Write8Bit(RomAddress);/写I2C器件内部数据的读取首地址 TestAck();/应答检查 Start();/重新启动 Write8Bit(ReadDviceAddress);/写读器件的寻址地址 TestAck();/应答检查 while(bytes!=1)/循环读入字节数据 *RamAddress=Read8Bit();/读入一个字节 Ack();/应答 RamAddress+;/地址指针递增 bytes-;/待读入数据个数递减 *RamAddress=Read8Bit();/读入最后一个字节数据 NoAck();/非应答 Stop();/停止voi

9、d main()unsigned char writeByte8=0xC0,0x34,0x12,0x11,0x22,0x01,0x00,0x00;/需要写的8个字节USB数据IDunsigned char readByte8;/用于存读入的8个字节数据unsigned char *addw;/写数据指针操作unsigned char *addr;/读数据指针操作unsigned char i;unsigned char ok=0;bit write=1;/读写标志DOG=0;while(1)if(write=1)/当write=1时,执行写和读操作addw=writeByte;/写地址映射addr=readByte;/读地址映射WriteI2C(addw,0x00,8);/写数据ReadI2C(addr,0x00,8);/读数据for(i=0;i8;i+) /判断每个字节读写是否一致if(writeBytei=readBytei)ok+;if(ok=8)DOG=1;/当读写一致时,P3.7输出高电平elseDOG=0;/当读写不一致时,P3.7输出低电平 write=0;/置write=0,读写完毕

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 教学课件 > 初中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号