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..!!