Another UART implementation of MSP430

Publisher:乐基儿vosLatest update time:2017-01-03 Source: eefocusKeywords:MSP430  UART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  1 #include "msp430g2553.h"

  2 

  3 typedef unsigned char uchar;

  4 typedef unsigned int  uint;

  5 

  6 #define LED     BIT0

  7 #define TXD     BIT1                            // TXD on P1.1

  8 #define RXD    BIT2                             // RXD on P1.2

  9 #define POUT    P1OUT

 10 

 11 #define BITTIME_1b 13*4 //1 bit width

 12 #define BITTIME_1b5 13*6 //1.5 bit width

 13 

 14 flying bitcnt;

 15 uint    uart_buf;

 16 int    Send_flag;

 17 

 18 uchar  *str=" Hello EEWorld!  \r";

 19 

 20 void FaultRoutine(void)

 21  {

 22 while(1); // Exception hang

 23  }

 24 

 25 void ConfigClocks(void)

 26  {

 27  if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)

 28    FaultRoutine();                            // If calibration data is erased

 29                                              // run FaultRoutine()

 30   BCSCTL1 = CALBC1_1MHZ;                     // Set range

 31   DCOCTL = CALDCO_1MHZ;                      // Set DCO step + modulation

 32   BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO

 33   IFG1 &= ~OFIFG;                           // Clear OSCFault flag

 34   BCSCTL2 = 0;                               // MCLK = DCO = SMCLK

 35  }

 36 

 37 void ConfigPins(void)

 38  {

 39 P1DIR |= TXD+LED;

 40   P1DIR &= ~RXD;                    // P1.3 input, other outputs

 41   P1OUT |= TXD;                                  // clear output pins

 42 

 43  }

 44 

 45 

 46 //Send a byte

 47 void send_char(uchar tchar)

 48 {

 49 TACTL = TACLR + TASSEL_2 + ID_3; //Select SMCLK clock; clear TAR

 50 CCR0 = BITTIME_1b5; //crr0 timing interval is 1 bit time width

 51 CCTL0 |= CCIE; //Open CCR0 interrupt

 52 bitcnt = 10; //Number of bits to be sent

 53   uart_buf = 0x0100;                     //8+N+1

 54   uart_buf |= tchar;                  //stop bit and start bit;

 55   uart_buf <<=1;

 56   Send_flag = 0;

 57   TACTL |= MC_1;                        //Start TA, UP mode.

 58 _BIS_SR(GIE);

 59   while(!Send_flag);                   //wait until send complete

 60   Send_flag = 1;

 61 }

 62 

 63 //Send a string

 64 void send_String(uchar *tstr)

 65 {

 66   while(*tstr)

 67     send_char(*tstr++);

 68 }

 69 

 70 

 71 void send_IRQ(void)

 72 {

 73   if(bitcnt>0)

 74   {

 75     if(uart_buf & 0x01)

 76       POUT |= TXD;

 77     else

 78       POUT &= ~TXD;

 79     uart_buf >>= 1;

 80     bitcnt--;

 81   }

 82   else

 83   {

 84     TACTL &= ~MC_3;                    //Close the TA when a Byte send over.   ??

 85 CCTL0 &= ~CCIE; //Disable CCR0 interrupt

 86     Send_flag = 1;

 87   }

 88 

 89 }

 90 

 91 

 92 

 93 void main( void )

 94 {

 95   // Stop watchdog timer to prevent time out reset

 96   uint i;

 97 

 98 

 99 

100   WDTCTL = WDTPW + WDTHOLD;

101   ConfigClocks();

102   ConfigPins();

103 

104 

105   while(1)

106   {

107    send_String(str);

108 

109    for(i=50000;i>0;i--);

110    for(i=50000;i>0;i--);

111    for(i=50000;i>0;i--);

112    for(i=50000;i>0;i--);

113 POUT ^= LED; // Flip LED

114   }

115 }

116 

117 // Timer A0 interrupt service routine

118 #pragma vector=TIMER0_A0_VECTOR

119 __interrupt void Timer_A (void)

120 {

121 

122 

123    send_IRQ();

124 }


Keywords:MSP430  UART Reference address:Another UART implementation of MSP430

Previous article:MSP430 MCU ADC analog-to-digital conversion operation
Next article:Temperature measurement and serial port transmission of Launchpad

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号