STM32 interrupt and DMA communication programming

1, Interrupt mode programming

One pin of the GPIOA end of the stm32F103 core board is connected to an LED, and one pin of the GPIOB port is connected to a switch (replaced by DuPont line simulation). Interrupt mode programming is adopted. When the switch is connected to high level, the LED lights up; When connected to low level, the LED is off.

1. Stm32subemx project creation

1. Create a new project and select STM32F103C8 as the chip

2. Set pin


3. Remember to check here

4. Set project name and location

5. Click create project to generate the project as follows:

2.Keil modification code

1. Pass stm32f1xx_ Exti9 in it. C file_ 5_ The irqhandler function f12 jumps to stm32f1xx_ hal_ The GPIO. C file found HAL_GPIO_EXTI_Callback function and modify

    void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
      if(GPIO_Pin == SWITCH_Pin){
        GPIO_PinState pinState = HAL_GPIO_ReadPin(SWITCH_GPIO_Port,SWITCH_Pin);
        if(pinState==GPIO_PIN_RESET)
        HAL_GPIO_WritePin(LED_A4_GPIO_Port,LED_A4_Pin,GPIO_PIN_RESET);
        else
          HAL_GPIO_WritePin(LED_A4_GPIO_Port,LED_A4_Pin,GPIO_PIN_SET);
        }
    }

2. Burn

3. Operation results

Embedded system - STM32 interrupt mode programming effect

2, Serial port interrupt

1. Stm32subemx create project



2.Keil modification code

1. Rewrite interrupt handling function

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(c=='0'){
		flag=0;
		HAL_UART_Transmit(&huart1, (uint8_t *)&tips2, 12,0xFFFF); 
	}
	else if(c=='1'){
		flag=1;
		HAL_UART_Transmit(&huart1, (uint8_t *)&tips1, 12,0xFFFF); 
	}
		HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);  
}

2. Modify the while loop in the main function

if(flag==1){
			HAL_UART_Transmit(&huart1, (uint8_t *)&message, 10,0xFFFF); 
			HAL_Delay(1000);
		}

3. Set receiving interrupt in main function

HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);

4. Define global variables

char c;//Directive 0: stop 1: start
char message[]="hello!\n";//Output information
char tips1[]="Start.....\n";//Tip 1
char tips2[]="Stop......\n";//Tip 2
int flag=0;//Flag 0: stop sending 1. Start sending

3. Burn and run the program

Embedded system STM32 serial port interrupt

3, DMA mode

1.stm32cubeMX create project process

1.rcc setting

2. Set serial port

3. Click dma settings

4. Add rxtx two channels

2. Code modification

Modify main function

int main(void)
{
  HAL_Init();
  uint8_t message[] = "Hello DMA!\n";
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  while (1)
  {
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *)message, sizeof(message));
	  HAL_Delay(1000);
  }
}

3. Burn and run the results


4, Summary

Through this experiment, I learned that the DMA transmission mode does not need the CPU to directly control the transmission, nor does it have the process of retaining and restoring the site like the interrupt processing mode. Opening up a direct data transmission path for RAM and I/O equipment through hardware can greatly improve the efficiency of the CPU. On the same DMA module, the priority between multiple requests can be set by software programming (there are four levels: very high, high, medium and low). When the priority settings are equal, they are determined by hardware (request 0 is better than request 1, and so on). The transmission width (byte, half word, full word) of independent data source and target data area, simulating the process of packaging and unpacking. The source and destination addresses must be aligned by data transfer width.

5, Reference link

https://blog.csdn.net/m0_51120713/article/details/121179281?spm=1001.2014.3001.5501.
https://blog.csdn.net/m0_51120713/article/details/121179281?spm=1001.2014.3001.5501.
https://blog.csdn.net/qq_47281915/article/details/121063896?spm=1001.2014.3001.5501.

Tags: Single-Chip Microcomputer stm32

Posted on Mon, 08 Nov 2021 17:38:49 -0500 by atkman