lulugl 发表于 2016-11-2 09:50:28

本帖最后由 lulugl 于 2016-11-2 09:51 编辑

BaudRate_Mantissa    = ((uint32_t)CLK_GetClockFreq() / (BaudRate << 4));
这个计算出: BaudRate_Mantissa = 主时钟/(波特率*16) 当于 主时钟/波特率 右移4位
(16M/9600*16)=104=0x68;
    BaudRate_Mantissa100 = (((uint32_t)CLK_GetClockFreq() * 100) / (BaudRate << 4));
BaudRate_Mantissa100 = 主时钟*1000/(波特率/16) 当于 主时钟/波特率 扩大1000 右移4位
16M*1000/(9600*16)=104166=0x196E6
UART1->BRR2 |= (uint8_t)((uint8_t)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100)) << 4) / 100) & (uint8_t)0x0F);
UART1->BRR2 |=((104166-(104*100))/16)/100) &0x0F =0x3A*0x0f=0x02
也就是第1位
    /* Set the MSB mantissa of UART1DIV*/
    UART1->BRR2 |= (uint8_t)((BaudRate_Mantissa >> 4) & (uint8_t)0xF0);
   UART1->BRR2 |=0x68/16 &0xF0 =0;
就是BBR2第4位:
UART1->BRR2=0x02
    /* Set the LSB mantissa of UART1DIV*/
    UART1->BRR1 |= (uint8_t)BaudRate_Mantissa;   得到第3、2位
UART1->BRR1=0x68;

终上所讲算得来的就是
BRR2=0x02;BRR1 = 0x68;

lulugl 发表于 2016-11-2 10:02:59

其实原理就是你输入主时频率频,预期波特率,给你换算出来而已

zbber 发表于 2016-11-2 10:41:18

时钟分频了
页: 1 [2]
查看完整版本: 关于STM8S库里面的这段UART通信的波特率设置代码问题请教