初中生
最后登录1970-1-1
在线时间 小时
注册时间2022-4-2
|
- #include "bsp_iic.h"
- void I2C_EE_Config()
- {
- I2C_InitTypeDef I2C_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //开GPIOB时钟(用 | 同时能开两个)
- EEPROM_I2C_GPIO_APBx_ClkCmd((EEPROM_I2C_SCL_GPIO_CLK|EEPROM_I2C_SDA_GPIO_CLK),ENABLE);
-
- //开I2C时钟
- EEPROM_I2C_APBxClkCmd(RCC_APB1Periph_I2C1,ENABLE);
-
-
- //配SCL
- 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_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_Ack=I2C_Ack_Enable;//使能应答
- I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
- I2C_InitStructure.I2C_ClockSpeed=EEPROM_I2C_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);//使能iic
-
- }
- //************************************* 写一个字节 ************************************************
- 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);
-
- //发送设备地址
- 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_TRANSMITTED)==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 num)
- {
- 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 (num)
- {
- if(num==1)//如果是读的最后一个数据,关应答
- {
- I2C_AcknowledgeConfig(EEPROM_I2C,DISABLE);
- }
-
- *data=I2C_ReceiveData(EEPROM_I2C);
- while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED)==ERROR);
- num--;
- data++;
- }
- I2C_GenerateSTOP(EEPROM_I2C,ENABLE);//停
- I2C_AcknowledgeConfig(EEPROM_I2C,ENABLE);//开应答,(应答默认状态为开)
- }
- void EEPROM_WaitForWriteEnd()
- {
- 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);//停
- }
复制代码
|
-
|