学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-7-25
|
main.c
#include "stm32f10x.h"
#include "bsp_usart.h"
#include "bsp_i2c.h"
int main(void)
{
uint8_t readData[20]={0};
uint8_t writeData[8]={4,5,6,7,8,9,10,11};
uint8_t i=0;
USART_Config();
I2C_EE_Config();
//发送一个字符串
printf("这是一个I2C通讯实验\n");
//写入一串数据
EEPROM_Page_Write(0,writeData,8);
//等待写入操作完成
EEPROM_WaitForWriteEnd();
//读取数据
EEPROM_Read(0,readData,8);
for(i=0;i<8;i++)
{
printf("%d ",readData); //%x为16进制输出
}
printf("\n");
while(1)
{}
}
bsp_i2c.c
#include "bsp_i2c.h"
void I2C_EE_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
//打开IIC GPIO的时钟
EEPROM_I2C_GPIO_APBxClkCmd(EEPROM_I2C_SCL_GPIO_CLK,ENABLE);
//打开IIC外设的时钟
EEPROM_I2C_APBxClkCmd(EEPROM_I2C_CLK,ENABLE);
//将I2C的GPIO配置为推挽复用模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SCL_GPIO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT ,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SDA_GPIO_PIN;
GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT ,&GPIO_InitStructure);
//配置I2C的工作参数
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//使能应答
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = EEPROM_I2_BAUDRATE;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_OwnAddress1 = STM32_I2C_OWN_ADDR;
I2C_Init(EEPROM_I2C,&I2C_InitStructure);
//I2C_Cmd(EEPROM_I2C,ENABLE);
}
//从EEPROM读取数据
void EEPROM_Read(uint8_t addr,uint8_t *data,uint8_t numByteToRead)
{
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C, ENABLE);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS);
//EV5事件被检测到,发送设备地址
I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDR,I2C_Direction_Transmitter);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) != SUCCESS);
//EV6事件被检测到,发送要操作的存储单元地址
I2C_SendData(EEPROM_I2C, addr);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING) != SUCCESS);
//第二次起始信号
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C, ENABLE);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS);
//EV5事件被检测到,发送设备地址
I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDR,I2C_Direction_Receiver);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) != SUCCESS);
while(numByteToRead)
{
if(numByteToRead==1)
{
//如果为最后一个字节
I2C_AcknowledgeConfig(EEPROM_I2C, DISABLE);
}
//EV7事件被检测到
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED) != SUCCESS);
//EV7事件被检测到,即数据寄存器有新的有效数据
*data=I2C_ReceiveData(EEPROM_I2C);
data++;
numByteToRead--;
}
//数据传输完成
I2C_GenerateSTOP(EEPROM_I2C, ENABLE);
//重新配置ACK使能,以便下次通讯
I2C_AcknowledgeConfig(EEPROM_I2C, ENABLE);
}
//等待EEPROM内部时序完成
void EEPROM_WaitForWriteEnd(void)
{
do{
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C, ENABLE);
while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_SB) != SET);
//EV5事件被检测到,发送设备地址
I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDR,I2C_Direction_Transmitter);
}
while(I2C_GetFlagStatus(EEPROM_I2C, I2C_FLAG_ADDR) != SET);
//内部时序完成
I2C_GenerateSTOP(EEPROM_I2C, ENABLE);
}
//向EEPROM写入多个字节(页写入),小于八个字节
void EEPROM_Page_Write(uint8_t addr,uint8_t *data,uint8_t numByteToWrite)
{
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C, ENABLE);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS);
//EV5事件被检测到,发送设备地址
I2C_Send7bitAddress(EEPROM_I2C, EEPROM_ADDR,I2C_Direction_Transmitter);
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) != SUCCESS);
//EV6事件被检测到,发送要操作的存储单元地址
I2C_SendData(EEPROM_I2C, addr);
//while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING) != SUCCESS);
while(numByteToWrite)
{
while(I2C_CheckEvent(EEPROM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING) != SUCCESS);
//EV8事件被检测到,发送要存储的数据
I2C_SendData(EEPROM_I2C, *data);
//while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS);
data++;
numByteToWrite--;
}
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS);
//数据传输完成
I2C_GenerateSTOP(EEPROM_I2C, ENABLE);
}
|
|