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 distributed relatively evenly, and can be 01001010, which is 4A
. Note that the value of UXBR1 is not unique. The values
that have been 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 UART has the software reset function. Generally, this function needs to be turned off, that is, reset.
UCTL0 &= ~SWRST;
7. Finally, we need to turn on the interrupt. Of course, if you do not need to use interrupt transmission and reception but use query transmission and reception, then you do not need to turn on the interrupt.
The registers that need to be configured are as follows:
You can see that by default, the USART transmit interrupt and receive interrupt are disabled. In this example, we use the receive interrupt, so:
IE1 |= URXIE0;
At this point, the entire program is basically finished.
#include <msp430x14x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; UCLK = ACLK
UBR00 = 0x03; // 32k/9600 - 3.41
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART0 RX interrupt
// Mainloop
for (;;)
{
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0
}
}
// UART0 RX ISR will for exit from LPM3 in Mainloop
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
_BIC_SR_IRQ(LPM3_ bits); // Clear LPM3 bits from 0(SR)
}
|