这两天在玩STM32L0,但是串口怎么调试都有问题。数据位选择8B,显示如下:
数据位选择7B时:
而我的输出应该是:printf("123456\n");printf("testing\n");
下面时我的程序:
//时钟初始化
void SystemClockConfig( void )
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
__HAL_RCC_SYSCFG_CLK_ENABLE( );
__HAL_RCC_PWR_CLK_ENABLE( );
__HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE1 );
// __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;//内部高速时钟8M 和 外部低速32.768K
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
if( HAL_RCC_OscConfig( &RCC_OscInitStruct ) != HAL_OK )
{
assert_param( FAIL );
}
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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if( HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_1 ) != HAL_OK )
{
assert_param( FAIL );
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_RTC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if( HAL_RCCEx_PeriphCLKConfig( &PeriphClkInit ) != HAL_OK )
{
assert_param( FAIL );
}
HAL_SYSTICK_Config( HAL_RCC_GetHCLKFreq( ) / 1000 );
HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK );
// SysTick_IRQn interrupt configuration
HAL_NVIC_SetPriority( SysTick_IRQn, 0, 0 );
}
UART.C
/******************************************************************************
* Copyright 2017-2030 DaPu Co.
* FileName: uart_new.c
* Desc:
*
*
* Author: CGH
* Date: 2017/10/12
* Notes:
*
* -----------------------------------------------------------------
* Histroy: v1.0 2017/10/12, CGH create this file
*
******************************************************************************/
/*------------------------------- Includes ----------------------------------*/
#include "board.h"
#include "uart_new.h"
#include "gpio.h"
#include "delay.h"
/*------------------- Global Definitions and Declarations -------------------*/
/*------------------------ Constant / Macro Definitions ---------------------*/
/*------------------------- Type Declarations -------------------------------*/
/*------------------------- Function Prototype ------------------------------*/
/*------------------------- Variable Declarations ---------------------------*/
UART_HandleTypeDef husart_debug;
uint8_t RxDatabuf;
/*------------------------- Function Implement ------------------------------*/
/**
* 函数功能: 串口硬件初始化配置
* 输入参数: huart:串口句柄类型指针
* 返 回 值: 无
* 说 明: 该函数被HAL库内部调用
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
Gpio_t TX,RX;
if(huart->Instance==DEBUG_USARTx)
{
/* 串口外设时钟使能 */
DEBUG_USART_RCC_CLK_ENABLE();
DEBUG_USARTx_GPIO_ClK_ENABLE();
/* 串口外设功能GPIO配置 */
//GpioInit( &TX, UART_TX, PIN_ALTERNATE_FCT, PIN_PUSH_PULL, PIN_PULL_UP, GPIO_AF4_USART1 );
//GpioInit( &RX, UART_RX, PIN_INPUT, PIN_PUSH_PULL, PIN_PULL_UP, GPIO_AF4_USART1 );
GPIO_InitStruct.Pin = DEBUG_USARTx_Tx_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_AF4_USART1;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
HAL_GPIO_Init(DEBUG_USARTx_Tx_GPIO, &GPIO_InitStruct);
GPIO_InitStruct.Pin = DEBUG_USARTx_Rx_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
HAL_GPIO_Init(DEBUG_USARTx_Rx_GPIO, &GPIO_InitStruct);
GpioWrite(&Led2,1);
Delay(3);
}
}
/**
* 函数功能: 串口硬件反初始化配置
* 输入参数: huart:串口句柄类型指针
* 返 回 值: 无
* 说 明: 该函数被HAL库内部调用
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==DEBUG_USARTx)
{
/* 串口外设时钟禁用 */
DEBUG_USART_RCC_CLK_DISABLE();
/* 串口外设功能GPIO配置 */
HAL_GPIO_DeInit(DEBUG_USARTx_Tx_GPIO, DEBUG_USARTx_Tx_GPIO_PIN);
HAL_GPIO_DeInit(DEBUG_USARTx_Rx_GPIO, DEBUG_USARTx_Rx_GPIO_PIN);
/* 串口中断禁用 */
HAL_NVIC_DisableIRQ(DEBUG_USART_IRQn);
}
}
/**
* 函数功能: 串口参数配置.
* 输入参数: 无
* 返 回 值: 无
* 说 明:无
*/
void MX_DEBUG_USART_Init(void)
{
int ret = -1;
__HAL_RCC_USART1_FORCE_RESET( );
__HAL_RCC_USART1_RELEASE_RESET( );
__HAL_RCC_USART1_CLK_ENABLE( );
/* 使能串口功能引脚GPIO时钟 */
DEBUG_USARTx_GPIO_ClK_ENABLE();
husart_debug.Instance = DEBUG_USARTx;
husart_debug.Init.BaudRate = DEBUG_USARTx_BAUDRATE;
husart_debug.Init.WordLength = UART_WORDLENGTH_8B;
husart_debug.Init.StopBits = UART_STOPBITS_1;
husart_debug.Init.Parity = UART_PARITY_NONE;
husart_debug.Init.Mode = UART_MODE_TX_RX;
husart_debug.Init.HwFlowCtl = UART_HWCONTROL_NONE;
husart_debug.Init.OverSampling = UART_OVERSAMPLING_16;
ret = HAL_UART_Init(&husart_debug);
if(ret != HAL_OK)
{
GpioWrite(&Led2,0);
}
else
{
GpioWrite(&Led3,1);
}
/* 串口中断起用 */
HAL_NVIC_SetPriority( USART1_IRQn, 1, 0 );
HAL_NVIC_EnableIRQ( USART1_IRQn );
/* Enable the UART Data Register not empty Interrupt */
HAL_UART_Receive_IT( &husart_debug, &RxDatabuf, 1 );
}
UART.H
/******************************************************************************
* Copyright 2017-2030 DaPu Co.
* FileName: uart_new.h
* Desc:
*
*
* Author: CGH
* Date: 2017/10/12
* Notes:
*
* -----------------------------------------------------------------
* Histroy: v1.0 2017/10/12, CGH create this file
*
******************************************************************************/
#ifndef UART_NEW_H
#define UART_NEW_H
/*------------------------------- Includes ----------------------------------*/
#include "stm32l0xx_hal.h"
#include
/*----------------------------- Global Defines ------------------------------*/
#define DEBUG_USARTx USART1
#define DEBUG_USARTx_BAUDRATE 9600
#define DEBUG_USART_RCC_CLK_ENABLE() __HAL_RCC_USART1_FORCE_RESET();\\
__HAL_RCC_USART1_RELEASE_RESET();\\
__HAL_RCC_USART1_CLK_ENABLE()
#define DEBUG_USART_RCC_CLK_DISABLE() __HAL_RCC_USART1_CLK_DISABLE()
#define DEBUG_USARTx_GPIO_ClK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define DEBUG_USARTx_Tx_GPIO_PIN GPIO_PIN_9
#define DEBUG_USARTx_Tx_GPIO GPIOA
#define DEBUG_USARTx_Rx_GPIO_PIN GPIO_PIN_10
#define DEBUG_USARTx_Rx_GPIO GPIOA
#define DEBUG_USART_IRQn USART1_IRQn
/*----------------------------- Global Typedefs -----------------------------*/
/*----------------------------- External Variables --------------------------*/
extern UART_HandleTypeDef husart_debug;
/*------------------------ Global Function Prototypes -----------------------*/
extern void MX_DEBUG_USART_Init(void);
#endif
希望各路兄台帮忙看看!!!感谢不尽!