STM32 learning path: basic reading and writing of I2C

Publisher:柳絮轻风Latest update time:2018-04-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Macro definition:


/*I2C transmission speed, up to 400kHz*/

#define I2C_SPEED 400000


/* The I2C address of the STM32 itself. This address only needs to be different from the I2C device address of the STM32 external device*/

#define I2C_OWN_ADDR 0x77


/*EEPROM address*/

#define EEPROM_ADDR (0x50<<1) //0xA0


#define EEPROM_SCL_GPIO_CLK RCC_AHB1Periph_GPIOB

#define EEPROM_SCL_PIN GPIO_Pin_6

#define EEPROM_SCL_GPIO_PORT GPIOB

#define EEPROM_SCL_AF GPIO_AF_I2C1

#define EEPROM_SCL_SOURCE GPIO_PinSource6


#define EEPROM_SDA_GPIO_CLK RCC_AHB1Periph_GPIOB

#define EEPROM_SDA_PIN GPIO_Pin_7

#define EEPROM_SDA_GPIO_PORT GPIOB

#define EEPROM_SDA_AF GPIO_AF_I2C1

#define EEPROM_SDA_SOURCE GPIO_PinSource7


#define EEPROM_I2C_CLK RCC_APB1Periph_I2C1

#define EEPROM_I2C I2C1




/*================1-Initialize EEPROM_GPIO port (refer to USART for multiplexed pin function)==========================*/

void EEPROM_GPIO_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;


RCC_AHB1PeriphClockCmd(EEPROM_SCL_GPIO_CLK|EEPROM_SDA_GPIO_CLK,ENABLE);


/* GPIO initialization */

/* Push-pull output/pull-up resistor/50M output speed*/

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  

  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;


/* Configure the SCL pin as an alternate function */

/* Select alternate function mode/select SCL pin*/

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

        GPIO_InitStructure.GPIO_Pin = EEPROM_SCL_PIN;  

  GPIO_Init(EEPROM_SDA_GPIO_PORT, &GPIO_InitStructure);



  /* Configure the SDA pin as an alternate function*/

 /* Select alternate function mode/select SDA pin*/

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin = EEPROM_SDA_PIN;

  GPIO_Init(EEPROM_SCL_GPIO_PORT, &GPIO_InitStructure);


/* Connect PXx to EEPROM_SCL*/

  GPIO_PinAFConfig(EEPROM_SCL_GPIO_PORT,EEPROM_SCL_SOURCE,EEPROM_SCL_AF);



  /* Connect PXx to EEPROM_SDA*/

  GPIO_PinAFConfig(EEPROM_SDA_GPIO_PORT,EEPROM_SDA_SOURCE,EEPROM_SDA_AF);


}




/*================2-Initialize IIC mode==============================================*/

/*


I2C Structure


 


ClockSpeed: Set the I2C transmission rate


Requirements: less than 400kHz


 


Mode: I2C usage mode


I2C_Mode_I2C : I2C normal mode


I2C Master and Slave Mode


I2C_Mode_SMBusDevice


I2C_Mode_SMBusHost


 


DutyCycle: sets the duty cycle of the I2C SCL line clock


I2C_DutyCycle_16_9 Duty cycle 16:9


I2C_DutyCycle_2 Duty cycle 2:1


 


OwnAddress1: STM32's I2C device's own address


 


Ack: I2C response enable setting


I2C_Ack_Enable: Enable


I2C_Ack_Disable: Disable


 


AcknowledgedAddress: I2C addressing mode


I2C_AcknowledgedAddress_7bit 7-bit addressing


I2C_AcknowledgedAddress_10bit 10-bit addressing


/*


void EEPROM_I2C_ModeConfig(void)

{

/* Define I2C structure declaration*/

I2C_InitTypeDef I2C_InitStruct;

/* Turn on the peripheral I2C clock */

RCC_APB1PeriphClockCmd(EEPROM_I2C_CLK,ENABLE);


/* I2C transfer rate */

I2C_InitStruct.I2C_ClockSpeed ​​= I2C_SPEED;

/* I2C mode */

I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

/* Duty cycle */

