您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > IIC协议详解-基于AT24CXX详细编程
IICE2PROMAT24C02AT24C02WPWPVccWPVss/AT24C02I2C12SCL=1SDASCL=1SDAAT24C021.SCLSDAI2CvoidIIC_Start(void){SDA_OUT();//sdaIIC_SDA=1;IIC_SCL=1;delay_us(5);IIC_SDA=0;delay_us(5);IIC_SCL=0;//I2C}2.SCLSDAI2CvoidIIC_Stop(void){SDA_OUT();//sdaSCL=1IIC_SCL=0;SDAIIC_SDA=0;delay_us(4);IIC_SCL=1;//IIC_SDA=1;//I2Cdelay_us(4);}3.I2C9SDA8voidIIC_Ack(void){IIC_SCL=0;//SDA_OUT();IIC_SDA=0;delay_us(2);IIC_SCL=1;delay_us(4);IIC_SCL=0;}4.voidIIC_NAck(void){IIC_SCL=0;SDA_OUT();IIC_SDA=1;delay_us(2);IIC_SCL=1;delay_us(4);IIC_SCL=0;}5.24C02SCLSDAu8IIC_Wait_Ack(void){u8ucErrTime=0;SDA_IN();//SDAIIC_SDA=1;delay_us(1);//IIC_SCL=1;delay_us(1);//SCLwhile(READ_SDA)//SDA{ucErrTime++;if(ucErrTime250){IIC_Stop();return1;}}IIC_SCL=0;//0return0;}7.SCL=0SCL=0SCLvoidIIC_Send_Byte(u8txd){u8t;SDA_OUT();IIC_SCL=0;//for(t=0;t8;t++){if((txd&0x80)7)//IIC_SDA=1;elseIIC_SDA=0;txd=1;delay_us(2);//TEA5767IIC_SCL=1;//delay_us(2);IIC_SCL=0;delay_us(2);}}8.SCL=0SCL=0SCL//1ack=1ACKack=0nACKu8IIC_Read_Byte(unsignedcharack){unsignedchari,receive=0;SDA_IN();//SDAfor(i=0;i8;i++){IIC_SCL=0;delay_us(2);IIC_SCL=1;receive=1;//if(READ_SDA)receive++;delay_us(1);}if(!ack)IIC_NAck();//nACKelseIIC_Ack();//ACKreturnreceive;}AT24C029.1).AT24CE2PROM1010A2A1A0372).7“0”8SDASCL9;SDA3).4).//AT24CXX//WriteAddr://DataToWrite:voidAT24CXX_WriteOneByte(u16WriteAddr,u8DataToWrite){IIC_Start();///*if(EE_TYPEAT24C16)//EE_TYPE=AT24c02{IIC_Send_Byte(0XA0);//IIC_Wait_Ack();//IIC_Send_Byte(WriteAddr8);//}else//01\02\04\08{*/IIC_Send_Byte(0XA0+((WriteAddr/256)1));//0XA0,}IIC_Wait_Ack();//IIC_Send_Byte(WriteAddr%256);//0-255IIC_Wait_Ack();//IIC_Send_Byte(DataToWrite);//IIC_Wait_Ack();//IIC_Stop();//delay_ms(10);//}10.P=151.//AT24CXX//WriteAddr:24c020~255//pBuffer://NumToWrite:8voidAT24CXX_Write(u16WriteAddr,u8*pBuffer,u16NumToWrite){while(NumToWrite--){AT24CXX_WriteOneByte(WriteAddr,*pBuffer);//1WriteAddr++;pBuffer++;}}2.//AT24CXXLen//16bit32bit.//WriteAddr://DataToWrite://Len:2,4voidAT24CXX_WriteLenByte(u16WriteAddr,u32DataToWrite,u8Len){u8t;for(t=0;tLen;t++){//24AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite(8*t))&0xff);}}11.1).2).24C02R/W124C028//AT24CXX//ReadAddr://:u8AT24CXX_ReadOneByte(u16ReadAddr){u8temp=0;IIC_Start();//if(EE_TYPEAT24C16){IIC_Send_Byte(0XA0);//IIC_Wait_Ack();IIC_Send_Byte(ReadAddr8);//IIC_Wait_Ack();}elseIIC_Send_Byte(0XA0+((ReadAddr/256)1));//0XA0,IIC_Wait_Ack();IIC_Send_Byte(ReadAddr%256);//IIC_Wait_Ack();IIC_Start();//IIC_Send_Byte(0XA1);//IIC_Wait_Ack();temp=IIC_Read_Byte(0);IIC_Stop();//returntemp;//}12.1).//AT24CXX//ReadAddr:24c020~255//pBuffer://NumToRead:voidAT24CXX_Read(u16ReadAddr,u8*pBuffer,u16NumToRead){while(NumToRead){*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);NumToRead--;}}2).//AT24CXXLen//16bit32bit.//ReadAddr://://Len:2,4u32AT24CXX_ReadLenByte(u16ReadAddr,u8Len){u8t;u32temp=0;for(t=0;tLen;t++){temp=8;temp+=AT24CXX_ReadOneByte(ReadAddr+Len–t-1);}returntemp;}
本文标题:IIC协议详解-基于AT24CXX详细编程
链接地址:https://www.777doc.com/doc-4978165 .html