bsp_i2c.h:
#ifndef _BSP_I2C_H
#define _BSP_I2C_H
#include <stm32f10x.h>
#define EEPROM_I2C I2C1
#define EEPROM_I2C_CLK RCC_APB1Periph_I2C1
#define EEPROM_I2C_APBxClkCmd RCC_APB1PeriphClockCmd
#define EEPROM_I2C_BAUDRATE 400000
#define STM32_I2C_OWN_ADDR 0x5f
#define EEPROM_ADDR 0xA0
//I2C的GPIO引脚初始化
#define EEPROM_I2C_SCL_GPIO_CLK RCC_APB2Periph_GPIOB
#define EEPROM_I2C_SDA_GPIO_CLK RCC_APB2Periph_GPIOB
#define EEPROM_I2C_GPIO_APBxClkCmd RCC_APB2PeriphClockCmd
#define EEPROM_I2C_SCL_GPIO_PORT GPIOB
#define EEPROM_I2C_SCL_GPIO_PIN GPIO_Pin_6
#define EEPROM_I2C_SDA_GPIO_PORT GPIOB
#define EEPROM_I2C_SDA_GPIO_PIN GPIO_Pin_7
void I2C_Config(void);
void EEPROM_byte_Write(uint8_t addr,uint8_t data);
void EEPROM_Read(uint8_t addr,uint8_t* data,uint8_t number);
void EEPROM_WaitForWriteEnd(void);
#endif /*_BSP_I2C_H*/
bsp_i2c.c:
#include &quot;bsp_i2c.h&quot;
#include &quot;bsp_usart.h&quot;
void I2C_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
//打开GPIO时钟
EEPROM_I2C_GPIO_APBxClkCmd(EEPROM_I2C_SCL_GPIO_CLK|EEPROM_I2C_SDA_GPIO_CLK,ENABLE);
//打开I2C外设的时钟
EEPROM_I2C_APBxClkCmd(EEPROM_I2C_CLK,ENABLE);
//SCL的GPIO的配置
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SCL_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT,&GPIO_InitStructure);
//SDA的GPIO的配置
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SDA_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(EEPROM_I2C_SDA_GPIO_PORT,&GPIO_InitStructure);
//配置I2C的工作参数
I2C_InitStructure.I2C_ClockSpeed = EEPROM_I2C_BAUDRATE;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1= STM32_I2C_OWN_ADDR;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(EEPROM_I2C,&I2C_InitStructure);
//使能I2C
I2C_Cmd(EEPROM_I2C, ENABLE);
}
void EEPROM_byte_Write(uint8_t addr,uint8_t data)
{
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C,ENABLE);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
printf(&quot;1&quot;);
//发送设备地址
I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR);
//发送写地址
I2C_SendData(EEPROM_I2C,addr);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING) == ERROR);
//发送数据
I2C_SendData(EEPROM_I2C,data);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR);
//发送停止信号
I2C_GenerateSTOP(EEPROM_I2C,ENABLE);
}
void EEPROM_Read(uint8_t addr,uint8_t* data,uint8_t number)
{
//第一次起始信号
//产生起始信号
I2C_GenerateSTART(EEPROM_I2C,ENABLE);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
//发送设备地址
I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR);
//发送写地址
I2C_SendData(EEPROM_I2C,addr);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING) == ERROR);
//第二次起始信号
I2C_GenerateSTART(EEPROM_I2C,ENABLE);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) == ERROR);
//接收设备地址
I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Receiver);
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR);
while(number>0)
{
//判断是否为最后一个数据
if(number == 1)
{
I2C_AcknowledgeConfig(EEPROM_I2C,DISABLE);
}
while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED) == ERROR);
//取数据
*data = I2C_ReceiveData(EEPROM_I2C);
number--;
}
//发送停止信号
I2C_GenerateSTOP(EEPROM_I2C,ENABLE);
//重新设置应答信号
I2C_AcknowledgeConfig(EEPROM_I2C,ENABLE);
}
void EEPROM_WaitForWriteEnd(void)
{
do
{
I2C_GenerateSTART(EEPROM_I2C,ENABLE);
while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_SB) == RESET);
I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);
}while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_ADDR )== RESET);
I2C_GenerateSTOP(EEPROM_I2C,ENABLE);
}
main.c:
#include &quot;bsp_i2c.h&quot;
#include &quot;bsp_usart.h&quot;
int main()
{
uint8_t a[10] = {0};
Usart_Config();
I2C_Config();
printf(&quot;I2C实验:\n&quot;);
EEPROM_byte_Write(11,0x55);
EEPROM_WaitForWriteEnd();
EEPROM_byte_Write(12,0x56);
EEPROM_WaitForWriteEnd();
EEPROM_Read(11,a,2);
printf(&quot;接收到的数据为:0x%x,0x%x\n&quot;,a[0],a[1]);
while(1)
{
}
} |
|