This post was last edited by lising on 2019-1-6 14:15 The NUCLEO-G071RB development board used in this experiment is provided by
https://www.stmcu.com.cn/index.p ... /261/layout/product . For more relevant information, please go to the query. This experiment includes two contents: one is to use the query method to detect the "USER" button and control the on and off of LD4; the other is to use the interrupt method. 1. Experimental resources 1. NUCLEO-G071RB development board; 2. Keil v5.25.2.0; 3. Keil.STM32G0xx_DFP.1.0.0; 4. STM32CubeMX v5.0.1; 5. stm32cube_fw_g0_v100; 2. Experimental process 1. Software installation. I won’t talk about the specific installation process of the above software. Keil and STM32CubeMX have been installed before. Since the microcontroller STM32G071RBT6 used in the NUCLEO-G071RB development board is a new generation of products from ST, Keil.STM32G0xx_DFP.1.0.0 and stm32cube_fw_g0_v100 must be installed before using Keil and STM32CubeMX. These two plug-ins can be installed directly in the application software or downloaded and installed. The final result is as shown below:
2. The first experiment - use the query method to detect the state of the user button (blue) "USER" and control the on and off of LD4. From the schematic diagram, it can be seen that "USER" is connected to PC13 through an external pull-up resistor. When the button is pressed and a low level is detected in PC13, LD4 is lit, otherwise LD4 is extinguished; LD4 is controlled by an N-MOS and the PA5 pin. When PA5 outputs a high level, the N-MOS is turned on and LD4 is lit, otherwise LD4 is extinguished;
3. Program implementation The main body of the program is generated by STM32CubeMX, which is convenient, fast and efficient. Here PA5 is configured as output mode, and the initial output is low level; PC13 is configured as input; the clock uses HSE. After processing these and simple settings, the code can be generated.
When the generated project file is opened by Keil, you will find that the port and clock have been configured. You don’t need to pay attention to them anymore. You just need to implement the target function in the main function. The following is the function code:
- /* USER CODE BEGIN WHILE */ while (1) { if(!HAL_GPIO_ReadPin(USER_KEY_GPIO_Port, USER_KEY_Pin)) { HAL_Delay(20); if(!HAL_GPIO_ReadPin(USER_KEY_GPIO_Port, USER_KEY_Pin)) { HAL_GPIO_WritePin(LD4_GPIO _Port, LD4_Pin, GPIO_PIN_SET); while(!HAL_GPIO_ReadPin(USER_KEY_GPIO_Port, USER_KEY_Pin)); key_cont++; if(key_cont==2) { key_cont=0; HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET); } } } /* USER CODE END WHILE */
复制代码4. The second experiment - the interrupt method detects the button status and controls the on and off of LD4. The interrupt function can save a lot of valuable resources and respond quickly. In fact, the user button (PC13) itself has the "GPIO_EXTI13" external interrupt function. This experiment uses its function of generating an interrupt on the falling edge.
5. Program implementation The following is the main code for the function implementation. Some comments have been added to the function according to my understanding. If there are errors caused by my ability level, please give me advice. a. Interrupt configuration function, which is initialized in the main function.
- /*External interrupt configuration*/ void EXTI4_15_IRQHandler_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /*Enable GPIOC clock*/ __HAL_RCC_GPIOC_CLK_ENABLE(); /* Falling edge external interrupt; No pull-up or pull-down; Set PC13 pin; Complete pin configuration; */ GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = USER_KEY_Pin; HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStructure); /* User key connected to PC13 port, that is, external interrupt "EXTI Line 4 to 15 Interrupts"; Set interrupt priority (0~3), here set to the highest "0"; The M0+ core does not support interrupt sub-priority, so it is set to "0" here; */ HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0,0); /* Enable PC13 pin interrupt */ HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); }
复制代码b. Interrupt handling function, which is ready to work at any time in stm32g0xx_it.c;
- /* USER CODE BEGIN 1 */ void EXTI4_15_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(USER_KEY_Pin); } /* USER CODE END 1 */
复制代码c. After the interrupt is generated, HAL_GPIO_EXTI_IRQHandler(USER_KEY_Pin) will call the HAL_GPIO_EXTI_Falling_Callback(GPIO_Pin) function, which is defined as a "weak function" (__weak void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)), when the function is rewritten, the newly written one will be called in, and the "weak function" will be ignored. The called function is also called the "callback function";
- /*PC13 pin interrupt callback function, called by the interrupt processing function EXTI4_15_IRQHandler()*/ void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin == USER_KEY_Pin)//Confirm that PC13 generates a falling edge pulse { HAL_GPIO_TogglePin(LD4_GPIO_Port, LD4_Pin);//LD4 port flips } }
复制代码3. Experimental results The results of the two experiments are as described above. 4. Experimental summary Through the study of the two small experimental processes, the NUCLEO-G071RB development board and STM32G071RBT6 microcontroller have gained some new understanding and knowledge. Next, we will do more experiments to learn. Some of the descriptions in this article may have some problems, so please give me some advice.
RM0444_STM32G0x1单片机参考手册.rar
(7.39 MB, downloads: 15)
This content was originally created by EEWORLD forum user lising. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source