Detailed explanation of MSP430F149 serial port receiving and sending program
[Copy link]
The serial port receiving and sending program of MSP430 microcontroller involves multiple registers, which can be configured step by step according to the following steps:
1. First, set the used IO port to the second function. For example, if you want to use P3.4 and P3.5 as TXD and RXD of USART0, you have P3SEL |= BIT3+BIT4;
2. Enable the serial port receiving and sending function, and configure the following registers
If you want to send and receive, you need to set UTXE0 and URXE0 to 1, because they are not turned on by default.
There are two-bit masks in the header file of IAR:
#define UTXE0 (0x80)
#define USPIE0 (0x40)
Therefore, we can directly use the statement:
ME1 |= UTXE0 + URXE0;
3. Select the number of data bits per frame, and you need to configure the register:
The default is 7-bit data bits, so setting the CHAR position to 1 is 8-bit data bits.
UCTL0 |= CHAR;
4. Select the baud rate source. There are four baud rate sources for MSP430F149. The baud
rate division factor must be greater than 3. To select a suitable clock source, follow the following rules: When the baud rate is low (<9600), ACLK can be selected. The auxiliary clock is generally a 32768Hz Watch Oscilliater.
When the baud rate is high, the SMCLK subsystem clock should be used.
The following definitions are included in the IAR header file:
#define SSEL0 (0x10) /* Clock Source Select 0 */
Therefore, to select ACLK, you can use:
UTCTL0 |= SSEL0;
5. Configure the baud rate. This requires corresponding calculations based on the selected clock source:
First, it requires three registers UXBR0, UXBR1, and UXMCLK.
Then you need to figure out the division ratio:
BRCLK refers to the frequency of the clock source of the baud rate generator. For example, ACLK= 32768Hz is used in this example. Baude rate refers to the frequency we ultimately want to configure. For example, if 9600 is required in this example,
then N=32768/9600=3.41
UXBRO and UXBR1 are combined into a 16-bit register, which stores the integer part, so UXBR0=3, UXBR1=0;
UXMCLK is used to store the decimal part. The calculation of the decimal part is more complicated.
First, let the decimal part 0.41*8=5.08 Rounding to 5
means there should be 5 1s in the 8-bit register UXBR1. These 5 1s should be evenly distributed, so 01001010 is 4A
. Note that the value of UXBR1 is not unique. The values
calculated in the User's Guide are as follows:
These are common values. It is recommended to refer to them when using them to avoid the trouble of calculation.
6. Software clear reset flag bit
The register is as follows:
The first bit is used to determine whether the software reset UART function is enabled. Generally, this function needs to be disabled, that is, reset.
UCTL0 &= ~SWRST;
|