MSP430F149 MCU implements uart data receiving interrupt[Copy link]
/******************************************************** Program function: MCU keeps sending data to PC, and displays ASCII characters corresponding to 0~127 on the screen------------------------------------------------------ Communication format: N.8.1, 9600 ------------------------------------------------------ Test instructions: Open the serial port debugging wizard, set the communication format correctly, and observe the screen **********************************************************/ #include
typedef unsigned char uchar; typedef unsigned int uint; extern void Delays(void); extern uchar GetChar(void); extern void PutChar(uchar c); extern void PutString(uchar *ptr); extern void InitUART(void); static uchar pstr = 'A'; /************************Main function************************ */ void main(void) { uchar *tishi = " MCU sends 0~127 to PC and the\ \n screen will display their corresponding\ \n ASCII code as follows:"; uchar value = 0; //uchar c; int i = 10; int j = 100; WDTCTL = WDTPW + WDTHOLD; // Turn off the dog InitUART(); _EINT(); // Turn on the global interrupt while(i--) { while (!(IFG1 & UTXIFG0)); TXBUF0 = value++; value &= 0x7f; // Ensure that value is less than 128 while (!(IFG1 & UTXIFG0)); TXBUF0 = '\n'; Delays(); } PutString(tishi); while(j--) { PutChar(pstr); / /if(IFG1 & URXIFG0) //If a character is received //c = RXBUF0; //PutChar(c); Delays(); } while(1) { Delays(); } } #include
typedef unsigned char uchar; typedef unsigned int uint; void Delays(void); uchar GetChar(void); void PutChar(uchar c); void PutString(uchar *ptr); void InitUART(void); /******************************************* Function name: GetChar Function: Send character to the development board Parameter: None Return value: char ********************************************/ uchar GetChar(void) { uchar c; while(URXIFG0 == 1); c = RXBUF0; return c; //Send the received character } /*********************************************** Function name: PutChar Function: Send character to the development board Parameter: uchar Return value: None **************************************************/ void PutChar(uchar c) { while (!(IFG1 & UTXIFG0)); TXBUF0 = c; //Send the received character } /******************************************* Function name: PutSting Function: Send a string to PC Parameter: None Return value: None **********************************************/ void PutString(uchar *ptr) { while(*ptr != '\0') { while (!(IFG1 & UTXIFG0)); // Is TX buffer free? TXBUF0 = *ptr++; // Send data } while (!(IFG1 & UTXIFG0)); TXBUF0 = '\n'; } /******************************************* Function name: Delays Function: Delay for a while Parameters: None Return value: None********************************************/ void Delays(void) { uchar i=20; uint j; while(i--) { j=2000; while(j--); } } /*********************************************** Function name: InitUART Function: Initialize UART port Parameters: None Return value: None********************************************/ void InitUART(void) { P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD ME1 |= URXE0 + UTXE0; // Enable USART0 T/RXD UCTL0 |= CHAR; // 8-bit character UTCTL0 |= SSEL0; // UCLK = ACLK UBR00 = 0x03; // 32k/9600 - 3.41 UBR10 = 0x00; // UMCTL0 = 0x4A; // Modulation UCTL0 &= ~SWRST; // Initialize USART state machine IE1 |= URXIE0; // Enable receive interrupt for USART0 } #pragma vector=UART0RX_VECTOR __interrupt void UART0_RXISR(void) { uchar *Pstring = " Receive Data :"; pstr = RXBUF0; PutString(Pstring); PutChar(pstr); }