MSP430 Study Notes-usart

Source: Internet
Author: User

This article refers to: Http://bbs.ednchina.com/BLOG_ARTICLE_3013784.HTM

MSP430F149 has two usart communication ports, the performance is exactly the same, each communication port can be converted through the RS232 and RS485 chip, and the corresponding serial interface circuit communication. The msp430f149 supports serial asynchronous and synchronous communication, each with a separate frame format and independent control registers.



Usart Asynchronous communication
The MSP430 serial asynchronous communication mode is connected to the outside world via two pins: The Receive pin URXD and the transmit pin utxd. The asynchronous frame format consists of a starting bit, 7 or 8 data bits, a check digit (odd/even/none), 1 address bits, and 1 or 2 stop bits. Start sending and receiving from the lowest bit. In asynchronous mode, the transferred data is in characters.   in asynchronous mode, the Usart supports two multi-machine modes, the line idle and the address-bit multi-machine mode. (1) Line idle multi-machine mode:  in this mode, data blocks are separated by idle time. After the first stop bit of a character, more than 10 1 of the identity detects that the receive line is idle, and if a two-bit stop bit is used, the second stop bit is considered the first flag of the idle cycle. The first character after the idle period is the address character. When the received character is an address character, the Rxwake is placed and fed into the receive cache. (2) Address bit multi-machine mode    in this mode, the character contains an additional bit as the address flag. The first IgE character of a data block is provided with an address bit that indicates that the character is an address.   Baud rate generation:    in asynchronous serial communication, the baud rate is an important indicator, defined as the number of bits per second to transmit a binary digit. The baud rate reflects the speed of asynchronous serial communication. The baud rate is partly composed of clock input selection and crossover, baud rate generator, regulator and baud rate register. The following is a diagram of the baud rate generator:  divide factor N is determined by the frequency of the clock (BRCLK) sent to the divider counter and the desired baud rate to determine the:  n=brclk/baud rate divisor factor as defined below:  n=ubr+ (m7+m6+m5+m4+m3+ M2+M1+M0)/8  Interrupt: The Usart module has received and sent two separate interrupt sources. Use two separate interrupt vectors, one for receiving interrupt times and one for sending interrupt events. The interrupt control bit of the Usart module is in a special function register.  usart0
Special function Registers Receive interrupt control bit Send interrupt control bit
IFG1 Receive Interrupt flag URXIFG0 Send Interrupt Flag UTXIFG0
IE1 Receive interrupt Enable URXIE0 Send interrupt Enable UTXIE0
ME1 Receive Allow URXE0 Send Allow UTXE0
The msp430f149 has a USART0 and USART1 two communication hardware modules, so there are two sets of registers. The correct Usart module initialization should be in this order: (1) first set the serial port (2) in the case of the control bit swrst=1 (default) and then set the swrst=0; (3) Finally, if interrupt is required, set the corresponding interrupt enable routine: The MCU sends data to the PC and displays 0-127 of the corresponding ASCII characters on the screen. The data format has no parity, 8 bits of data, 1 stop bits, and a baud rate of 115200. #include "io430.h" #define UCHAR unsigned char#define uint unsigned intvoid initsys () {uint iq0;//Use XT2 oscillator Bcsctl1&amp ; =~xt2off;//Open XT2 oscillatordo {IFG1 &= ~OFIFG; //Clear oscillator failure flag For (iq0 = 0xFF; iq0 > 0; iq0--); //delay, wait for XT2 to start vibration }while ((IFG1 & OFIFG)! = 0); //Determine if the XT2 is vibrating BCSCTL2 =selm_2+sels; //Select MCLK, SMCLK for XT2, four frequency  }void delay (void), void putstring (Uchar *ptr), void main (void) { //Stop watchdog timer to prevent time out res et  Wdtctl = wdtpw + wdthold;  initsys ();  Uchar value=0; //The following is the serial port settings   P3SEL|=0XC0;                //p3.6,7 selected as USART0 transceiver   p3dir|=0x40;                //p3.6 for output   me2|=utxe1+urxe1;          //enable USART0 to receive and send   Uctl1|=char;                //8-bit  Utctl1|=ssel1;              //UCLK=SMCLK  ubr01=0x45;                //Set baud rate 32/2400=13.65  ubr11=0x00;  umctl1=0x049;  uctl1&=~swrst;              //initial USART0 state machine     while (1)   {   while (! IFG2&UTXIFG1));    //USART1 Send buffer ready    TXBUF1=value++;   value&=0x7f;                //guaranteed value is less than 128   while (! IFG2&UTXIFG1));   txbuf1= ' \ n ';                //send line breaks    delay ();   }} void putstring ( Uchar *ptr)          //Send string { while (*ptr! = ') to the PC           &N Bsp      //is not a string Terminator  {  while (!) IFG2&UTXIFG1));  txbuf1=*ptr++;                    //Send data  }}  void delay (void) {  UC Har i=20;  uint j;  while (i--)   {   j=2000;   while (j--); }  i Example 2:PC wants to MSP430 send a data, +1 in send back to PC#include "io430.h" #define UINT unsigned intvoid delay (), void Initsys () {uint iq0;//Use XT2 oscillator bcsctl1&=~xt2off;//Open XT2 oscillatordo {IFG1 &= ~OFIFG; //Clear oscillator failure flag For (iq0 = 0xFF; iq0 > 0; iq0--); //delay, wait for XT2 to start vibration }while ((IFG1 & OFIFG)! = 0); //Determine if the XT2 is vibrating BCSCTL2 =selm_2+sels; //select MCLK, smclk for XT } void main (void)  { //Stop watchdog timer to prevent time out reset  Wdtctl = WDTPW + Wdthold;&nbs P Initsys ();   //serial port settings   P3SEL|=BIT6+BIT7;      //p3.6,7=usart0 txd,rxd  p3dir|=bit6;  ME2 |=urxe1+utxe1; Enable to send and receive   Uctl1|=char;      //8-bit   u1tctl=ssel1;      //UCLK=SMCLK  u1br0 = 0x45;                            //baud rate 115200  U1BR1 = 0x00;                  //          UMCTL1 = 0x49;                            //modulation  UCTL1 &=~SWRST;    //usart module initialization    IE2 |=urxie1;      //can receive interrupts   for (;;)  {     __bis_sr_register (lpm0_bits+gie);   while (!) ( IFG2&UTXIFG1));         //USART1 send buffers ready    TXBUF1=RXBUF1+1;                //Send data    delay ();   }}  #pragma vector=uart1rx_vector__interrupt void Usart0_rx (void) { lpm0_exit;}  void delay (void) { unsigned i=50000; while (i!=0)  {i--;}}

MSP430 Study Notes-usart

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.