我无法在STM32L0
. 我正在使用硬件抽象层(HAL)。
我的初始化代码是:
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 300;
huart1.Init.WordLength = UART_WORDLENGTH_9B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_EVEN;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
huart1.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart1.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;
HAL_UART_Init(&huart1);
}
我通过 UART1 发送的字节是:
0 1 2 3 4 5 6 7 8
000x 68 0B 0B 68 53 FD 52 FF FF .. etc.
0x68 = 0b01101000
0x0B = 0b00001011
0xFD = 0b11111101 <-- Character starting with 1, baudrate should be detected
0xFD :
start 1 1 .....
___ bit __________
¦______¦
...
为什么没有检测到波特率?我试过了:
UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT
和UART_ADVFEATURE_AUTOBAUDRATE_ONFALLINGEDGE
所以我调整了模式设置的顺序和驱动程序中的启用:
/* if required, configure auto Baud rate detection scheme */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
/* set auto Baudrate detection parameters if detection is enabled */
if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
}
}
到
/* if required, configure auto Baud rate detection scheme */
if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart-
/* set auto Baudrate detection parameters if detection is enabled */
if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
}
MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
}
什么都不做。
此外,以下似乎很好:
时钟源频率必须与预期的通信速度兼容(16 倍过采样时,波特率介于 fCK/65535 和 fCK/16 之间。8 倍过采样时,波特率介于 fCK/65535 和 fCK/8 之间)。
我过采样 16,所以
fCK= 16000000
fCK > 16000000 / 65535 = 244 = 244 Hz
fCK < 16000000 / 16 = 1000000 = 1 MHz
我选择的波特率是:19200/ 9600 /2400 /300