Welcome to Self-balancing Robot Car repository
15.DMA-I2C
DMA:I2C
DMA:I2CHardware connectionControl principleSoftware configurationPin definitionSoftware codeControl functionExperimental phenomenon
The tutorial demonstrates I2C communication through DMA to control OLED display.
The tutorial only introduces the standard library project code
Hardware connection

| Peripherals | Development board | Description |
|---|---|---|
| OLED: VCC | 5V | OLED power supply |
| OLED: GND | GND | OLED common ground |
| OLED: SCL | PB8 | Serial clock line (SCL) |
| OLED: SDA | PB9 | Serial data line (SDA) |
Control principle
xxxxxxxxxxI2C related knowledge will not be introduced, mainly DMA knowledge.
STM32F103RCT6 has two DMA controllers, DMA1 has 7 channels and DMA2 has 5 channels;
It is used for high-speed data transmission between peripherals and memory and between memory and memory.
DMA characteristics
DMA initialization and startup are completed by the CPU, and the transmission process is performed by the DMA controller without CPU participation, thus saving CPU resources for other operations.
DMA1 request for each channel

DMA2 request for each channel

This tutorial uses DMA1 channel 6.
Software configuration
Pin definition
| Main control chip | Pin | Main function (after reset) | Default multiplexing function | Redefine function |
|---|---|---|---|---|
| STM32F103RCT6 | PB8 | PB8 | TIM4_CH3 | I2C1_SCL/CAN_RX |
| STM32F103RCT6 | PB9 | PB9 | TIM4_CH4 | I2C1_SDA/CAN_TX |
Software code
Since the default function of the pin is the ordinary IO pin function, we need to use the redefine function.
xxxxxxxxxxProduct supporting data source code path: Attachment → Source code summary → 1.Base_Course → 15.I2C+DMA
Control function
The tutorial only briefly introduces the code, and you can open the project source code to read it in detail.
OLED_I2C_Init
xvoid OLED_I2C_Init(void){GPIO_InitTypeDef GPIO_InitStructure;I2C_InitTypeDef I2C_InitStructure;RCC_APB1PeriphClockCmd(OLEDI2C_RCC, ENABLE);RCC_APB2PeriphClockCmd(OLED_RCC, ENABLE);//开启复用功能RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);//PB8——SCL PB9——SDAGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;GPIO_InitStructure.GPIO_Pin = OLE