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.

itoa implementation to print output to serial monitor through UART



Hi,

I am trying to print integer value to a serial monitor by converting first to a string using itoa() function but i am NOT  getting any output at the serial monitor. Can anyone suggest what is wrong with the code. My code is as below:

#include <msp430.h>
#include <math.h>
#include <stdio.h>
#include <string.h>


void UART_Init(void);
void OUTA_UART(char A);
void STR_puts(char *str);
void Serial_Write(void);

void CLOCK_Init(void);

void itoa(unsigned int, char*, int);

char result[10];
const unsigned int value = 1099;

void main(void)
{

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

CLOCK_Init(); // Initialize clocks for 16 MHz

UART_Init();

itoa(value, result, 10);


while(1)
{
Serial_Write();

__delay_cycles(10000);

}


}

void CLOCK_Init(void)
{
//if we want DCO=MCLK=SMCLK,then just configure DCOCTL and BCSCTL1
//16Mhz @ maximum device frequency
if (CALBC1_16MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_16MHZ; // Set range
DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation*/

__delay_cycles (80000000); // 5 sec delay to stabilize DCO (1 msec is enough)
}

void UART_Init(void)
{
P1SEL |= BIT1 + BIT2 ;                          // P1.1 = RXD, P1.2=TXD
P1SEL2 |= BIT1 + BIT2 ;                       // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2;                 // SMCLK
UCA0BR0 = 0x82;                                 // 16MHz 9600
UCA0BR1 = 0x06;                                   // 16MHz 9600
UCA0MCTL = 0x06;                                    // Modulation UCBRSx = 6
UCA0CTL1 &= ~UCSWRST;                         // **Initialize USCI state machine**
}

void OUTA_UART(char A)
{
do{
}
while(!(IFG2&UCA0TXIFG));
// send the data to the transmit buffer
UCA0TXBUF = A;
}

void STR_puts(char *str)
{

while(*str!= '\0')
OUTA_UART(*str++);

}


void Serial_Write(void)
{
OUTA_UART(0x20); // format the display and the output
OUTA_UART(0x3D);
OUTA_UART(0x20);
STR_puts(result);
OUTA_UART(0x0D);
OUTA_UART(0x0A);
}

void itoa(unsigned int value, char* result, int base)
{
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0';}

char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;

do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );

// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}

}

Thank you in advance..!!

  • AMIT,

    In the future please use the code formatting tool (it looks like a window with </> in it), I've done that for you this time.

    I assume you are using the Launchpad, in which case you likely have the RXD and TXD jumpers in the "Software UART" Configuration.  To set up as HW UART, all you need to do is rotat the jumpers 90*, ( from || shaped configuration to = ). There is a picture here: http://energia.nu/Serial.html

    I also had to change your code to:

    void itoa(unsigned int value, char* result, int base)
    {
    // check that the base if valid
    	if (base < 2 || base > 36) { *result = '\0';}
    	char* ptr = result;
    	char* ptr1 = result;
    	char* tmp_char;
    	int tmp_value;

  • Thank you for your reply and suggestion..!!

    I was not aware of code formatting tool but i will use it in future.

**Attention** This is a public forum