当前位置:网站首页>Stm32cubemx learning (6) external interrupt experiment
Stm32cubemx learning (6) external interrupt experiment
2022-06-29 20:22:00 【Xiaohui_ Super】
Personal learning record
List of articles
One 、 New project

Two 、 Choose chip model
The development board I use is a punctual atom STM32F103ZET6 Core board

3、 ... and 、 Configure clock
The development board is welded with an external crystal oscillator , So I RCC(Reset and Cock Control) Configuration selected Crystal/Ceramic Resonator( Quartz / Ceramic resonator ), When the configuration is complete , Dexter Pinout view The relevant pins in the are marked green .

After the external high-speed clock configuration is completed , Get into Clock Configuration Options , According to the actual situation , Configure the system clock to 72 MHz, The configuration steps are as follows , Finally press enter , The software will automatically adjust the frequency division and frequency doubling parameters .

Four 、 Configure debug mode
ST-Link Namely Serial Wire Debug mode , Be sure to set !!!
I used to use M0 Chip , There is no problem if this mode is not configured , But now this model , If you don't configure Serial Wire Pattern , Once the program is passed ST-Link Burn into the chip , Chips can no longer be ST-Link Identified .( Later I passed STMISP Tool burning program / After erasing, it will return to normal )

5、 ... and 、 External interrupt parameter configuration

The development board I use has two buttons , And two of the single-chip computers IO Connected to a , Pull the other foot up , So when the key is pressed ,IO Can receive high level signal .
Follow the steps below to configure these two GPIO: take GPIO Set to external interrupt function , The interrupt mode is rising edge / Falling edge trigger ( This experiment mainly uses the rising edge trigger )

External interrupts also need to be enabled , Can be in GPIO Of NVIC Enable... In configuration , You can also directly NVIC Turn on the corresponding interrupt line in the general configuration . in addition , Because my experiment will use Hal Library delay function ( Put it in the external interrupt handler , By the tick timer interrupt ) Therefore, the interrupt priority of the tick timer must be higher than that of the external interrupt .

6、 ... and 、 Generate Keil engineering
Set up IDE and Project catalogue and name :

Store the code of each peripheral in a different .c /.h In file , Easy to manage ( Otherwise, they will be put in main.c in ).

