1962 views|1 replies

2015

Posts

0

Resources
The OP
 

Understanding MSP430G2553 serial communication [Copy link]

The PC sends characters to the microcontroller and the microcontroller sends them back to the PC. It is such a simple code. 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
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 //_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 of all, let’s talk about the G2553 serial port. (1): The jumper cap on the hardware connection should be changed to HW instead of SW Note that if you look carefully on the board, 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 built-in download circuit of the board no longer supports it. An external serial port circuit must be connected. 2: Here is a simple process of serial port code: 1: Select the clock source you need, external crystal oscillator 32768 or internal DCO If you select 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 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. USCI logic is held in reset state. This means that after the microcontroller is reset, if this bit is 0, the serial port can work normally. It is 1, that is, it keeps 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: Configure the above After the control register is UCAxBR0, the following is the baud rate configuration: Baud rate control register 0 UCAxBR0 = (clock frequency/baud rate) The integer part of the times 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 interrupt and receive interrupt as needed, be sure not to forget to enable the general interrupt Final: Clear the UCSWRST bit. At this time, the serial port starts working. Let me explain below. The problem of correcting the clock 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 find that this is not the case. We can receive the following message: #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: the clock of this chip is calibrated at the factory, and the calibration data is stored in a certain section of the Flash, CAKBC1_1MHZ It 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 CAKBC1_1MHZ and compare it with 0xff. Do you understand now? You can verify it by single-step simulation.

This post is from Microcontroller MCU

Latest reply

Worth collecting, very detailed explanation, learning  Details Published on 2018-12-25 22:24
 

6366

Posts

4936

Resources
2
 
Worth collecting, very detailed explanation, learning
This post is from Microcontroller MCU
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list