stm32cubemx 按键控制流水灯
时间: 2024-12-28 15:16:00 浏览: 4
### 使用STM32CubeMX实现按键控制流水灯功能
#### 配置环境与创建项目
为了使用STM32CubeMX配置并生成基于HAL库的代码框架,需先完成软件安装以及项目的初步设置。这包括但不限于安装STM32CubeMX工具本身及其配套的HAL库文件[^1]。
#### 初始化外设配置
通过图形界面选择合适的微控制器型号,并启用必要的外设模块来支持LED和按键的操作。对于本案例而言,主要涉及GPIO端口作为输出驱动LED指示灯,同时另一组GPIO被设定成输入模式用来检测按键状态变化。
#### 编写主程序逻辑
下面展示了一段简化版的C语言源码片段,展示了如何利用定时器中断配合按键扫描机制实现动态改变多个LED的状态切换效果:
```c
/* Includes --*/
static void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
uint8_t led_state = 0;
while (1)
{
if(HAL_GPIO_ReadPin(KEY_BUTTON_PORT, KEY_BUTTON_PIN) == GPIO_PIN_RESET){
// 检测到按键按下事件
__NOP(); // 延迟消抖处理
if(led_state >= 3){
led_state = 0;
}else{
led_state++;
}
switch(led_state){
case 0:
HAL_GPIO_WritePin(LED_RED_PORT, LED_RED_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_BLUE_PORT, LED_BLUE_PIN, GPIO_PIN_RESET);
break;
case 1:
HAL_GPIO_WritePin(LED_RED_PORT, LED_RED_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_BLUE_PORT, LED_BLUE_PIN, GPIO_PIN_RESET);
break;
case 2:
HAL_GPIO_WritePin(LED_RED_PORT, LED_RED_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_BLUE_PORT, LED_BLUE_PIN, GPIO_PIN_SET);
break;
default:
break;
}
HAL_Delay(200); // 设置适当延时防止连续触发
}
}
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 72000000
* HCLK(Hz) = 72000000
* AHB Prescaler = 1
* APB1 Prescaler = 2
* APB2 Prescaler = 1
* HSE Frequency(Hz) = 8000000
* PREDIV = 1
* PLLMUL = 9
* Flash Latency(WS) = 2
* @param None
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**Initializes the CPU, AHB and APB busses clocks
*/
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.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses 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();
}
}
```
这段代码实现了当特定按键被按下的时候,在三个不同颜色之间循环点亮对应的RGB LED灯泡的效果。这里假设`KEY_BUTTON_PORT`, `KEY_BUTTON_PIN`, `LED_RED_PORT`, `LED_RED_PIN`, `LED_GREEN_PORT`, 和 `LED_GREEN_PIN` 已经按照实际硬件连接情况定义好相应的宏常量值[^2]。
阅读全文