对于初学者,在ARM单片机上控制流水灯的亮灭是必经之路。控制流水灯的亮灭有很多方法,比如8个LED,可以控制8灯同时亮、灭;可以控制8灯依次亮、灭;可以控制8灯依次亮、灭,然后反方向再依次亮、灭;可以控制8灯依次点亮全部、然后依次熄灭全部;
实例使用的开发板是ST公司的NUCLEO-G474RE,使用的MCU型号STM32G474RET6,淘宝上有卖。
实例使用的配套扩展板:
实例所创建的工程简介:使用MCU的PB0~PB7依次连接扩展板的LED0~LED7,外部时钟源24MHz,系统时钟SYSCLK配置为170MHz,GPIO output lecel = High,GPIO mode = PP,GPIO Pull up/Pull down = Pull_up。工程名称:ex_led_ch2.ioc。其它,略。
当熟悉了HAL_GPIO_WritePin()函数的定义之后,就可以修改ex_led_ch2中的代码,用HAL_GPIO_WritePin()函数来实现对发光二极管亮灭的控制。主要通过修改main.c文件中while(1)循环的代码来实现。
修改后的代码如下:
- /* 8个灯同时亮灭 */
- HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED4_GPIO_Port,LED4_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED5_GPIO_Port,LED5_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED6_GPIO_Port,LED6_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED7_GPIO_Port,LED7_Pin,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(LED0_GPI0_Port,LED0_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED4_GPI0_Port,LED4_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED5_GPIO_Port,LED5_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED6_GPI0_Port,LED6_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED7_GPI0_Port,LED7_Pin,GPIO_PIN_SET);
- HAL_Delay(500);
当然也可以用HAL_GPIO_TogglePin()函数来实现。
- /* 8个灯同时亮灭 */
- HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
- HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
- HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
- HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);
- HAL_GPIO_TogglePin(LED4_GPIO_Port,LED4_Pin);
- HAL_GPIO_TogglePin(LED5_GPIO_Port,LED5_Pin);
- HAL_GPIO_TogglePin(LED6_GPIO_Port,LED6_Pin);
- HAL_GPIO_TogglePin(LED7_GPIO_Port,LED7_Pin);
- HAL_Delay(500);
编译、下载后,会发现8个发光二极管同时闪烁,并且闪烁周期与使用HAL_GPIO_Tog-lePin()函数时完全相同。
有多种方式可以实现让8个发光二极管按顾序依次闪烁。
假如还用HAL_GPIO_WritePin()实现对I/O输出状态的控制。要想实现上述功能,需顺次改变传递给HAL_GPIO_WritePin(GPIOx,GPIO_Pin,PinState)函数的GPIO_Pin(引脚号)参数就可以了。可以定义一个变量,譬如led_num作为引脚号,由于只需控制8个I/O,所以led_num只要8位就够了,于是可将led_num定义为8位无符号数(uint8_t)。当然,要实现顺次点亮发光二极管,变量led_num的8位中,在某一时刻只能有1位为1,以控其中一个I/O;到下一时刻,让该位移位,就可控制下一个I/O。led_num的初值可以为0x01。
定义好led_num以后,可以利用一个循环次数为8的for语句,将led_num传递给HAL_GPIO _WritePin(GPIOx,GPIO_Pin,PinState)函数的GPIO_Pin参数,调用延时函数,随后改变led_num的值(移位)。当然,在两次调用WritePin函数时,还要配合修改PinState参数,这样,就可以实现让灯依次闪烁的功能。
根据以上分析,修改while(1)循环中的代码如下:
- uint8_t led_num = 0x01;
- for(uint8_t i=0;i<8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_RESET)
- HAL_Delay(500); //延时500 ms
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_SET)
- HAL_Delay(500); //延时500 ms
- led_num <<= 1; //通过移位,改变led_num的值
- }
修改代码实现另一种闪烁效果:当第8个灯闪烁后,再反方向(从8到1)控制灯的闪烁。
- uint8_t led_num = 0x01;
- uint8_t led_num_reverse = 0x80;
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_SET);
- HAL_Delay(500);
- led_num <<= 1;
- }
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num_reverse,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(GPIOB,led_num_reverse,GPIO_PIN_SET);
- HAL_Delay(500);
- led_num_re >>= 1;
- }
进一步修改代码,实现让8个发光二极管依次点亮再依次熄灭的效果。
使用HAL_GPIO_TogglePin()函数,实现对I/O输出状态的控制。HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)函数有两个参数:一个是端口,由于使用的是GPIOB端口,所以可以不用改变;另一个是引脚号,可以尝试在改变引脚号上着手。
还是用变量led_num作为引脚号,并将其初值设为0x01。然后依然用一个循环次数为8的for语句,将led_num传递给HAL_GPIO_TogglePin()函数的GPIO_Pin参数,随后改变led_num的值(移位),再调用延时函数。这样就可以实现让发光二极管依次点亮再依次熄灭的流水灯效果。
修改while(1)循环中的代码:
- uint8_t led_num = 0x01;
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_TogglePin(GPIOB,led_num);
- led_num <<= 1;
- HAL_Delay(500);
- }
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 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"
-
- /* 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 */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(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();
- /* USER CODE BEGIN 2 */
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- /* 8个灯同时亮灭 */
- /* HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
- HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
- HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
- HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);
- HAL_GPIO_TogglePin(LED4_GPIO_Port,LED4_Pin);
- HAL_GPIO_TogglePin(LED5_GPIO_Port,LED5_Pin);
- HAL_GPIO_TogglePin(LED6_GPIO_Port,LED6_Pin);
- HAL_GPIO_TogglePin(LED7_GPIO_Port,LED7_Pin);
- HAL_Delay(1000); */
-
- /* 8个灯同时亮灭 */
- /* HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED4_GPIO_Port,LED4_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED5_GPIO_Port,LED5_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED6_GPIO_Port,LED6_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LED7_GPIO_Port,LED7_Pin,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(LED0_GPI0_Port,LED0_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED2_GPIO_Port,LED2_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED3_GPIO_Port,LED3_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED4_GPI0_Port,LED4_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED5_GPIO_Port,LED5_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED6_GPI0_Port,LED6_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED7_GPI0_Port,LED7_Pin,GPIO_PIN_SET);
- HAL_Delay(500); */
-
- /* 8个灯依次亮灭 */
- /* uint8_t led_num = 0x01;
- for(uint8_t i=0;i<8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_RESET);
- HAL_Delay(500); //延时500ms
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_SET);
- HAL_Delay(500); //延时500ms
- led_num <<= 1; //通过移位,改变led_num的值
- } */
-
- /* 循环左移,当第8个灯时,反方向右移,以类类推 */
- /* uint8_t led_num = 0x01;
- uint8_t led_num_reverse = 0x80;
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(GPIOB,led_num,GPIO_PIN_SET);
- HAL_Delay(500);
- led_num <<= 1;
- }
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_WritePin(GPIOB,led_num_reverse,GPIO_PIN_RESET);
- HAL_Delay(500);
- HAL_GPIO_WritePin(GPIOB,led_num_reverse,GPIO_PIN_SET);
- HAL_Delay(500);
- led_num_reverse >>= 1;
- } */
-
- /* 8个LED依次点亮再依次熄灭 */
- uint8_t led_num = 0x01;
- for(uint8_t i=0;i < 8;i++)
- {
- HAL_GPIO_TogglePin(GPIOB,led_num);
- led_num <<= 1;
- HAL_Delay(500);
- }
-
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
-
- /** Configure the main internal regulator output voltage
- */
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
-
- /** 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.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;
- RCC_OscInitStruct.PLL.PLLN = 85;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- 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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* USER CODE BEGIN MX_GPIO_Init_1 */
- /* USER CODE END MX_GPIO_Init_1 */
-
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOF_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, LED0_Pin|LED1_Pin|LED2_Pin|LED3_Pin
- |LED4_Pin|LED5_Pin|LED6_Pin|LED7_Pin, GPIO_PIN_SET);
-
- /*Configure GPIO pins : LED0_Pin LED1_Pin LED2_Pin LED4_Pin
- LED5_Pin LED6_Pin LED7_Pin */
- GPIO_InitStruct.Pin = LED0_Pin|LED1_Pin|LED2_Pin|LED4_Pin
- |LED5_Pin|LED6_Pin|LED7_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- /*Configure GPIO pin : LED3_Pin */
- GPIO_InitStruct.Pin = LED3_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(LED3_GPIO_Port, &GPIO_InitStruct);
-
- /* USER CODE BEGIN MX_GPIO_Init_2 */
- /* USER CODE END MX_GPIO_Init_2 */
- }
-
- /* USER CODE BEGIN 4 */
-
- /* 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 */