igendel icon

MSP430G2553 RX/TX demo

igendel | PRO | 03/12/21 11:34:34 PM UTC | 0 ⭐ | 1524 👁️ | Never ⏰ | []
C |

1.76 KB

|

None

|

0 👍

/

0 👎

#include <stdint.h>
#include <msp430.h> 
 
void setup(void) {
 
    // P1.0 (red LED) is output
    P1DIR |= 1;
 
    // Set DCO to 16 MHz
    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;
 
    // Clock output to pin P1.4 (for debugging)
    P1DIR |= 16;
    P1SEL = 16; // Alternate function (SMCLK) on P1.4
 
    // Select TX,RX functionality for P1.2, P1.1
    P1SEL  |= 6;
    P1SEL2 |= 6;
 
    // Initialize USCI
    UCA0CTL1 |= 0x80; // Use SMCLK
    // Baudrate 9600 (assuming 16MHz clock)
    UCA0BR0 = 131; // 1667 % 256
    UCA0BR1 = 6;   // 1667 / 256
    // Start
    UCA0CTL1 &= ~1;
 
    // Enable RX interrupt. TX interrupt should only be enabled when transmitting.
    IE2 = 1;
 
}
 
volatile uint8_t gotByte = 0;
volatile char *p;
char msg[6] = "Got x\n";
 
int main(void) {
 
    volatile uint32_t x = 0;
 
    WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
    setup();
 
    __enable_interrupt();
    
    for ( ;  ; ) {
 
 
        // Blink LED
        if (x == 250000) {
            P1OUT ^= 1;
            x = 0;
        } else {
            x += 1;
          }
 
        // Check RX data indication
        if (gotByte) {
 
            gotByte = 0;
            // Update the reply string
            msg[4] = UCA0RXBUF;
            p = msg;
            // Enable TX interrupt to start transmission
            IE2 |= 2;
 
        }
 
 
    }
 
    return 0;
 
}
 
#pragma vector=USCIAB0TX_VECTOR
__interrupt void TX_ISR(void) {
 
    if (*p) {
      // Send next char in string
      UCA0TXBUF = *p++;
    } else {
        // Turn off TX interrupt
        IE2 &= ~2;
      }
 
}
 
#pragma vector=USCIAB0RX_VECTOR
__interrupt void RX_ISR(void) {
    gotByte = 1;
    // Clear the interrupt flag manually, since we didn't read the buffer yet
    IFG2 &= ~1;
}
 
 

Comments