This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SEND CHARACTERS THROUGH UART WITH MSP430F2252

Other Parts Discussed in Thread: MSP430F2252

Hellow, I want to communicate the MSP430F2252 with a GSM through UART. I'm using the printf function to do this, but I've read in several post you have to modify the putchar.c file or write your own putchar function. I haven't found the putchar.c file in the directory where I have the IAR installed, but in this post http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/63128.aspx#227027 I've found this c code: 

// putchar function to write standard output to serial port

#include "xstdio.h"
#include <processor_specific.h>

_STD_BEGIN

int (putchar)(int c)
{
   // **** BEGIN SPECIFIC CODE FOR ANSI TERMINAL ***** //
   //
   // For I/O with ANSI terminal (or equivalent), convert C
   // line end (\n) to carriage-return + linefeed combo (\r\n).
   //
   if (c == '\n') putchar('\r');
   //
   // (**** END SPECIFIC CODE FOR ANSI TERMINAL ***** //

   if (c != EOF)
   {
      while (!(UCA0IFG & UCTXIFG))
      {
         // keep waiting;
      }
      UCA0TXBUF = c;
   }
   return (c);
}

_STD_END


The problem is: I don't know if it is correct because another member of the comunity wrote that it isn't fully correct, besides this I don't have my board yet.


