- #define SCL_L //拉低SCL线的宏
- #define SCL_H //拉高SCL线的宏
- #define SDA_L //拉低SDA线的宏
- #define SDA_H //拉高SDA线的宏
- #define SDA_Read //读SDA线的宏
- #define Wait //延时宏 大约为5us
- void IIC_Start(void)//启动IIC函数
- {
- SDA_H;
- SCL_H;
- Wait;
- SDA_L;
- Wait;
- SCL_L;
- Wait;
- }
- void IIC_Stop(void)//停止IIC函数
- {
- SDA_L;
- SCL_H;
- Wait;
- SDA_H;
- }
- void IIC_Send_Byte(uint8_t data)//想IIC器件发送8个位数据
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- if(data&(0x80>>i))
- SDA_H;
- else
- SDA_L;
- Wait;
- SCL_H;
- Wait;
- SCL_L;
- Wait;
- }
- SDA_H;
- Wait;
- }
- uint8_t IIC_Read_Byte()//读取8位数据
- {
- uint8_t i,buff=0x00;
- for(i=0;i<8;i++)
- {
- buff<<=1;
- SCL_H;
- Wait;
- if(SDA_Read)
- buff|=0x01;
- SCL_L;
- Wait;
- }
- return buff;
- }
- uint8_t Wait_Ack(void)//等待应答
- {
- uint8_t flag;
- SDA_H;
- Wait;
- SCL_H;
- Wait;
- if(SDA_Read)
- flag=1;
- else
- flag=0;
- SCL_L;
- Wait;
- return flag;
- }
- void Ack(void)//发送应答
- {
- SDA_L;
- Wait;
- SCL_H;
- Wait;
- SCL_L;
- Wait;
- SDA_H;
- }
- void NAck(void)//发送非应答
- {
- SDA_H;
- Wait;
- SCL_H;
- Wait;
- SCL_L;
- Wait;
- }
- uint8_t AT24C02_Pag_Write(uint8_t address,uint8_t *data,uint8_t size)//AT24C02写数据函数 第一个参数是 AT24C02的要写页的地址 0-255,第二个参数是 缓冲区指针,第三个参数是 要写的字节数//不能超过8个字节
- {
- uint8_t i;
- IIC_Stop();
- for(i=0;i<100;i++)
- {
- IIC_Start();
- IIC_Send_Byte(0xa0);
- if(Wait_Ack()==0)
- {
- break;
- }
- }
- if(m=100)
- {
- IIC_Stop();
- return 0;
- }
- IIC_Send_Byte(address);
- if(Wait_Ack())
- {
- IIC_Stop();
- return 0;
- }
- for(i=0;i<size;i++)
- {
- IIC_Send_Byte(*(data+i));
- if(Wait_Ack())
- {
- IIC_Stop();
- return 0;
- }
- }
- IIC_Stpo();
- return 1;
- }
- uint8_t AT24C02_Write(uint8_t address,uint8_t *data,uint8_t size)//对任意AT24C02的地址进行写操作 其中第一个参数是 AT24C02要写的地址 第二个参数是 缓冲区指针 第三个参数是 要写的字节数 0-255;
- {
- uint8_t Page=0;//所需要的页数
- uint8_t Cont=0;//不足一页的字节数
- uint8_t Add_Pag=0;//某一页已经写了的字节数
- uint8_t Sur=0;//某一页还能写的字节数
-
- Page=size/8;
- Cont=size%8;
- Add_Pag=address%8;
- Sur=8-Add_Pag;
-
- if(Add_Pag==0)
- {
- if(Page==0)
- {
- AT24C02_Pag_Write(address,data,size);
- Delay();//此函数没有定义 需要自己定义 大约延时2000us
- }
- else
- {
- while(Page--)
- {
- AT24C02_Pag_Write(address,data,8);
- Delay();
- address+=8;
- data+=8;
- }
- if(Cont!=0)
- {
- AT24C02_Pag_Write(address,data,Cont);
- Delay(2000);
- }
- }
- }
- else
- {
- if(Page==0)
- {
- if(Sur>=size)
- {
- AT24C02_Pag_Write(address,data,size);
- Delay;
- }
- else
- {
- AT24C02_Pag_Write(address,data,Sur);
- Delay;
- address+=Sur;
- data+=Sur;
- AT24C02_Pag_Write(address,data,size-Sur) ;
- Delay;
- }
- }
- else
- {
- AT24C02_Pag_Write(address,data,Sur);
- Delay(2000);
- address+=Sur;
- data+=Sur;
- Page=size/8;
- Cont=size%8;
- Add_Page=address%8;
- Sur=8-Add_Page;
- if(Page==0)//
- {
- AT24C02_Pag_Write(address,data,size);
- Delay(2000);
- }
- else//
- {
- while(Page--)
- {
- AT24C02_Pag_Write(address,data,8);
- Delay(2000);
- address+=8;
- data+=8;
- }
- if(Cont!=0)
- {
- AT24C02_Pag_Write(address,data,Cont);
- Delay(2000);
- }
- }
- }
- }
- }
-
- uint8_t AT24C02_Read(uint8_t address,uint8_t *data,uint8_t size)//从AT24C02读取size个字节 第一个参数是 读AT24C02 的数据的首地址 第二个参数是 缓冲区指针 第三个参数是 要读取的字节数
- {
- uint8_t i;
- IIC_Start();
- IIC_Send_Byte(0xa0);
- if(Wait_Ack())
- {
- IIC_Stop();
- return 0;
- }
- IIC_Send_Byte(address)
- if(Wait_Ack())
- {
- IIC_Stop();
- return 0;
- }
- IIC_Start();
- IIC_Send_Byte(0xa1);
- if(Wait_Ack())
- {
- IIC_Stop();
- return 0;
- }
- for(i=0;i<size;i++)
- {
- *(data+i)=IIC_Read_Byte();
- if(i!=size-1)
- {
- Ack();
- }
- else
- {
- NAck();
- }
- }
- IIC_Stop();
- return 1;
- }
复制代码
|