STM32 controls LED lights, serial communication and sending information through DMA through interrupt mode

catalogue

1, STM32 interrupt

1. What is an interrupt

2. Interrupt the corresponding process

3. Interrupt priority

2, High and low level control LED light on and off

3, Interrupt to realize serial communication

4, Realize the continuous sending of data to the host computer with DMA

5, Summary

6, References

1, STM32 interrupt

1. What is an interrupt

In the processor, interrupt is a process, that is, when the CPU encounters an external / internal emergency during normal program execution, it needs to deal with it, temporarily suspend the execution of the current program, turn to deal with the emergency, and return to the interrupted program after processing.

2. Interrupt the corresponding process

Interrupt source sends interrupt request

Judge whether the processor allows interrupts and whether the interrupt source is masked

Interrupt priority queuing

The processor pauses the current program, protects the breakpoint address and the current state of the processor, looks up the interrupt vector table according to the interrupt type number, and goes to the corresponding interrupt service program

Execute interrupt service program

Restore the protected state, execute the interrupt return instruction and return to the interrupted program

3. Interrupt priority

Priority is a very important concept in interrupts. If multiple interrupts are generated at the same time, the CPU will select the processing order of these interrupts according to their priority. In the CM4 kernel, priority is represented by an integer. The smaller the number, the higher the level.

There has been an interruption in work. It is determined according to the preemption priority. If the new interrupt does not interrupt the original interrupt, the interrupt nesting occurs, and the interrupt is suspended without interruption.
Interrupts are in the state of pending waiting. They are sorted according to the preemption priority first. Those with high preemption priority go first. If the preemption priority is the same, they are sorted according to the sub priority. Those with high sub priority go first and the sub priority is the same. Then they are sorted according to the small IRQ number
Preemption priority > sub priority > IRQ number

2, High and low level control LED light on and off

First in stmsubex, new project

Enter System core and click SYS to change the debug option to Serial Wire

Then select Crystal/Ceramic Resonator from HSE in Rcc  

  Here, select PB0 as the external interrupt trigger and PA5 as the control LED light, which is set as follows

 

Then select PLLCLK and change the maximum crystal oscillator frequency to 72M Hz

In project, change toolchain to MDK-ARM  

 

  Finally, initialize the file and generate code

  Open the generated code, open it with keil, and change the callback function in main.c to the following code

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
	GPIO_PinState b0_pin = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);  // Read the status of b0
	switch (GPIO_Pin){//Judgment pin
		case GPIO_PIN_0:
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,b0_pin);  // Write a5 to the same potential as b0
			break;
	}
	
}

The results are as follows

3, Interrupt to realize serial communication

Open CUBEMX, create a new project, enter CC settings, and set HSE

  SYS also selects serial Wire

  Select UASRT1, change the mode to asynchronous communication, select NVIC Setting below and check it

  The steps in the project manager are the same as the previous one, then generate the code, open the generated code with keil, and transfer the data defined in the main function  

uint8_t aRxBuffer;//Receive buffer interrupt
uint8_t Uart1_RxBuff[256];//Receive buffer
uint8_t Uart1_Rx_Cnt=0;//Receive buffer count
uint8_t cAlmStr[]="Data overflow (greater than 256)";

Put Hal_ UART_ The rxcpltcallback function is rewritten with the following code

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if(Uart1_Rx_Cnt >= 255)  //Overflow judgment
	{
		Uart1_Rx_Cnt = 0;
		memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
		HAL_UART_Transmit(&huart1, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);	
	}
	else
	{
		Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer;   //Receive data transfer
	
		if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)||(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //Judgment end bit
		{
			HAL_UART_Transmit(&huart1, (uint8_t *)&Uart1_RxBuff, Uart1_Rx_Cnt,0xFFFF); //Send the received information
			Uart1_Rx_Cnt = 0;
			memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff)); //Empty array
		}
	}
	
	HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);   //Restart receive interrupt

}

Then add the following code to the main function

HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART1_UART_Init();
	//Receive interrupt function
	HAL_UART_Receive_IT(&huart1,(uint8_t*)&aRxBuffer,1);

The operation results are as follows

4, Realize the continuous sending of data to the host computer with DMA

First open CUBRMX to create a new project, click RCC under System core to select HSE as Crystal/Ceramic Resonator, and DEBUG under SYS as serial Wire

 

  Then select PA9 and PA10 as USART1_RX and USART1_TX, then click USART1 on the left, set the mode to asynchronous communication mode, and then select Add pin in DMA Setting

  Then change the IDE to MDK-ARM in the project manger interface

 

Generate in the code generator   Check the first item of files

  Finally, click generate code

Open the generated code with keil and replace the while loop part of the main function in main.c with the following code

while (1)
  {
   uint8_t send_char[]="hello world\n";//Sent string
    HAL_UART_Transmit_DMA(&huart1,(uint8_t *)send_char,0xc);//DMA transmission
		HAL_Delay(500);//delayed
  }

Finally, the following results are obtained

5, Summary

Through this experiment, I learned how to use cubemax and hal library to turn on and off high and low-level LEDs, interrupt serial communication and send information continuously with DMA, but there are still many incomprehensible places that need more learning.

6, References

https://blog.csdn.net/weixin_46628481/article/details/121056373

https://blog.csdn.net/junseven164/article/details/121066120?spm=1001.2014.3001.5501

https://blog.csdn.net/junseven164/article/details/121071585?spm=1001.2014.3001.5501

 

Tags: Single-Chip Microcomputer stm32 ARM

Posted on Fri, 05 Nov 2021 15:02:45 -0400 by beanman1