MSP430G2553 Serial Communication

Publisher:幸福之舞Latest update time:2018-07-14 Source: eefocusKeywords:MSP430G2553 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Code function: TI official routine: PC sends characters to MCU and then MCU sends them back to PC. It is such a simple code. I actually... The following explains the function of each statement one by one. 

Attach the code:

//*************************************************************************

//   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

//             -----------------

// /|\| PLEASE|-

//          | |                 |

//          --|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

int main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  if (CALBC1_1MHZ==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 USCI state machine**

  IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt

  //_UNITE();

  __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 you need to pay attention to when using the G2553 serial port.   

(1): Hardware connection. The jumper cap should be changed to HW instead of SW. Look carefully on the board and you can see the words SW and HW.

(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 that when the baud rate exceeds 9600, the board's built-in download circuit no longer supports it and 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 TX RX pins  

3: Now start configuring 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 novice, 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. The USCI logic is held in reset.

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 the 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 = integer part of (clock frequency/baud rate) The times must be less than 0xff and the extra ones are placed in UCAxBR1


Next:


Configure the baud rate adjuster: UCAxMCTL = (clock frequency/baud rate) fractional part * 8


Next_1:

At this time, according to the needs, whether to use the serial port to send interrupts and receive interrupts, be sure not to forget to enable the general interrupt  

Final: Clear the UCSWRST bit and the serial port starts working 


Now let me explain the problem of correcting the clock, which is the following sentence of 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 find that this is not the case.  

We can receive a passage like this:

#define CALDCO_1MHZ_ (0x10FEu) /* DCOCTL Calibration Data for 1MHz */
READ_ONLY DEFC( CALDCO_1MHZ , CALDCO_1MHZ_)

I won't say too much about this, just one thing: this chip is calibrated before leaving the factory, and the calibrated data is placed in a certain section of the Flash. 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 CAKBC1_1MHZ. The data in this flash is more similar to 0xff. You can verify it by single-step simulation. I will post 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 saw the following figure 3 7. Just choose the 1MHZ clock as shown below:


Keywords:MSP430G2553 Reference address:MSP430G2553 Serial Communication

Previous article:About the interrupt request flag
Next article:MSP430G2553 outputs ACLK and SMCLK

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号