本帖最后由 灞波儿奔 于 2020-11-13 11:56 编辑
研究摸索了接近一个星期,把所遇到的问题写出来以供参考。
最开始看一个官方的串口收发数据的示例,然后自己操作后收发数据老是会丢失一个数据,然后看User’s Guide,取消了函数收发的方法直接用寄存器操作,问题解决。
接下来写一个组帧协议,收发完全正常。但是看上去非常的杂乱就想美化一下,就开始写函数,进行函数调用。好了,这下问题又来了,数据又会丢失。
接近一个星期的思前想后,发现不是自己写的代码有问题,是官方示例的波特率配置问题。大家都知道波特率表示每秒钟传送的码元符号的个数,是衡量数据传输速率的指标,它用单位时间内载波调制状态改变的次数来表示。晶振为1MHz波特率为15200,我自己改成了9600,问题解决。如何配置波特率在User’s Guide里面有想要的答案,详阅即可。MSP430FR2311在586页:
Setting a Baud Rate
For a given BRCLK clock source, the baud rate used determines the required division factor N:
N = fBRCLK/Baud Rate
The division factor N is often a noninteger value, thus, at least one divider and one modulator stage is
used to meet the factor as closely as possible.
If N is equal or greater than 16, it is recommended to use the oversampling baud-rate generation mode by
setting UCOS16.
NOTE: Baud Rate settings quick set up
To calculate the correct the correct settings for the baud rate generation, perform these
steps:
Calculate N = fBRCLK/Baud Rate [if N > 16 continue with step 3, otherwise with step 2]
OS16 = 0, UCBRx = INT(N) [continue with step 4]
OS16 = 1, UCBRx = INT(N/16), UCBRFx = INT([(N/16) – INT(N/16)] × 16)
UCBRSx can be found by looking up the fractional part of N ( = N - INT(N) ) in table
Table 22-4
If OS16 = 0 was chosen, a detailed error calculation is recommended to be performed
更改后代码如下:
//Configure UART
//SMCLK = 1MHz, Baudrate = 9600
//UCBRx = 6, UCBRFx = 8, UCBRSx = 0x20, UCOS16 = 1
EUSCI_A_UART_initParam param = {0};
param.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
param.clockPrescalar = 6;//UCBRx = 6
param.firstModReg = 8;//UCBRFx = 8
param.secondModReg = 0x20;//CBRSx = 0x20
param.parity = EUSCI_A_UART_NO_PARITY;
param.msborLsbFirst = EUSCI_A_UART_LSB_FIRST;
param.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT;
param.uartMode = EUSCI_A_UART_MODE;
param.overSampling = EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;//UCOS16 = 1;
|