Hardware platform: STM32F401
Editor: keil 5.18
Operating system: win7
1. I2C Protocol
When transmitting data, the SDA line must remain stable during the high level period of the clock. The high or low level state of SDA can only change when the clock signal of the SCL line is low.
Start and Stop Conditions
When the SCL line is high, the SDA line switches from high to low, which indicates the start condition;
When the SCL line is high, the SDA line switches from low to high, which indicates a stop condition.
Addressing mode (7/10-bit address mode)
The first 7 bits of the first byte form the slave address, and the least significant bit (LSB) is the 8th bit, which determines the direction of the transmission of the 7-bit address format of the ordinary and repeated start conditions. The least significant bit of the first byte is "0", indicating that the host will write information to the selected slave; "1" indicates that the host will read information from the slave. When an address is sent, each device in the system compares the first 7 bits with its own address after the start condition. If the same, the device will determine that it is addressed by the master. Whether it is a slave receiver or a slave transmitter is determined by the R/W bit.
2. I2C Slave Mode
1. Set the i2c slave address
I2C_HandleTypeDef I2cHandle;
I2cHandle.Instance = I2C1;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.ClockSpeed = 400000;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
I2cHandle.Init.OwnAddress1 = I2C_ADDRESS;
I2cHandle.Init.OwnAddress2 = 0xFE;
As mentioned above, I2C has two address modes. Here, I2C_ADDRESSINGMODE_7BIT sets I2C to slave address mode 7. OwnAddress1 is the I2C address of this device, that is, the slave address we set ourselves. OwnAddress2 does not need to be set.
2. I2C pin and interrupt initialization
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIO TX/RX clock */
I2Cx_SCL_GPIO_CLK_ENABLE();
I2Cx_SDA_GPIO_CLK_ENABLE();
/* Enable I2C1 clock */
I2Cx_CLK_ENABLE();
/* Configure peripheral GPIO */
/* I2C TX GPIO pin configuration */
GPIO_InitStruct.Pin = I2Cx_SCL_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = I2Cx_SCL_AF;
HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
/* I2C RX GPIO pin configuration */
GPIO_InitStruct.Pin = I2Cx_SDA_PIN;
GPIO_InitStruct.Alternate = I2Cx_SDA_AF;
HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
/* Configure the NVIC for I2C */
/* NVIC for I2C1 */
HAL_NVIC_SetPriority(I2Cx_ER_IRQn, 0, 2);
HAL_NVIC_EnableIRQ(I2Cx_ER_IRQn);
HAL_NVIC_SetPriority(I2Cx_EV_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(I2Cx_EV_IRQn);
}
3. From sending
uint8_t aTxBuffer[] = "....hello world....";
while (1)
{
if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE+1)!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
HAL_Delay(100);
while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY)
{
Error_Handler();
}
}
That’s it.
Previous article:STM32's own IIC configuration
Next article:I2C communication between two STM32 chips
Recommended ReadingLatest update time:2024-11-16 14:28
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Introduction to DSP GPIO
- Examples of embedded system security issues
- The tool is still in Beta state
- EEWORLD University ---- Introduction to Power Electronics
- What does inductance 10t mean?
- How to Improve Heat Dissipation Using PCB Design
- Does anyone know which layers of AltiumDesigner the PCB production file suffixes correspond to?
- Comparison of CC2540 and nRF51822 application development
- The main purpose of immersion gold circuit board and the difference from other processes
- TAxIV interrupts for msp430