username 198 Posted March 14, 2013 Author Share Posted March 14, 2013 Howdy all, sorry I neglected this post. I updated the code for the rev 1.5 and made some improvements. My coding skills hopefully have improved abit over the last year. I'm avoiding interrupts & low power modes for simplicity. This code is meant for beginners gwdeveloper 1 Quote Link to post Share on other sites
roadrunner84 466 Posted March 14, 2013 Share Posted March 14, 2013 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]; } } Quote Link to post Share on other sites
Fred 453 Posted March 14, 2013 Share Posted March 14, 2013 So many ways to do the same thing. All good. Let's not get into a argument on coding style though! Anyone want to write an obfuscated version? You know the sort of thing. All crammed onto one line. Saves you 2 bytes and 10 microseconds but is bordering on unreadable and unmaintainable. gwdeveloper 1 Quote Link to post Share on other sites
roadrunner84 466 Posted March 14, 2013 Share Posted March 14, 2013 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++]);} Fred and gwdeveloper 2 Quote Link to post Share on other sites
Fred 453 Posted March 14, 2013 Share Posted March 14, 2013 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! Quote Link to post Share on other sites
gwdeveloper 275 Posted March 14, 2013 Share Posted March 14, 2013 LOL, no arguments here. That's what the community is here for, right? ...to confuse the heck out of each other sometimes. I'm still waiting for oPossum to show us how it's really done. :!!!: Quote Link to post Share on other sites
roadrunner84 466 Posted March 14, 2013 Share Posted March 14, 2013 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. Quote Link to post Share on other sites
mahojazz 0 Posted March 15, 2013 Share Posted March 15, 2013 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 Quote Link to post Share on other sites
roadrunner84 466 Posted March 15, 2013 Share Posted March 15, 2013 This is a known problem. It's not a problem in the chip itself, butg rather in the programmer part. If you have a FTDI TTL UART cable hanging around you could try and use that one instead, it should work then. Quote Link to post Share on other sites
oPossum 1,083 Posted March 15, 2013 Share Posted March 15, 2013 The MSP430 launchpad application UART is limited to 1200, 2400, 4800 or 9600 bps. It won't run at any other speeds. Quote Link to post Share on other sites
oPossum 1,083 Posted March 15, 2013 Share Posted March 15, 2013 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(); // } // } // dubnet, gwdeveloper, EngIP and 4 others 7 Quote Link to post Share on other sites
gwdeveloper 275 Posted March 15, 2013 Share Posted March 15, 2013 LMAO! I knew it. I've gotten better at reading your code. Man, that's great! Quote Link to post Share on other sites
username 198 Posted March 15, 2013 Author Share Posted March 15, 2013 Pretty darn awesome opossum! You by chance got a big archive somewhere of all your awesome msp430 code? Quote Link to post Share on other sites
mahojazz 0 Posted March 19, 2013 Share Posted March 19, 2013 thanks alot. Quote Link to post Share on other sites
yyrkoon 250 Posted March 20, 2013 Share Posted March 20, 2013 do (P1IN & BIT3) ? --n : (n = 5000); while(n); do (P1IN & BIT3) ? n = 5000 : --n; while(n) Down / Up Debounce ? Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.