STM32通过I2C读写EEPROM的程序
时间:10-02
整理:3721RD
点击:
- /*I2C3?ê??ˉoˉêy*/
- void i2c_init()
- {
- I2C_InitTypeDef I2C_Struct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
- I2C_Struct.I2C_ClockSpeed=200000;
- I2C_Struct.I2C_Mode=I2C_Mode_I2C ;
- I2C_Struct.I2C_DutyCycle=I2C_DutyCycle_2;
- I2C_Struct.I2C_Ack=I2C_Ack_Enable;
- I2C_Struct.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit ;
- I2C_Init ( I2C1, &I2C_Struct);
-
- I2C_Cmd(I2C1,ENABLE);
- }
-
- /*GPIOB3?ê??ˉoˉêy*/
- void gpio_init()
- {
- GPIO_InitTypeDef GPIO_Struct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_Struct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
- GPIO_Struct.GPIO_Mode=GPIO_Mode_AF_OD;
- GPIO_Struct.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_Struct);
- GPIO_Struct.GPIO_Pin=GPIO_Pin_9;
- GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Struct.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_Struct);
- }
- /*μè′yEEPROM???D×′ì?*/
- void I2C_WaitEepromStandbyState(u8 id)
- {
- vu16 SR1_Tmp = 0;
- do
- {
- /* Send START condition */
- I2C_GenerateSTART(I2C1, ENABLE);
- /* Read I2C1 SR1 register */
- SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
- /* Send EEPROM address for write */
- I2C_Send7bitAddress(I2C1, id, I2C_Direction_Transmitter);
- }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002)); //estimate the value of ADDR
-
- /* Clear AF flag */
- I2C_ClearFlag(I2C1, I2C_FLAG_AF);
- }
- /*?ò???¨μ??·D′μ¥??×??ú3ìDò*/
- void i2c_write(u8 id,u8 address,u8 byte)
- {
- I2C_WaitEepromStandbyState(id);
- I2C_GenerateSTART ( I2C1, ENABLE);//·¢?í?eê?D?o?
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); //ev5
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Transmitter); //·¢?í?÷?tμ??·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED )); //ev6
- I2C_SendData ( I2C1, address);//·¢?íêy?Y?ú′?′¢?÷?Dμ?μ??·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ev8
- I2C_SendData ( I2C1, byte);//·¢?íêy?Y
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ev8_2
- I2C_GenerateSTOP(I2C1, ENABLE);//·¢?íí£?1D?o?
- }
- /*′ó???¨μ??·?áμ¥??×??ú3ìDò*/
- u8 i2c_read(u8 id,u8 address)
- {
- u8 temp;
- I2C_WaitEepromStandbyState(id);
- I2C_GenerateSTART(I2C1, ENABLE);//·¢???ˉD?o?
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));//EV5
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Transmitter);//·¢?í?÷?tμ??·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//EV6
- I2C_SendData ( I2C1, address);//·¢?íêy?Y?ú′?′¢?÷?Dμ??·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//EV8
- I2C_GenerateSTART(I2C1, ENABLE); //??D?·¢?í?aê?D?o?
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); //ev5
- I2C_AcknowledgeConfig(I2C1, DISABLE); //1?±?ó|′e1|?ü
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Receiver);//??D?·¢?í?÷?tμ??·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //ev6
- temp=I2C_ReceiveData(I2C1);//?óê?êy?Y
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); //ev7
- I2C_GenerateSTOP(I2C1, ENABLE);//·¢?íí£?1D?o?
- return temp;
- }
- int main(void)
- {
- i2c_init();
- gpio_init();
- i2c_write(0xa2,3,0xab);
- data=i2c_read(0xa2,3);
- if(data == 0xab)//?ì2a?á3?D′è?ê?·??àí?
- {
- for(;;)//LEDéá??
- {
- Delay_MS(1000);
- // GPIO_Write(GPIOB, 0x10);
- GPIOB->ODR = 0x10;
- GPIO_SetBits(GPIOB,GPIO_Pin_9);
- // GPIO_Write(GPIOB, 0x00);
- Delay_MS(1000);
- GPIOB->ODR = 0x00;
- GPIO_ResetBits(GPIOB,GPIO_Pin_9);
- }
- }
- while(1);
- }
写入读出结果不一样,什么地方有问题呢?
这在HAL库中已经自动实现了。
我记得STM32F103自身带的I2C有问题,不稳定,一般都是模拟I2C时序。
HAL库只是听说过,没有用过,一直都是用标准库的,HAL库好用吗
嗯,在网上看到相关的问题了,但是模拟的话好像是比较麻烦的吧
我一开始也是用标准库,后来领导要用HAL库,我痛苦了一个星期,后来发现HAL库确实比标准库好用。改变吧。