目录
参考链接
基于STM32的串口收发讲解(标准库) - STM32团队 ST意法半导体中文论坛 (stmicroelectronics.cn)https://shequ.stmicroelectronics.cn/thread-637987-1-1.html【经验分享】STM32 HAL库之串口详细篇(基于HAL库) - STM32团队 ST意法半导体中文论坛 (stmicroelectronics.cn)https://shequ.stmicroelectronics.cn/thread-633088-1-1.html19. USART—串口通讯 — [野火]STM32 HAL库开发实战指南——基于F103系列开发板 文档 (embedfire.com)https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/doc/chapter20/chapter20.html
当前完整原理图框图
下图参照野火霸道开发构建。
电路分析:
1.Type-c的CC1和CC2分别都接了5.1K的下拉电阻,是为了兼容一些具备快充功能的适配器,告诉它输出5V,最大电流3A。
2.L1磁珠是为了抗干扰,过EMC用的,自己diy来说没看出有什么用。
3.CH340C相比CH340G来说内部集成了时钟,不用再外接晶振;如果RX和TX直接和MCU相连的话,手册中建议使用和MCU一样的供电,所以这里接的是和stm32一样的3.3V电源。
4.ISP一键下载电路我没想到有什么用处,用来下载程序的话,SW比这个方便多了,霸道教程也是一带而过,到时候用到的时候再来深入了解吧。需要注意的地方,霸道开发板原理图中,D47很有必要,它给C154提供了放电通道,有利于系统的稳定。
代码
main.c
/* 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"
#include "usart.h"
#include "gpio.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);
/* 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 */
/* USER CODE BEGIN 2 */
RGB_Init();
KEY_EXTI_Init();
DEBUG_USART1_Init(115200);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint16_t x,y,z;
uint32_t a,b,c;
x=1;
y=0xffff;
z=x-y;
printf("u16 x=%d,y=%d;\n" ,x,y);
printf("x-y=%d\n",z);
a=1;
b=0xffffffff;
c=a-b;
printf("u32 a=%d,b=%d;\n" ,a,b);
printf("a-b=%d\n",c);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(getchar()=='1') RGB_SetColour(RED);
else if(getchar()=='2') RGB_SetColour(GREEN);
else if(getchar()=='3') RGB_SetColour(BLUE);
else if(getchar()=='4') RGB_SetColour(YELLOW);
else if(getchar()=='5') RGB_SetColour(PURPLE);
else if(getchar()=='6') RGB_SetColour(CYAN);
else if(getchar()=='7') RGB_SetColour(WHITE);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_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 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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* 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 */
main.h
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "./key/bd_key.h"
#include "./rgb/bd_rgb.h"
#include "./debug/bd_usart1_debug.h"
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
bd_usart1_debug.c
#include "./debug/bd_usart1_debug.h"
UART_HandleTypeDef huart1;
void DEBUG_USART1_Init(uint32_t Baud)
{
char ch;
huart1.Instance = USART1;
huart1.Init.BaudRate = Baud;
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, (uint8_t *)&ch, 1);
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
// uint8_t ch;
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 15, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
//重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到串口DEBUG_USART */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000);
return (ch);
}
uint8_t R_CH=0;
//重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
static uint8_t r_ch=0xff;
if(r_ch != R_CH) return (R_CH);
return(0);
}
void USART1_IRQHandler(void)
{
if(__HAL_UART_GET_FLAG( &huart1, UART_FLAG_RXNE ) != RESET)
{
R_CH=( uint16_t)READ_REG(huart1.Instance->DR);
WRITE_REG(huart1.Instance->DR,R_CH);
}
}
bd_usart1_debug.h
#ifndef __USART1_DEBUG_H__
#define __USART1_DEBUG_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stdio.h>
extern UART_HandleTypeDef huart1;
/* DEBUG 操作函数 *****************************************************/
void DEBUG_USART1_Init(uint32_t Baud);
int fputc(int ch, FILE *f);
int fgetc(FILE *f);
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */