Implementation of IO-simulated uart and Key Point Analysis for transplantation on ipvfg439

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

I recently want to develop on the MSP430, and I/O simulation on the first hand will be difficult. Fortunately, there are already many examples of the implementation of the soft serial port of the MSP430 on the Internet. Most of the Code in this article is referred
> Logging .... It took a week to debug the I/O simulation UART ....


Next we will paste the code 100% that can be used on ipvfg439 (even different models of the same family will have a difference ):

// Use Timer A as the serial port baud rate generator and capture and compare functions to implement asynchronous serial communication. <Br/> // chip model: msp430fg439 P1.0---TXD P1.1---RXD <br/> // timer_a taclk = aclk <br/> // The baud rate is 2400bit/s tbit1 = 14 tbit0_5 = 6 <br/> // frame format: (0) XXXXXXXX (1), 0 is the start bit, 1 is the second bit, and XXXXXXXXX is the 8-Bit Data <br/> # include <symbol xg43x. h> <br/> # define tbit1 14; // tbit1 is a one-digit time <br/> # define tbit0_5 6; // tbit0_5 is the half-digit time <br/> # define txd bit0 // use p1.0 as the sending function, compare the output with cci0a <br/> # define rxd bit1 // use p1.1 as the receiver. Use cci0b as the special feature to capture the input. <br/> unsigned int tr _ Count; // sending and receiving counter <br/> unsigned int t_data = 0x00; // sending buffer <br/> unsigned int r_data = 0x00; // receiving buffer <br/> void Init (void); // initialization <br/> void txd (unsigned char byte ); // send one byte of data <br/> void rxd (void ); // receives the preparation subroutine <br/> //***************************** *********************** <br/> // main function <br/> //* **************************************** * *********** <br/> main () <br/>{< br/> Init (); <br/> txd (0xff); // because the first frame is wrong Send useless frames <br/> for (INT I = 0; I <255; I ++) // send 0 ~ 255 of Data <br/> txd (I); <br/> while (1) <br/>{< br/> rxd (); // prepare to receive <br/> lpm3; // enter the sleep state and wake up after receiving the data <br/> txd (r_data ); // resend the received data <br/>}< br/> //***************** *********************************** <br/> // initialization function <br/> //***************************** * ********************** <br/> void Init (void) <br/>{< br/> wdtctl = wdtpw + wdthold; // stop wdt <br/> p1dir | = txd; // txd pin is output <br/> p1sel | = (rxd + txd); // rxd And txd are both special functional feet <br/> cctl0 | = out; // set 1 for ccr0 out <br/> tactl | = tassel_1 + mc_2; // set the timer A to the clock source aclk and the timer to the continuous mode <br/> _ Eint (); // enable interrupt <br/>}< br/> //************************ * *************************** <br/> // send one-byte data subroutine <br/> // input parameter: unsigned char byte <br/> //****************************** * ********************* <br/> void txd (unsigned char byte) <br/>{< br/> t_data = 255-byte; // one of the key points: This is different from the soft serial port routines of other MSP430. <br/> C Cr0 = tar; // Save the tar time to ccr0 to determine the first length. <Br/> ccr0 = ccr0 + tbit1; // Add each time period to ccr0. <Br/> // UART frame format: (0) XXXXXXXX (1) <br/> t_data = t_data <1; // shift one digit left <br/> t_data | = bit0; // The last digit is 1 <br/> tr_count = 10; // The sending counter. <Br/> cctl0 = outmod0 + CCIE + outmod2; // reset cctl0 (CCIS1-0 = 00) <br/> // capture/compare module output mode 1, sufficient module interruption. <Br/> while (CCIE & cctl0); // wait for CCIE to be 0? If the value is 0, the data is sent. <Br/>}< br/> //***************************** * ********************* <br/> // receives the preparation subroutine <br/>/dependency ta0 is interrupted to receive one byte of data. <Br/> //********************************** * ****************** <br/> void rxd (void) <br/> {<br/> tr_count = 8; // number of digits of the received data bit <br/> // one of the key points: the soft serial ports of other MSP430 use the descent edge (cm1 ), in practice, I found that the receiving problem exists here, so I use the rising edge of cm0 <br/> cctl0 = SCS + ccis0 + outmod0 + cm0 + cap + CCIE; <br/> // compare Capture: output mode + comparison capture module: interrupt allowed + "rising edge capture" + set to capture mode <br/> // + select cci0b as the capture Source + synchronous capture <br/>}< br/> //************************** **************************************** ****** * ****** <Br/> # pragma vector = timera0_vector // timer_a interrupt function <br/>__ interrupt void cc10int (void) <br/> {<br/> ccr0 = ccr0 + tbit1; // reload the next time (current time + 1 time) <br/> // --------------------------- receiving handler section <br/> If (cctl0 & ccis0) // is the receiving or sending status in progress? <Br/>{< br/> If (cctl0 & Cap) // is the capture mode or comparison mode? <Br/> {<br/> cctl0 & = ~ CAP; // yes-start capture and change the capture function to the comparison function <br/> ccr0 = ccr0 + tbit0_5; // start the capture bit plus a half time <br/>}< br/> else <br/>{< br/> r_data = r_data> 1; // In the comparison function, move the previous one to the lower position. <br/> If (cctl0 & SCCI) // This sentence is equivalent to (p1in & rxd) = 1, if the rxd pin is high <br/>{< br/> r_data | = bit7; // the top position of r_data is 1 <br/>}< br/> tr_count --; // The counter minus 1 <br/> If (tr_count = 0) // do you want to receive 8 digits? <Br/>{< br/> r_data = 255-r_data; // one of the key points, <br/> //**************************** * <br/> cctl0 & = ~ CCIE; // indicates the receipt is complete, and the capture/compare block stop interrupt is sufficient <br/> lpm3_exit; // exit the low-power mode (generally, it is used only when it enters lpm3) <br/>}< br/>}// end of receiving <br/> // else <br/> else // start the sending program segment <br/> {<br/> If (tr_count = 0) <br/> cctl0 & = ~ CCIE; // capture/compare block interruption, sending ends. <br/> else <br/> {<br/> If (t_data & 0x0001) // is the Status Register C bit 1 or 0? <Br/> cctl0 & = ~ Outmod2; // The C-bit of the Status Register is 0 and 0 is sent. <br/> else <br/> cctl0 | = outmod2; // The C bit of the Status Register is 1, and 1 is sent. <br/> t_data = t_data> 1; // shift the byte data one to the right <br/> -- tr_count; // The counter minus 1. <br/>}< br/> 

Because the online MSP430 routines are almost the same (all from Ti routines), and the general idea is not wrong (the setting varies between different models ), so next we will introduce the problems that beginners may encounter during transplantation:

1. I/O scripts with special functions are used here. Therefore, you must check the chip information to determine the IO on which the 430 function is used;

2. Emphasis: The serial frame format must be clear. Here, 10 bits are used: (0) XXXXXXXX (1), starting with 0, and ending with 1.

Many porting problems are caused by this:

(1) Since the sending starts from the low position, that is, starting from 1, and sending at the end of 0, xxxxxxx should be switched once (t_data = 255-byte ;);

(2) due to the unknown influence of other registers on txd, an error may occur when sending the first frame. Therefore, to determine whether the sending is successful, start from the second needle (txd (0xff ););

(3) When receiving, It is also vulnerable to the frame format .... The online routine uses a descent edge (cm1), but the first frame received by rxd is 1, and the last frame is 0 (as mentioned earlier ), therefore, the source data can be returned only when the rising edge of cm0 is changed to 1xxxxxxx0, and xxxxxxx needs to be switched once (r_data = 255-r_data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.