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.

Problems when Doing a succession of ADC conversions over UART

Other Parts Discussed in Thread: MSP430G2553

Hai,

In ccs5.5 code not compiling .I get this code from http://dbindner.freeshell.org/msp430/adc_g2553.html

and edited a little.here is the code please point out the problem.

Is it possible to use itoa in ccs and stdlib.h or string.h like c headder files?

#include "msp430g2553.h"
//#include <string.h>
//#include <stdlib.h>
char hello[] = " \r\n \r\n \r\n \r\n \r\n \r\n";
int tx_count = 0;

//unsigned int adc_result[6];
unsigned int adc_result[6] = { 7, 8, 9, 10, 11, 12 };
void uart_init( void ) {
/**
* 1. Set UCSWRST
* 2. Initialize all USCI registers
* 3. Configure ports
* 4. Clear UCSWRST
* 5. Enable interrupts via UCxRXIE and/or UCxTXIE
*/

// (1) Set state machine to the reset state
UCA0CTL1 = UCSWRST;

// (2) Initialize USCI registers
UCA0CTL1 |= UCSSEL_2; // CLK = SMCLK
UCA0BR0 = 104; // 1MHz/9600 =104
UCA0BR1 = 0x00; // from User Guide table 15-4
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1

// (3) Configure ports
P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
P1SEL2= BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD

// (4) Clear UCSWRST flag
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
}

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog timer

// UART settings depend on an accurate 1 MHz clock.
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

P1DIR &= ~BIT3; // Set P1.3 to input pin
P1IE |= BIT3; // P1.3 (S2) triggers an interrupt
P1IES |= BIT3; // on falling edge

// Initialize the UART
uart_init();


// Initialize the ADC10 using
// 2.5V reference, 16 clocks, on, multiple samples
ADC10CTL0 = SREF_1 | REF2_5V | REFON | ADC10SHT_2 | ADC10ON | MSC;

// input channel 5, trigger on ADC10SC bit, no clock division,
// internal ADC clock, sequence of conversions counting down from A5
ADC10CTL1 = INCH_5 | SHS_0 | ADC10DIV_0 | ADC10SSEL_0 | CONSEQ_1;

// We want samples of A5, A4, A3, and A0 but you have to take all 6.
ADC10DTC1 = 6;

// This is a funny register. It's called "analog enable" but really
// what it does is "digital disable". You can always sample from a pin,
// and this doesn't change that. What it does is disconnect any digital
// activity from the pin, so it can only work for ADC. Saves power.
ADC10AE0 = BIT5 + BIT4 + BIT0; // (all but A3 which we need for switch S2)

// Go to sleep. We'll do an ADC and send data when the switch S2 is pressed.
while( 1 ) {
__bis_SR_register( LPM3_bits + GIE );

ADC10SA = (unsigned int)adc_result; // destination address for DMA
ADC10CTL0 |= ENC + ADC10SC; // trigger the conversion

while( ADC10CTL1 & ADC10BUSY ) ;

itoa(adc_result[5], hello, 10 ); // convert A0 to a string
itoa(adc_result[4], hello+7, 10 ); // convert A1 to a string
itoa(adc_result[3], hello+14, 10 ); // convert A2 to a string
itoa(adc_result[2], hello+21, 10 ); // convert A3 to a string
itoa(adc_result[1], hello+28, 10 ); // convert A4 to a string
itoa(adc_result[0], hello+35, 10 ); // convert A5 to a string

IE2 |= UCA0TXIE; // enable interrupts to start the UART
}
}

// Switch S2 was pressed. Get things started.
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
int i, j;

P1IFG &= ~BIT3; // clear interrupt flag

P1IE &= ~BIT3; // disable this button

// blank out any previous values
for( i = 0; i < 6 ; i++ )
for( j = 0; j < 5; j++ )
hello[i*7 + j] = ' ';

__bic_SR_register_on_exit( LPM3_bits ); // wake up main program
}

// This routine does the sending. An interrupt is triggered each time
// the UART is ready to transmit the next character.
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
UCA0TXBUF = hello[ tx_count++ ]; // Load the next character
// and increment the count.

// After the 15 characters of the message are sent, we're done.
if( tx_count >= 15 )
{
IE2 &= ~UCA0TXIE; // disable this routine so it won't be called again
P1IE |= BIT3; // re-enable the S2 switch interrupt
tx_count = 0; // reset the count for the next transmission
}
}

