Jump to content
43oh

G2553 Hardware UART "Hello World" Example


Recommended Posts

  • Replies 35
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

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

#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 frequenc

I had the same problem with the switched TX/RX pins on the launchpad. It took me a while to figure that one out...   I wrote my own library for the G2533, but should also work for the C2553. The ma

The routines by Fred and gwdeveloper are essentially the same, except for Fred's routine doing busy chacking before sending instead of after.

I do prefer for loops in this case, since you're using a counter with initialisation, condition and increment.

TX( char* string )
{
  int pointer;
  for( pointer = 0; string[pointer] != '\0' /* && pointer < 50 */; pointer++)
  {
    while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
    UCA0TXBUF = string[pointer];
  }
} 
Link to post
Share on other sites
void TX(char*s){do while(!(IFG2&UCA0TXIFG));while(UCA0TXBUF=*s++);}

something like this? :twisted:

void TX(char*s){int p;do while(!(IFG2&UCA0TXIFG));while(UCA0TXBUF=s[p++]);}

Or for the sake of unreadability, use upper case i and lower case L as variables:

void TX(char*l){int I;do while(!(IFG2&UCA0TXIFG));while(UCA0TXBUF=l[i++]);}
Link to post
Share on other sites

Pretty good. I particularly like the first one where I believe you modify the pointer that's passed in, potentially causing unexpected behaviour in code that follows. I've no idea if any of those would work or have serious bugs, so I'd call that a success!

Link to post
Share on other sites

No, it will work. C cannot pass anything by reference, so instad a pointer to the concerning data is passed. This pointer is a copy of the pointer used in the calling code. So now there are two pointers to the same string of characters in memory; one in the calling code and one in the local scope of the TX function. Since the local variable is destroyed (popped off of the stack or just abandon the value in the general purpose register) after TX returns, there is no harm done. So yeah, TX loses the start of the string pointer as it progresses over the string, but the calling code has its own version of that pointer, which is untouched and still points to the start of the string.

Link to post
Share on other sites

Hi i do have some issue using the TI hardware UART on launchpad. Somehow, i can't communicate at 115200 baud rate but 9600 baud works fine. What should i set for DCOCTL, BCSCTL1, UCA0CTL1, UCA0BR0 and UCA0BR1 if i want to have a system clock of 16MHz, hardware UART.

I am using RealTerm to communicate. Setting port to Baud 115200, no hardware flow control, 8N1

 

 Right now, my settings are as follows:

DCOCTL = CALDCO_16MHz;

BCSCTL1= CALBC1_16MHz;

UCA0CTL1 = UCSSEL_2;

UCA0BR0 = 125;

UCA0BR1 = 0;

UCA0MCTL = 0;

 

Thanks alot

 

 

Eric

Link to post
Share on other sites

I'm still waiting for oPossum to show us how it's really done.

 


#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();                                 //
    }                                           //
 }                                              //
 
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...