Advertisement
igendel

MSP430G2553 RX/TX demo

Mar 12th, 2021
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <msp430.h>
  3.  
  4. void setup(void) {
  5.  
  6.     // P1.0 (red LED) is output
  7.     P1DIR |= 1;
  8.  
  9.     // Set DCO to 16 MHz
  10.     BCSCTL1 = CALBC1_16MHZ;
  11.     DCOCTL = CALDCO_16MHZ;
  12.  
  13.     // Clock output to pin P1.4 (for debugging)
  14.     P1DIR |= 16;
  15.     P1SEL = 16; // Alternate function (SMCLK) on P1.4
  16.  
  17.     // Select TX,RX functionality for P1.2, P1.1
  18.     P1SEL  |= 6;
  19.     P1SEL2 |= 6;
  20.  
  21.     // Initialize USCI
  22.     UCA0CTL1 |= 0x80; // Use SMCLK
  23.     // Baudrate 9600 (assuming 16MHz clock)
  24.     UCA0BR0 = 131; // 1667 % 256
  25.     UCA0BR1 = 6;   // 1667 / 256
  26.     // Start
  27.     UCA0CTL1 &= ~1;
  28.  
  29.     // Enable RX interrupt. TX interrupt should only be enabled when transmitting.
  30.     IE2 = 1;
  31.  
  32. }
  33.  
  34. volatile uint8_t gotByte = 0;
  35. volatile char *p;
  36. char msg[6] = "Got x\n";
  37.  
  38. int main(void) {
  39.  
  40.     volatile uint32_t x = 0;
  41.  
  42.     WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
  43.     setup();
  44.  
  45.     __enable_interrupt();
  46.    
  47.     for ( ;  ; ) {
  48.  
  49.  
  50.         // Blink LED
  51.         if (x == 250000) {
  52.             P1OUT ^= 1;
  53.             x = 0;
  54.         } else {
  55.             x += 1;
  56.           }
  57.  
  58.         // Check RX data indication
  59.         if (gotByte) {
  60.  
  61.             gotByte = 0;
  62.             // Update the reply string
  63.             msg[4] = UCA0RXBUF;
  64.             p = msg;
  65.             // Enable TX interrupt to start transmission
  66.             IE2 |= 2;
  67.  
  68.         }
  69.  
  70.  
  71.     }
  72.  
  73.     return 0;
  74.  
  75. }
  76.  
  77. #pragma vector=USCIAB0TX_VECTOR
  78. __interrupt void TX_ISR(void) {
  79.  
  80.     if (*p) {
  81.       // Send next char in string
  82.       UCA0TXBUF = *p++;
  83.     } else {
  84.         // Turn off TX interrupt
  85.         IE2 &= ~2;
  86.       }
  87.  
  88. }
  89.  
  90. #pragma vector=USCIAB0RX_VECTOR
  91. __interrupt void RX_ISR(void) {
  92.     gotByte = 1;
  93.     // Clear the interrupt flag manually, since we didn't read the buffer yet
  94.     IFG2 &= ~1;
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement