Jump to content

Leaderboard


Popular Content

Showing content with the highest reputation on 06/08/2018 in all areas

  1. 1 point
    A simple well documented hardware uart "Hello World" example. Updated, thanks for member comments 3/13/13 Notes: This code is for launchpad rev 1.5 This is hardware UART, your launchpad jumpers must be set for hardware UART The TI TUSB3410 is a TERRIBLE usb-> UART chip and is very buggy on WIN7 64bit. If your still having issues, it could be a driver issue. Try on XP or use a different USB -> serial device. //Nate Zimmer UART example // Press button to print hello to terminal #include <msp430g2553.h> // System define for the micro that I am using #define RXD BIT1 //Check your launchpad rev to make sure this is the case. Set jumpers to hardware uart. #define TXD BIT2 // TXD with respect to what your sending to the computer. Sent data will appear on this line #define BUTTON BIT3 void UART_TX(char * tx_data); // Function Prototype for TX void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop Watch dog timer BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz DCOCTL = CALDCO_1MHZ; P1DIR &=~BUTTON; // Ensure button is input (sets a 0 in P1DIR register at location BIT3) P1OUT |= BUTTON; // Enables pullup resistor on button P1REN |= BUTTON; P1SEL = RXD + TXD ; // Select TX and RX functionality for P1.1 & P1.2 P1SEL2 = RXD + TXD ; // UCA0CTL1 |= UCSSEL_2; // Have USCI use System Master Clock: AKA core clk 1MHz UCA0BR0 = 104; // 1MHz 9600, see user manual UCA0BR1 = 0; // UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; // Start USCI state machine while(1) // While 1 is equal to 1 (forever) { if(!((P1IN & BUTTON)==BUTTON)) // Was button pressed? { UART_TX("Hello World! \r\n"); // If yes, Transmit message & drink beer __delay_cycles(100000); //Debounce button so signal is not sent multiple times } } } void UART_TX(char * tx_data) // Define a function which accepts a character pointer to an array { unsigned int i=0; while(tx_data[i]) // Increment through array, look for null pointer (0) at end of string { while ((UCA0STAT & UCBUSY)); // Wait if line TX/RX module is busy with data UCA0TXBUF = tx_data[i]; // Send out element i of tx_data array on UART bus i++; // Increment variable for array address } }
  2. 1 point
    #include <msp430.h> #include <stdint.h> const unsigned RXD = BIT1; const unsigned TXD = BIT2; const unsigned SW2 = BIT3; const unsigned long smclk_freq = 16000000; // SMCLK frequency in hertz const unsigned long bps = 9600; // Async serial bit rate // Output char to UART static inline void putc(const unsigned c) { while(UCA0STAT & UCBUSY); UCA0TXBUF = c; } // Output string to UART void puts(const char *s) { while(*s) putc(*s++); } // CR LF void crlf(void) { puts("\r\n"); } // Output binary array to UART void putb(const uint8_t *b, unsigned n) { do putc(*b++); while(--n); } // Print unsigned int void print_u(unsigned x) { static const unsigned dd[] = { 10000, 1000, 100, 10, 1 }; const unsigned *dp = dd; unsigned d; if(x) { while(x < *dp) ++dp; do { d = *dp++; char c = '0'; while(x >= d) ++c, x -= d; putc(c); } while(!(d & 1)); } else putc('0'); } // Print signed int void print_i(const int x) { if(x < 0) putc('-'); print_u((x < 0) ? -x : x); } // Print hex nibble static inline void puth(const unsigned x) { putc("0123456789ABCDEF"[x & 15]); } // Print hex byte void print_hb(const unsigned x) { puth(x >> 4); puth(x); } // Print hex word void print_hw(const unsigned x) { print_hb(x >> 8); print_hb(x); } void main(void) { WDTCTL = WDTPW + WDTHOLD; // No watchdog reset // DCOCTL = 0; // Run at 16 MHz BCSCTL1 = CALBC1_16MHZ; // DCOCTL = CALDCO_16MHZ; // // P1DIR = ~(RXD | SW2); // Setup I/O for UART and switch P1OUT = P1REN = RXD | SW2; // P1SEL = P1SEL2 = RXD | TXD; // // const unsigned long brd = (smclk_freq + (bps >> 1)) / bps; // Bit rate divisor UCA0CTL1 = UCSWRST; // Hold USCI in reset to allow configuration UCA0CTL0 = 0; // No parity, LSB first, 8 bits, one stop bit, UART (async) UCA0BR1 = (brd >> 12) & 0xFF; // High byte of whole divisor UCA0BR0 = (brd >> 4) & 0xFF; // Low byte of whole divisor UCA0MCTL = ((brd << 4) & 0xF0) | UCOS16; // Fractional divisor, oversampling mode UCA0CTL1 = UCSSEL_2; // Use SMCLK for bit rate generator, release reset for(; { // int n = 1; // do (P1IN & BIT3) ? --n : (n = 5000); while(n); do (P1IN & BIT3) ? n = 5000 : --n; while(n); puts("Poke me with a Stick\r\n"); // for(n = -5; n <= 5;) print_i(n++), putc(' '); crlf(); n = 0xDEAD; print_hw(n); putc(' '); // n += 0xE042; print_hw(n); crlf(); // crlf(); // } // } //
×