Pay attention to these MSP430G2553 serial communication issues
[Copy link]
I directly recorded every bit of code I learned from this code
: TI official routine: PC sends characters to the microcontroller, and then the microcontroller sends it back to the PC. It is such a simple code, but I actually... : Below is an explanation of the function of each statement one by one,
and the code is attached:
//**************************************************************************************
// MSP430G2xx3 Demo - USCI_A0, 9600 UART Echo ISR, DCO SMCLK
//
// Description: Echo a received character, RX ISR used. Normal mode is LPM0.
// USCI_A0 RX interrupt triggers TX Echo.
// Baud rate divider with 1MHz = 1MHz/9600 = ~104.2
// ACLK = n/a, MCLK = SMCLK = CALxxx_1MHZ = 1MHz
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.2/UCA0TXD|------------>
// | | 9600 - 8N1
// | P1.1/UCA0RXD|<---------------------
//
// D. Dang
// Texas Instruments Inc.
// February 2011
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//************************************************************************
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
if (CALBC1_1MH Z==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize US CI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
//_EINT();
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready ?
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
}
1: First, let's talk about some things that need to be paid attention to when using the G2553 serial port.
(1): Hardware connection. The jumper cap should be changed to HW instead of SW. Note that you can see the words SW and HW on the board if you look carefully.
(2): When using an external crystal oscillator 32768, the baud rate cannot exceed 9600. When using the internal DCO, it can be higher, but be careful that the register for baud rate calculation should not be less than 3. Remember, when the baud rate exceeds 9600, the board's built-in download circuit no longer supports it. An external serial port circuit must be connected.
2: Introduce a simple process of serial port code:
1: Select the clock source you need, external crystal oscillator 32768 or internal DCO. If you choose internal DCO, you need to verify whether the correction data is erased.
2: Configure the TX RX pins
3: Now start to configure the registers related to the serial port
First:
USCI_Ax Control Register G2553 has two serial port control registers UCAxCAL0 and UCAxCTL1. Generally, as a beginner, we don't need to worry about UCAxCTL0. The default is fine. We only need to configure the clock source selection and software reset enable in UCAxCTL1. Generally, ACLK or SMCLK can be selected. As for the software reset UCSWRST
UCSWRST: 0 is disabled. USCI reset is released for operation.
1 is enabled. USCI logic remains in reset state.
This means that after the microcontroller is reset, if this bit is 0, the serial port can work normally. If it is 1, it remains in reset state. After the reset state, this bit is 1, which means that if it is not cleared after reset, the serial port will not work.
Second:
After configuring the above control registers, the following is the baud rate configuration UCAxBR0 Baud rate control register 0
UCAxBR0 = (clock frequency / baud rate) integer part The number must be less than 0xff The excess is placed in UCAxBR1
Next:
Configure the baud rate adjuster: UCAxMCTL = (clock frequency/baud rate) fractional part * 8
Next_1:
At this time, whether to use the serial port to send interrupts and receive interrupts as needed, be sure not to forget to enable the general interrupt
Final: Clear the UCSWRST bit, and the serial port starts working
Now I will explain the problem of clock correction, which is the sentence below the watchdog
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
As novices, when we see CALBC1_1MHZ, we will naturally think that it is also a mask similar to BIT0. However, after searching the header file, we found that this is not the case.
We can receive a sentence like this:
#define CALDCO_1MHZ_ (0x10FEu) /* DCOCTL Calibration Data for 1MHz */
READ_ONLY DEFC( CALDCO_1MHZ , CALDCO_1MHZ_)
I will not say too much about this, just one thing: this chip has the clock calibrated before leaving the factory, and the calibration data is placed in a certain section of the Flash. This CAKBC1_1MHZ is defined as an address in the flash. If you accidentally erase the data in the Flash, all the data in it will become 0XFF So the meaning of this sentence is to take out the data in the flash CAKBC1_1MHZ and 0xff. You can verify it by single-step simulation. I will paste my simulation for reference below.
You can see that the operation of the assembly instruction is to take out the data in that address and compare it with 0XFF. Well, we will introduce it here. It is enough to know that the thing is an address. If you want to know more, you can read the following article which is very detailed http://blog.sina.com.cn/s/blog_a85e142101010myq.html. If
you are interested, you can simulate and see if the values in your two clock registers can correspond to the corresponding DCO selection frequency. I see the following figure 3 7. Just choose a 1MHZ clock as shown below:
I am a novice. The above description is bound to have some mistakes. Please forgive me. Welcome to discuss and share better learning materials and methods.
|