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
}
Previous article:I2C communication between two STM32 chips
Next article:STM32 FSMC usage--LCD
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Vicor high-performance power modules enable the development of low-altitude avionics and EVTOL
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- "Cross-chip" quantum entanglement helps build more powerful quantum computing capabilities
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Europe's three largest chip giants re-examine their supply chains
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- GD32 setup reminder when downloading program in KEIL
- EEWORLD University ---- Introduction to CC2650MODAPluetooth? Low-energy RF module
- Thank you for having me + EE has brought me so much knowledge this year! + Thank you to my family for their support and love! + Thank you to my colleagues and friends for their generous help along the way!
- What is the drilling process in the Siliton ceramic substrate PCB?
- TJA1054 - Fault-tolerant CAN receiver
- Establishment and management of customer value assessment system
- What is the maximum transmission distance of serial communication?
- It was despised the day before yesterday, it was debunked yesterday, and today... it was blocked.
- P89LPC921/922 MCU Data Sheet
- J-linK connection problem