编写代码通过串口调试助手发送0、1、2、3、4给STM32F407单片机的USART1,单片机根据收到的数字,利用高级定时器输出不同占空比的PWM波形,控制LED的亮度; STM32单片机要用中断的方式接收串口调试助手发来的数据。利用两个按键分别能够增强、减弱LED亮度等级,最暗不再减弱,最亮不再增强,按键动作检测也要用中断方式。
时间: 2023-12-23 16:05:04 浏览: 169
stm32单片机通过USART1 收发数据.rar_STM32 串口 收发_STM32USART1_stm32 usart1_s
5星 · 资源好评率100%
好的,这是一个基于串口通信和中断的STM32F407单片机控制LED亮度的程序。以下是程序的主要框架:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#define LED_PIN GPIO_PIN_13
#define LED_PORT GPIOC
UART_HandleTypeDef huart1;
TIM_HandleTypeDef htim1;
uint8_t received_data = 0;
uint16_t pwm_value = 0;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM1_Init(void);
void increase_brightness(void);
void decrease_brightness(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
while (1)
{
// 根据接收到的数字设置PWM占空比
switch (received_data)
{
case '0':
pwm_value = 0;
break;
case '1':
pwm_value = 3277; // 10% 占空比
break;
case '2':
pwm_value = 6554; // 20% 占空比
break;
case '3':
pwm_value = 9831; // 30% 占空比
break;
case '4':
pwm_value = 13107; // 40% 占空比
break;
default:
break;
}
// 更新PWM占空比
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm_value);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// 接收到一个字节
if (huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, &received_data, 1);
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
// 检测到按键动作
if (GPIO_Pin == GPIO_PIN_0)
{
increase_brightness();
}
else if (GPIO_Pin == GPIO_PIN_1)
{
decrease_brightness();
}
}
void increase_brightness(void)
{
// 增加PWM占空比
if (pwm_value < 13107)
{
pwm_value += 3277; // 增加10% 占空比
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm_value);
}
}
void decrease_brightness(void)
{
// 减少PWM占空比
if (pwm_value > 0)
{
pwm_value -= 3277; // 减少10% 占空比
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm_value);
}
}
static void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim1.Instance = TIM1;
htim1.Init.Prescaler = 83;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_Base_Start(&htim1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart1, &received_data, 1);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
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);
/*Configure GPIO pins : PA0 PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
void Error_Handler(void)
{
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
```
该程序首先初始化GPIO、UART和TIM模块,然后进入while循环进行串口接收和PWM输出。在接收到一个字节时,程序会根据接收到的数字设置PWM占空比,然后更新PWM输出。在按下按键时,程序会检测到中断并执行相应的函数,增加或减少PWM占空比,从而控制LED亮度。
需要注意的是,需要在HAL_UART_RxCpltCallback和HAL_GPIO_EXTI_Callback中调用HAL_UART_Receive_IT和HAL_GPIO_Exti_IRQHandler来重新启动UART和GPIO中断。此外,需要在MX_TIM1_Init函数中设置PWM占空比的初始值为0。
希望这个程序能够对您有所帮助!
阅读全文