1, Interrupt
Data output mode:
Interrupt process:
Interrupt program execution process:
Function of interrupt:
1. It can solve the contradiction of data transmission between fast CPU and slow external devices.
2. The CPU can serve multiple external devices in time-sharing to improve the utilization of the computer.
3. The CPU can handle the random events of the application system in time to enhance the practicability of the system.
4.CPU can deal with equipment failure, power failure and other emergencies to improve system reliability.
Interrupt priority:
1. It can solve the contradiction of data transmission between fast CPU and slow external devices.
2. The CPU can serve multiple external devices in time-sharing to improve the utilization of the computer.
3. The CPU can handle the random events of the application system in time to enhance the practicability of the system.
4.CPU can deal with equipment failure, power failure and other emergencies to improve system reliability.
Interrupt priority:
The processor sets different priority levels according to the important programs of different interrupts. The processing principles of different priority interrupts are: high-level interrupts can interrupt low-level interrupts; Low level interrupts cannot interrupt high-level interrupts.
Interrupt vector:
2, HAL library interrupt light LED
1. Create project
Find PB5 and select GPIO - EXTI5 interrupt mode
PA1 pin setting
Open interrupt
RCC configuration: configure the clock source as an external clock source
Clock tree settings
project management
Generate code, open project, and in the project code, open stm32f1xx_ hal_ The interrupt service function can be found in the GPIO. C file
Add the following code to the main.c file:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { /* Prevent unused argument(s) compilation warning */ HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1); //Flip level /* NOTE: This function Should not be modified, when the callback is needed, the HAL_GPIO_EXTI_Callback could be implemented in the user file */ }
3, HAL library interrupts serial communication
CubeMX configuration:
Configure USART1 as asynchronous communication mode:
Configure serial port:
Generate the code, open the project, and add the definition in main.c
uint8_t aRxBuffer; //Receive interrupt buffer uint8_t Uart1_RxBuff[256]; //Receive buffer uint8_t Uart1_Rx_Cnt = 0; //Receive buffer count uint8_t cAlmStr[] = "data overflow (Greater than 256)\r\n";
Add code to the main function at the following location:
/* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); /* USER CODE END 2 */ ``/* USER CODE BEGIN 4 */ /** * @brief Rx Transfer completed callbacks. * @param huart pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* Prevent unused argument(s) compilation warning */ UNUSED(huart); /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART_TxCpltCallback could be implemented in the user file */ if(Uart1_Rx_Cnt >= 255) //Overflow judgment { Uart1_Rx_Cnt = 0; for(int i=0;i<255;i++) { Uart1_RxBuff[i]=0; } 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; for(int i=0;i<255;i++) { Uart1_RxBuff[i]=0; } //Empty array } } HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //Restart receive interrupt } /* USER CODE END 4 */c
Commissioning results
4, Summary
Program setting interrupt can flexibly handle transactions and give priority to more urgent transactions.
5, Reference link
https://blog.csdn.net/weixin_46628481/article/details/121056373
https://blog.csdn.net/cayloon/article/details/79196942