Thanks in advance, Biara.

  • Well, this putchar implementation handles EOF differently. However, the MSP doesn't implement a file system and streaming by default (there is no OS, only your code and what you include).

    So EOF has no meaning here. Also, the auto-conversion of LF into CR/LF is a bit strange here. EOF handling and linefeed expansion belongs to the fputch() implementation which acts differently based on the binary or text mode the stream was opened with.
    IMHO, putchar should just send what's been passed.

    In this case, the implementation is as simple as

    int putchar(int c)
    {
      while (!(UCA0IFG & UCTXIFG));
      UCA0TXBUF = c;
      return c;
    }

    However, this assumes an already correctly set-up USCI A0 (baudrate etc.).
    Also, on your board, the USCI used for the data transfer might be USCI A1.

  • Thanks for your reply Jens, I wrote my putchar function:

    #include "msp430f2252.h"
    #include "stdio.h"
    int (putchar)(int c)
    {
    while (!(UCA0TXIFG & IFG2));
    UCA0TXBUF = c;
    return c;
    }

    I added the putchar.c file to my project, but Have I to add as well a putchar.h file?, in other words: I have to write #include "putchar.h" in the printf.c document or the function is carried out by the stdio.h?

    Thanks, in advance, Biara.

  • The header file, when included, just tells the compiler that 'there is something out there that is defined like this...'.
    There is no conneciton between a header file and the C file whose content it describes.

    Since the definition of putchar matches the standard definition, it doesn't matter whether you include a.h files that repeats it or jsut use the one in stdio.h (if it is already there).

    If you don't use putchar directly in your code, but include an already compiled library that calls putchar (e.g. through printf), then you don't need a header file (or any other declaration) of putchar at all. The library has been already compiled witht he reference to 'some funciton called putchar' and the linker will link putchar. And hopefully the library's own verison of putchar is marked as a 'weak' reference (which means it is only used if there is no other reference with this name). Well, it should. :)

  • Thanks a lot for your replies Jens but I test my code but it doesn't work :`( , I can't see any wave at P3.4(UCA0TXD). I'm using a 32768Hz external watch crystal which works fine with timerA and timerB. Here is my code, only the part which belongs to the UART communication:

    MAIN

    #include  "sendstring.h"

    #include  "msp430f2252.h"

    void main(void){

      WDTCTL = WDTPW + WDTHOLD;      // Stop WDT

      P3SEL |= 0x30;                                 // P3.4,5 = USCI_A0 TXD/RXD; 

      UCA0CTL1 |= UCSSEL_1;                  // CLK = ACLK

      UCA0BR0 = 0x03;                             // 32kHz/9600 = 3.41

      UCA0BR1 = 0x00;                          

      UCA0MCTL = UCBRS_3;                  // modulation UCBRSx=3

      __bis_SR_register(GIE);

      sendstring(); 

    }

    SENDSTRING

    #include  "msp430f2252.h"

    void sendstring(void)

    {

      UCA0CTL1 &= ~UCSWRST;                   // Initialize USCI state machine

      printf ("AT+CPIN=\"4923\"\r");

    }

    PRINTF FUNCTION, (IN THE LIBRARY OF THE PROGRAM)

    /*                      - PRINTF.C -

     

       The ANSI "printf" function.

     

       $Revision: 38615 $

     

       Copyright 1986 - 1999 IAR Systems. All rights reserved.

    */

     #include "stdarg.h"

    #include "stdio.h"

    #include "icclbutl.h"

     static void put_one_char(char c, void *dummy)

    {

      putchar (c);

      (void)dummy;  /* Warning on this line OK (Optimized Away) */

    }

     int printf(const char *format, ...)                    /* Our main entry */

    {

      va_list ap;

      int nr_of_chars;

      va_start(ap, format);      /* Variable argument begin */

      nr_of_chars = _formatted_write(format, put_one_char, (void *) 0, ap);

      va_end(ap);                /* Variable argument end */

      return nr_of_chars;        /* According to ANSI */

    }

     MY PUTCHAR FUNCTION

    #include "msp430f2252.h"

    #include "stdio.h"

    int (putchar)(int c)

    {

      while ((UCA0TXIFG & IFG2)==0){

      }

      UCA0TXBUF = c;

      return c;

    }

    Thanks in advance, Biara.

  • Biara said:
    int (putchar)(int c)

    Why the brackets around putchar?

    If you put a breakpoint onto your putchar function, does it trigger?

    Are you sure the crystal is really oscillating? Even if so, I doubt it is already up and runnig when your code reaches the printf. It takes several to hundreds of ms to start up a watch crystal.
    Try outputting ACLK on a clock pin. Or use SMCLK for a test - it won't provide a proper baudrate, btu you can at least be sure that it is providing a clock signal to the USCI.

    Do you see anything on the TX pin when you just clear SWRST and then write something to UCA0TXBUF? Put this code, followed by a "while(1);", before the line that sets GIE.
    Since your code (at least the posted portion) does not use any itnerrupts, there is no need to set GIE anyway.

  • Why the brackets around putchar?

    You are right, there is no need to put them.

    If you put a breakpoint onto your putchar function, does it trigger?

    I haven't no idea how to manage a breakpoint properly because the fact that I'm a beginner, but I'll study it tomorrow.

    Are you sure the crystal is really oscillating? Even if so, I doubt it is already up and runnig when your code reaches the printf. It takes several to hundreds of ms to start up a watch crystal.

    Try outputting ACLK on a clock pin. Or use SMCLK for a test - it won't provide a proper baudrate, btu you can at least be sure that it is providing a clock signal to the USCI.

    I'm not in the lab at the moment, but I'll try it.

    Do you see anything on the TX pin when you just clear SWRST and then write something to UCA0TXBUF? Put this code, followed by a "while(1);", before the line that sets GIE.

    like this?

    UCA0MCTL = UCBRS_3; // modulation UCBRSx=3

    UCA0CTL1 &= UCSWRST;

    UCA0TXBUF = 'h';

    while(1);

    Since your code (at least the posted portion) does not use any itnerrupts, there is no need to set GIE anyway.

    yes, I need it in other portion.

    Thanks for your replies once more, there are very helpful.

  • I don't know what was happening, but it works fine now!

  • Biara said:
    like this?

    Almost.

    UCA0CTL1 &= ~UCSWRST;

    As soon as you did the write to TXBUF, the scope/logic analyzer should show the outgoing data on the TX pin.

    Note that as soon as you clear UCSWRST, UCTXIFG is set. (If UCTXIE and GIE are set too, this will immediately trigger the USCI ISR)

**Attention** This is a public forum