微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 I2C AT24C02驱动

STM32 I2C AT24C02驱动

时间:11-25 来源:互联网 点击:


I2C_Send7bitAddress(I2C,AT24Cx_Address, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//检查数据是不是已经发送地址完成


I2C_SendData(I2C, Address);
while (!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //检查数据是不是已经发送完成

//需要读取数据,这个时候需要变换数据传输方向,就要主机重新发送起始位
I2C_GenerateSTART(I2C, ENABLE);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT));


I2C_Send7bitAddress(I2C,AT24Cx_Address, I2C_Direction_Receiver); //再一次发送EEPROM地址
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));


while(ReadNumber)
{

if(ReadNumber==1)
{
I2C_AcknowledgeConfig(I2C, DISABLE);//关闭应答
I2C_GenerateSTOP(I2C,ENABLE);//使能停止功能
}

while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_RECEIVED)); //检查是不是接受到了数据
*ReadBuffer=I2C_ReceiveData(I2C);
ReadBuffer++;
ReadNumber--;
}
I2C_AcknowledgeConfig(I2C, ENABLE);//允许应答模式
}

static void I2C_AT24Cx_WaitForComplete(void)
{
vu16 SR1_Tmp;
do
{

I2C_GenerateSTART(I2C1, ENABLE); //一旦发送起始位,主机等待读取SR1,然后发送地址

SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1); //也是为了清除该事件

I2C_Send7bitAddress(I2C1,AT24Cx_Address, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002)); //直到地址发送完成


I2C_ClearFlag(I2C, I2C_FLAG_AF); //前面如果出现过忙碌 就会出现应答错误现象

I2C_GenerateSTOP(I2C, ENABLE);
}

void I2C_AT24Cx_WriteByte(u8 Address,u8 WriteData)
{

I2C_GenerateSTART(I2C, ENABLE);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT));


I2C_Send7bitAddress(I2C,AT24Cx_Address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //实际就是等待收到EEPROM的ACK应答信号(收到ACK后,硬件会做相应的置位)


I2C_SendData(I2C, Address);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));


I2C_SendData(I2C, WriteData);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));


I2C_GenerateSTOP(I2C, ENABLE); //这里的设置停止为与接收时不同 这里是写入最后一个数据之后设置停止位

I2C_AT24Cx_WaitForComplete();
}

void I2C_AT24Cx_WritePage(u8 Address,u8 *WriteData,u16 WriteNumber)
{
while(I2C_GetFlagStatus(I2C, I2C_FLAG_BUSY));


I2C_GenerateSTART(I2C, ENABLE);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_MODE_SELECT));


I2C_Send7bitAddress(I2C,AT24Cx_Address, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));


I2C_SendData(I2C,Address);
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

while(WriteNumber--)
{
I2C_SendData(I2C,*WriteData);
WriteData++;
while(!I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}


I2C_GenerateSTOP(I2C, ENABLE);
}

void I2C_AT24Cx_Writes(u8 Address,u8 *WriteData,u16 WriteNumber)
{
u8 Temp;


Temp=Address%AT24Cx_PageSize;
if(Temp)//存在页不对齐的现象
{
Temp=AT24Cx_PageSize-Temp;//求出页不对齐的数量
I2C_AT24Cx_WritePage(Address,WriteData,Temp);
Address+=Temp;
WriteData+=Temp;
WriteNumber-=Temp;
I2C_AT24Cx_WaitForComplete();
}

while(WriteNumber)
{

if(WriteNumber>=AT24Cx_PageSize)
{
I2C_AT24Cx_WritePage(Address,WriteData,AT24Cx_PageSize);
Address+=AT24Cx_PageSize;
WriteData+=AT24Cx_PageSize;
WriteNumber-=AT24Cx_PageSize;
I2C_AT24Cx_WaitForComplete();
}

else
{
I2C_AT24Cx_WritePage(Address,WriteData,WriteNumber);
WriteNumber=0;
I2C_AT24Cx_WaitForComplete();
}
}
}

i2c_at24c02.h

#ifndef _I2C_AT24C02_H
#define _I2C_AT24C02_H

#include"stm32f10x.h"

#define I2C I2C1
#define I2C_RCC RCC_APB1Periph_I2C1

#define I2C_SCL_GPIO_RCC RCC_APB2Periph_GPIOB
#define I2C_SCL_GPIO GPIOB
#define I2C_SCL_Pin GPIO_Pin_6

#define I2C_SDA_GPIO_RCC RCC_APB2Periph_GPIOB
#define I2C_SDA_GPIO GPIOB
#define I2C_SDA_Pin GPIO_Pin_7

void I2C_AT24Cx_Init(void); //I2C外设与AT24C02初始化

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top