////////////////////////////////////////

 the problems encountered

#10010 errors encountered during linking; "tesadc.out" not built tesadc C/C++ Problem
<a href="file:/C:/ti/ccsv5/tools/compiler/dmed/HTML/10234.html">#10234-D</a> unresolved symbols remain tesadc C/C++ Problem
unresolved symbol itoa, first referenced in ./main.obj tesadc C/C++ Problem
<a href="file:/C:/ti/ccsv5/tools/compiler/dmed/HTML/225.html">#225-D</a> function "itoa" declared implicitly main.c /tesadc line 77 C/C++ Problem

  • itoa()  function is not defined in ANSI-C and is not part of C++, but is supported by some compilers

    you can try use,

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    but first check iota function support in CCS documentation,

  • how can I check CCS support iota or not?

  • the simplest way will be checking this example

    #include "msp430g2553.h"
    #include <stdlib.h>
    #include <stdio.h>	
    
    
    int num = 123;
    char buf[5];
    
    void main() {
    
            WDTCTL = WDTPW + WDTHOLD;
    
    	// convert 123 to string [buf]
    	itoa(num, buf, 10);
    
            // set breakpoint heare       
    	while(1);
    }
    
    

    for convertion you can also use sprintf()  function

    http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

    this function is supported in CCS !

  • the above program shows errors

    the error is "unresolved symbol itoa"

  • ok, so you should used sprintf()

  • Hi,

    There's an alternative, use ltoa instead of itoa, but, in TI compiler, this exist only for C++, so create a .cpp and add using namespace std; so the previous example become:

    main.cpp

    #include "msp430g2553.h"
    #include <stdlib.h>
    #include <stdio.h>

    using namespace std;

    int num = 123;
    char buf[5];
    int main() {
            WDTCTL = WDTPW + WDTHOLD;
        // convert 123 to string [buf]
        ltoa(num, buf);
            // set breakpoint heare
        while(1);
    }

    Regards

  • For a fixed base (10, in this case), an old thread contains a fast and small implementation of an itoa algorithm by using subtractions.
    It does not even need a buffer space, as one put every digit to TXBUF as soon as determined.

    Sorry, I don’t have the link, but a clever forum search should reveal it.

  • your above modified code compiled  

    How can I use in my code 

    itoa(adc_result[5], hello, 10 ); // convert A0 to a string

    I replaced like this

    char adc_1[8];

    itoa(adc_result,adc_1);

    but errors

     

  • Hi,

    Please note that the function that I mentioned is ltoa, it converts a long to an array.

    Try this:

    long num;

    char adc_str[8]; // Check the number of characters necessry, maybe 8 is not enough

    // ... get value and store in num

    ltoa(num, sdc_str);

  • you can also use sprintf:

    #include "msp430g2553.h"
    #include <stdio.h>   
    
    int num = 123;
    char buf[5];
    
    void main() {
         WDTCTL = WDTPW + WDTHOLD;
    
        // convert 123 to string [buf]
        sprintf(buf, "%d", num);
    
        // set breakpoint heare      
        while(1);
    }

    or my procedure (integer into string conversion )

    #include "msp430g2553.h"
    #include <stdio.h>   
    
    int  num = 123;
    char buf[7];
    
    
    
    unsigned char ConvertInt2Str(char * result, int value) 
    {
       unsigned char    buff[7]; 
       
       int             vDiv   = 0;   
       unsigned char    minus  = 0;
       unsigned char    offset = 0, idx = 0;
    
       if(value < 0) 
       {
            minus = 1;
            value *= -1;
       } 
    
       do                                                               
       {                                                                  
           vDiv      =     (value / 10); 
           buff[idx ++]  = (value - vDiv* 10 + '0');
           
           value   =  vDiv;                                        
       }                                                                  
       while(vDiv);       
       
       
       if(minus == 1)     buff[idx ++] = '-';
      //else               buff[idx ++] = '+';   
       
       offset = idx;   
       while(idx --)     *(result ++) = buff[idx];    
       
       *(result) = 0;                                                                                                                       
       return    offset;                                                                                                           
     }
    
    void main() {
         WDTCTL = WDTPW + WDTHOLD;
    
        // convert 123 to string [buf]
          ConvertInt2Str(buf, num);
    
        // set breakpoint heare      
        while(1);
    }

**Attention** This is a public forum