![](/img/trans.png)
[英]Blinking Led in Bare-Metal C programming: STM32L476RG Nucleo Board
[英]How to properly configure the USART_BRR register in STM32L476RG uC?
我正在尝试在 STM32L476RG Nucleo 板上编写自己的 USART_TX 驱动程序。 这里是数据表和参考手册。
我正在使用 Keil uVision 5,并在“管理”对话框中设置:
我想创建一个单字符发射器。 根据 Sec 中的手册说明。 40 p 1332 我写了这段代码:
// APB1 connects USART2
// The USART2 EN bit on APB1ENR1 is the 17th
// See alternate functions pins and label for USART2_TX! PA2 is the pin and AF7 (AFRL register) is the function to be set
#include "stm32l4xx.h" // Device header
#define MASK(x) ((uint32_t) (1<<(x)));
void USART2_Init(void);
void USART2_Wr(int ch);
void delayMs(int delay);
int main(void){
USART2_Init();
while(1){
USART2_Wr('A');
delayMs(100);
}
}
void USART2_Init(void){
RCC->APB1ENR1 |= MASK(17); // Enable USART2 on APB1
// we know that the pin that permits the USART2_TX is the PA2, so...
RCC->AHB2ENR |= MASK(0); // enable GPIOA
// Now, in GPIOA 2 put the AF7, which can be set by placing AF7=0111 in AFSEL2 (pin2 selected)
// AFR[0] refers to GPIOA_AFRL register
// Remember: each pin asks for 4 bits to define the alternate functions. see pg. 87
// of the datasheet
GPIOA->AFR[0] |= 0x700;
GPIOA->MODER &= ~MASK(4);// now ... we set the PA2 directly with moder as alternate function "10"
// USART Features -----------
//USART2->CR1 |=MASK(15); //OVER8=1
USART2->BRR = 0x683; //USARTDIV=16Mhz/9600?
//USART2->BRR = 0x1A1; //This one works!!!
USART2->CR1 |=MASK(0); //UE
USART2->CR1 |=MASK(3); //TE
}
void USART2_Wr(int ch){
//wait when TX buffer is empty
while(!(USART2->ISR & 0x80)) {} //when data is transfered in the register the ISR goes 0x80.
//then we lock the procedure in a while loop until it happens
USART2->TDR =(ch & 0xFF);
}
void delayMs(int delay){
int i;
for (; delay>0; delay--){
for (i=0; i<3195; i++);
}
}
现在,问题:
系统工作,但不正常。 我的意思是:如果我以 9600 波特率使用 RealTerm,如 USART_BRR reg 中的 0x683 所配置,它会显示错误的字符,但如果我将 2400 设置为实际波特率,它会起作用!
为了在 USART_BRR reg 中提取 0x683,我参考了Sec。 40.5.4 USART 波特率生成,如果 OVER8=0,USARTDIV=BRR。 就我而言,USARTDIV=16MHz/9600=1667d=683h。
我认为问题在于这个代码行:
USART2->BRR = 0x683; //USARTDIV=16Mhz/9600?
因为如果我将其替换为
USART2->BRR = 0x1A1; //USARTDIV=16Mhz/9600?
系统以 9600 波特率工作。
我的代码或 USARTDIV 计算理解有什么问题?
预先感谢您对我们的支持。
真诚的,通用汽车
USART 的默认时钟源是PCLK1
(图 15) PCLK1
是SYSCLK / AHB_PRESC / AHB1_PRESC
。 如果0x1A1
导致波特率为 9600,则表明PCLK1
= 4MHz。
当从内部 MSI RC 振荡器运行时,4MHz 恰好是您的处理器(和PCLK1
)在启动时的默认频率。 所以最可能的解释是您没有配置时钟树,并且没有像您认为的那样从 16MHz HSE 运行。
将时钟树配置为使用 16MHz 源,或对 MSI 频率执行计算。 MSI 精度在正常温度范围内几乎足够好,可以保持足够准确的波特率,但并不理想。
关于克利福德的回答(谢谢!)有没有一种简单的方法可以在启动之外更改SYSCLK
?
例如,通过对RCC_CR
寄存器中的MSIRANGE
位域进行编程?
谢谢你,通用汽车
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.