stm32cubemx串口通信点灯
时间: 2023-08-24 13:09:58 浏览: 97
首先,你需要在 STM32CubeMX 中配置串口通信和 GPIO 端口。以下是基本步骤:
1. 在 Pinout & Configuration 选项卡中,选择需要使用的 GPIO 端口和管脚,将其配置为输出模式。
2. 在 Connectivity 选项卡中,选择需要使用的串口,配置其波特率、奇偶校验等参数。
3. 在 Code Generator 选项卡中,选择使用 HAL 库,生成代码。
接下来,你可以使用以下代码来实现串口通信和点灯:
```c
#include "stm32f4xx_hal.h"
#include <string.h>
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
uint8_t buffer[20];
uint8_t received[10];
uint8_t led_state = GPIO_PIN_RESET;
while (1)
{
if (HAL_UART_Receive(&huart2, received, 1, HAL_MAX_DELAY) == HAL_OK)
{
if (received[0] == '1')
{
led_state = GPIO_PIN_SET;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, led_state);
strcpy(buffer, "LED ON\r\n");
HAL_UART_Transmit(&huart2, buffer, strlen(buffer), HAL_MAX_DELAY);
}
else if (received[0] == '0')
{
led_state = GPIO_PIN_RESET;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, led_state);
strcpy(buffer, "LED OFF\r\n");
HAL_UART_Transmit(&huart2, buffer, strlen(buffer), HAL_MAX_DELAY);
}
}
}
}
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);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
*/
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();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
```
在此代码中,我们在主函数中使用了一个 while 循环来不断监听串口数据。如果接收到 '1',则点亮 LED 并发送 "LED ON" 消息;如果接收到 '0',则关闭 LED 并发送 "LED OFF" 消息。你可以通过串口调试工具向串口发送 '1' 或 '0' 来测试代码是否正常工作。
阅读全文