使用stm32最小系统板实现呼吸灯
时间: 2023-09-27 11:11:19 浏览: 99
stm32最小系统板例程,实现LED延时闪烁,keil工程文件
呼吸灯是一种常见的LED灯效,其亮度会逐渐增强或减弱,仿佛在呼吸一样,给人一种柔和的感觉。使用STM32最小系统板可以很方便地实现呼吸灯效果。
首先,我们需要连接一个LED到STM32的GPIO引脚上。假设我们连接的是PA5引脚,代码如下:
```c
/* 定义GPIO引脚 */
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
/* 使能GPIO时钟 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* 配置GPIO引脚为推挽输出模式 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
```
接下来,我们使用定时器来控制LED的亮度。以TIM2为例,代码如下:
```c
/* 定义TIM2的预分频器和周期值 */
#define PWM_PRESCALER 999
#define PWM_PERIOD 999
/* 使能TIM2时钟 */
__HAL_RCC_TIM2_CLK_ENABLE();
/* 配置TIM2为PWM模式 */
TIM_HandleTypeDef htim2 = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = PWM_PRESCALER;
htim2.Init.Period = PWM_PERIOD;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&htim2);
/* 配置TIM2通道1为PWM输出 */
TIM_OC_InitTypeDef sConfigOC = {0};
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
/* 启动TIM2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
```
现在,我们可以在主循环中不断调整TIM2的占空比来实现呼吸灯效果。代码如下:
```c
while (1)
{
for (int i = 0; i <= PWM_PERIOD; i++)
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, i);
HAL_Delay(10);
}
for (int i = PWM_PERIOD; i >= 0; i--)
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, i);
HAL_Delay(10);
}
}
```
在上述代码中,我们使用HAL_Delay函数来停顿一段时间,以控制呼吸灯的速度。可以根据需要调整停顿时间来改变呼吸灯的速度。
完整代码如下:
```c
#include "main.h"
/* 定义GPIO引脚 */
#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA
/* 定义TIM2的预分频器和周期值 */
#define PWM_PRESCALER 999
#define PWM_PERIOD 999
/* 使能GPIO和TIM2时钟 */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
while (1)
{
for (int i = 0; i <= PWM_PERIOD; i++)
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, i);
HAL_Delay(10);
}
for (int i = PWM_PERIOD; i >= 0; i--)
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, i);
HAL_Delay(10);
}
}
}
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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
/* 使能GPIO时钟 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* 配置GPIO引脚为推挽输出模式 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
static void MX_TIM2_Init(void)
{
/* 使能TIM2时钟 */
__HAL_RCC_TIM2_CLK_ENABLE();
/* 配置TIM2为PWM模式 */
TIM_HandleTypeDef htim2 = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = PWM_PRESCALER;
htim2.Init.Period = PWM_PERIOD;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&htim2);
/* 配置TIM2通道1为PWM输出 */
TIM_OC_InitTypeDef sConfigOC = {0};
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
/* 启动TIM2 */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
}
```
阅读全文