I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;

/* I2C address itself */

I2C_InitStruct.I2C_OwnAddress1 = I2C_OWN_ADDR;

/* Promise to enable */

I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;

/* Addressing mode */

I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;


/* Initialize I2C */

I2C_Init(EEPROM_I2C,&I2C_InitStruct);

  /* Enable I2C */

  I2C_Cmd(EEPROM_I2C,ENABLE);

}




/*==============3-Write a byte function==================================================*/



void EEPROM_Byte_Write(uint8_t* pData,uint8_t addr)

{

/* Start signal */

  I2C_GenerateSTART(EEPROM_I2C,ENABLE);

  

/* Detect EV5 event, used after the start signal*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS )

  {

     //....

  }

  

/* Send EEPROM address */

  I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);

  

  /* Detect EV6 event, used after slave address*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) != SUCCESS )

  {

  

  }

  

  /* Send storage address, that is, EEPROM internal address*/

  I2C_SendData(EEPROM_I2C,addr);

  

  /* Detect EV8 event, used to transmit data*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS )

  {

  

  }

  

  /* send data*/

  I2C_SendData(EEPROM_I2C,*pData);

  

  

  /* Detect EV8 events */

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS )

  {

  

  }

  

  /* Stop signal */

  I2C_GenerateSTOP(EEPROM_I2C,ENABLE);



}




/*==============4-Read a byte function===================================================*/




uint8_t EEPROM_Byte_Read(uint8_t addr)

{

  uint8_t readTemp;

  /* Wait for EEPROM internal writing to complete*/

  Wait_for_EEPROM();

  

  /* Start signal */

  I2C_GenerateSTART(EEPROM_I2C,ENABLE);

  

  /* Detect EV5 event, used after the start signal*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS )

  {

     //....

  }

  

  /* Send EEPROM address */

  I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);

  

  /* Detect EV6 event, used after slave address*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) != SUCCESS )

  {

  

  }

  

  /* Send the EEPROM internal address of the read data*/

  I2C_SendData(EEPROM_I2C,addr);

  

  /* Detect EV8_2 event*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED) != SUCCESS )

  {

  

  }

  

  /* Start signal */

  I2C_GenerateSTART(EEPROM_I2C,ENABLE);


  /* Detect EV5 event, used after the start signal*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS )

  {

     //....

  }



  /* Send EEPROM address */

  I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Receiver);

  

  /* Detect EV6 event, used after slave address*/

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) != SUCCESS )

  {

  

  }

  

  /*Terminate enable response*/

  I2C_AcknowledgeConfig(EEPROM_I2C,DISABLE);


  /* Detect EV7 event */

  while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED) != SUCCESS )

  {

  

  }

  

  /* Read received data */

  readTemp = I2C_ReceiveData(EEPROM_I2C);

   

  /* Stop signal */

  I2C_GenerateSTOP(EEPROM_I2C,ENABLE);

  

  return readTemp;



}




/*==============5-Waiting for EEPROM to finish writing (I don't quite understand the details)====================================================*/

void Wait_for_EEPROM(void)

{

  do

  {

    I2C_GenerateSTART(EEPROM_I2C,ENABLE);

  

// while(I2C_CheckEvent(EEPROM_I2C,I2C_EVENT_MASTER_MODE_SELECT) != SUCCESS )

// {

// //....

// }

      

    I2C_Send7bitAddress(EEPROM_I2C,EEPROM_ADDR,I2C_Direction_Transmitter);  

  

  }while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_ADDR) == RESET);

  

  //Wait for ADDR = 1 before executing the following statement

  

  I2C_ClearFlag(EEPROM_I2C,I2C_FLAG_AF);

  

  

  I2C_GenerateSTOP(EEPROM_I2C,ENABLE);



  

  while(I2C_GetFlagStatus(EEPROM_I2C,I2C_FLAG_BUSY) == SET);

  

  //The bus is idle, execute the following statement



}  


Keywords:STM32 Reference address:STM32 learning path: basic reading and writing of I2C

Previous article:I2C communication between two STM32 chips
Next article:STM32 FSMC usage--LCD

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号