单片机模拟I²C读写E2PROM

来源:本站
导读:目前正在解读《单片机模拟I²C读写E2PROM》的相关信息,《单片机模拟I²C读写E2PROM》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《单片机模拟I²C读写E2PROM》的详细说明。
简介:在这里给大家提供一个单片机模拟I²C读写E2PROM的程序。

//Name:  I2C standard program//Author: Ares//Date:    2005.11.4#i nclude <AT89X52.H>#i nclude <intrins.h>sbit SDA=P2^5;sbit SCL=P1^0;#define uchar unsigned char#define uint unsigned int#define byte unsigned charvoid delay(){_nop_();_nop_();_nop_();_nop_();_nop_();}void delayms (uint x){ byte j; while(x--){    for(j=0;j<125;j++){;}      }}void I2C_Start()                      //I2C START{ SDA=1; _nop_();  SCL=1;  delay();   SDA=0;   delay();  SCL=0;  _nop_();}void I2C_Stop()             //I2C STOP{SDA=0;_nop_();SCL=1;delay();SDA=1;delay();}bit  I2C_ReceiveAck() {bit ck;SDA=1;                  //THE HOST RELEASE THE BUS_nop_();_nop_();SCL=1;           _nop_();_nop_();ck=SDA;_nop_();_nop_();SCL=0;_nop_();return(ck);}void I2C_SendAck(){SDA=0;_nop_();_nop_();SCL=1;delay();          //remainSCL=0;_nop_();_nop_();}void I2C_SendNoAck(){SDA=1;_nop_();_nop_();SCL=1;delay();   //remainSCL=0;_nop_();_nop_();}void I2C_SendByte(byte SData){uchar i;for(i=0;i<8;i++){  SDA=(bit)(SData&0x80);   SData=SData<<1;   SCL=1;   delay();   SCL=0;}}byte I2C_ReceiveByte(){uchar i; byte RData; RData=0;for(i=0;i<8;i++){  SDA=1;  _nop_();  SCL=1;  _nop_();  _nop_();  RData=RData<<1;  RData=RData|SDA;  _nop_();  _nop_();  SCL=0;}return(RData);}void I2C_SendToSubAddress(byte SlaveAddress,byte SubAddress,byte *pSData,uchar N){uchar i;I2C_Start();I2C_SendByte(SlaveAddress);    //send slave address(write device)I2C_ReceiveAck();I2C_SendByte(SubAddress);    //send sub addressI2C_ReceiveAck();for(i=0;i<N;i++){  I2C_SendByte(pSData[i]);  I2C_ReceiveAck();}I2C_Stop();}void I2C_ReceiveFromSubAddress(byte SlaveAddress,byte SubAddress,byte *pRData,uchar N){uchar i;I2C_Start();I2C_SendByte(SlaveAddress);    //send slave address(write device)I2C_ReceiveAck();I2C_SendByte(SubAddress);    //send sub addressI2C_ReceiveAck();I2C_Start();                              //I2C Start Again!I2C_SendByte(SlaveAddress+1);    //send slave address(read device)I2C_ReceiveAck();for(i=0;i<N-1;i++){   pRData[i]=I2C_ReceiveByte();   I2C_SendAck();}pRData[i]=I2C_ReceiveByte();I2C_SendNoAck();I2C_Stop();}main(){byte *pData;  byte a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};  pData=a;I2C_ReceiveFromSubAddress(0xA0,0x20,pData,16);I2C_SendToSubAddress(0xA0,0,pData,16);}

提醒:《单片机模拟I²C读写E2PROM》最后刷新时间 2024-03-14 01:07:38,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《单片机模拟I²C读写E2PROM》该内容的真实性请自行鉴别。