Here is the generation Keil The project is about GPIO(EXTI) Initialized code :
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PE4 */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI4_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}
7、 ... and 、 Where is the interrupt function written
When using the standard library , We write interrupt handling in the bottom interrupt handling function , Such as EXTI0_IRQHandler(), but Hal The library adds a callback function , Will interrupt some necessary operations at the bottom “ hide ” Get up ( Such as clearing the interrupt ).
The order in which interrupts are called is ( With EXTI0 For example ):EXTI0_IRQHandler() —> HAL_GPIO_EXTI_IRQHandler() —> HAL_GPIO_EXTI_Callback().
/** * @brief This function handles EXTI line0 interrupt. */
void EXTI0_IRQHandler(void)
{
/* USER CODE BEGIN EXTI0_IRQn 0 */
/* USER CODE END EXTI0_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
/* USER CODE BEGIN EXTI0_IRQn 1 */
/* USER CODE END EXTI0_IRQn 1 */
}
/** * @brief This function handles EXTI interrupt request. * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
/* EXTI line interrupt detected */
if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
HAL_GPIO_EXTI_Callback(GPIO_Pin);
}
}
8、 ... and 、 Test examples
The serial port is used in the experiment , Not mentioned in the above configuration , For serial port configuration, please refer to STM32CubeMx Study (2)USART Serial port experiment
The core of my experimental code is the interrupt callback function :
// External interrupt callback function
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_0)
{
HAL_Delay(10); // Eliminate jitter ( In actual use, it is not recommended to delay in interruption )
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET)
{
Exti_Flag1 = 1; // The external interrupt flag is set to 1, stay main Processing in function
}
}
if(GPIO_Pin == GPIO_PIN_4)
{
HAL_Delay(10); // Eliminate jitter ( In actual use, it is not recommended to delay in interruption )
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4) == SET)
{
Exti_Flag2 = 1; // The external interrupt flag is set to 1, stay main Processing in function
}
}
}
complete main.c
/* USER CODE BEGIN Header */
/** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
uint8_t Exti_Flag1; // External interrupt trigger flag
uint8_t Exti_Flag2; // External interrupt trigger flag
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/** * @brief The application entry point. * @retval int */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(Exti_Flag1)
{
// Serial port sends data
HAL_UART_Transmit(&huart1, "WK_UP is pressed.\r\n", 19, 0xffff);
// Clear the sign
Exti_Flag1 = 0;
}
if(Exti_Flag2)
{
// Serial port sends data
HAL_UART_Transmit(&huart1, "KEY0 is pressed.\r\n", 18, 0xffff);
// Clear the sign
Exti_Flag2 = 0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/** * @brief System Clock Configuration * @retval None */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {
0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {
0};
/** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
// External interrupt callback function
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_0)
{
HAL_Delay(10); // Eliminate jitter ( In actual use, it is not recommended to delay in interruption )
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == SET)
{
Exti_Flag1 = 1; // The external interrupt flag is set to 1, stay main Processing in function
}
}
if(GPIO_Pin == GPIO_PIN_4)
{
HAL_Delay(10); // Eliminate jitter ( In actual use, it is not recommended to delay in interruption )
if(HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_4) == SET)
{
Exti_Flag2 = 1; // The external interrupt flag is set to 1, stay main Processing in function
}
}
}
/* USER CODE END 4 */
/** * @brief This function is executed in case of error occurrence. * @retval None */
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Experimental results :
I didn't use real objects for testing , It USES Keil Software debugging function of , I've been using this function Remap serial port to rt_kprintf function ( Learning notes ) mention , There is no introduction here .
In software simulation , Set the... Corresponding to the key GPIO The input level of , The serial port will print the corresponding data , Indicates that the key is interrupted ( External interrupt ) Successfully triggered and the interrupt callback function successfully executed .

边栏推荐
- Tag based augmented reality using OpenCV
- Software engineering - principles, methods and Applications
- Introduction to the latest version 24.1.0.360 update of CorelDRAW
- 2021 CCPC Harbin E. power and modulo (thinking questions)
- Several policies of Shenzhen Futian District to support investment attraction in 2022
- . NETCORE unified authentication authorization learning - run (1)
- 4-1 port scanning technology
- 攻防演练中的防守基石——全方位监控
- 数据链路层
- Notepad++--宏(记录操作过程)
猜你喜欢

Hangfire详解

Flume配置2——监控之Ganglia

「运维有小邓」日志分析工具使用越来越频繁的原因

CorelDRAW最新24.1.0.360版本更新介绍讲解

18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点

「运维有小邓」审核并分析文件和文件夹访问权限
![[compilation principle] type check](/img/fc/458871e2df4e0384f65e09faa909d7.png)
[compilation principle] type check

Liunx instruction

Flume configuration 3 - interceptor filtering

Etcd database source code analysis - put process of server
随机推荐
雪花id,分布式唯一id
Bigder: Automation Test Engineer
mapbox-gl开发教程(十二):加载面图层数据
. NETCORE unified authentication authorization learning - run (1)
Hangfire details
软件工程—原理、方法与应用
Flume theory
Sentinel's quick start takes you through flow control in three minutes
18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点
2022年深圳市福田区支持先进制造业发展若干措施
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
thinkphp5中的配置如何使用
Regular expression series of mobile phone numbers
. NETCORE unified authentication authorization learning - first authorization (2)
jfinal中如何使用过滤器监控Druid监听SQL执行?
SSH command and instructions
Flume configuration 3 - interceptor filtering
「运维有小邓」审核并分析文件和文件夹访问权限
XSS漏洞
60天远程办公经验分享 | 社区征文