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.

ADC temperature UART

Hi,

I'm trying to read the internal ADC temperature and send the data to UART. Problem is that if the temperature is 34 then it doesnt print the 34, but prints the corresponding ASCII character. Is ther any way around this problem? I tried using itoa() dunction but it doesnt work for me.

#include "msp430g2553.h"

const char data;
//int i=0;

void init_uart();
void init_led();
void send_byte(char data);
int read_ADC();

int main(void)
{
	WDTCTL = WDTPW + WDTHOLD; // Stop WDT
	int temp;

	init_uart();
	init_led();
	temp = read_ADC();

	send_byte(temp);


	__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ int until Byte RXed
	while(1);
}

void init_uart()
{
	P1SEL |= BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
	P1SEL2 |= BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD

	UCA0CTL1 |= UCSSEL_2;                     // SMCLK
   	UCA0BR0 = 104;                            // 1MHz 9600
    UCA0BR1 = 0;                              // 1MHz 9600
    UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1

	UCA0CTL1 &= ~UCSWRST;
}

void init_led()
{
	P1DIR |= BIT6 + BIT0;
	P1OUT &= ~(BIT6 + BIT0);
}

void send_byte(char data)
{
	UCA0TXBUF = data;			//send data
	while((UCA0STAT&UCBUSY)){}
}

int read_ADC()
{
	ADC10CTL0=SREF_1 + REFON + ADC10ON + ADC10SHT_3 ; //1.5V ref,Ref on,64 clocks for sample
	ADC10CTL1=INCH_10+ ADC10SSEL_3; //temp sensor is at 10 and clock/4

	int t=0;
	__delay_cycles(1000);              //wait 4 ref to settle
	ADC10CTL0 |= ENC + ADC10SC;      //enable conversion and start conversion
	while(ADC10CTL1 & BUSY);         //wait..i am converting..pum..pum..
	t=ADC10MEM;                       //store val in t
	ADC10CTL0&=~ENC;                     //disable adc conv
	return(int) ((t * 27069L - 18169625L) >> 16); //convert and pass
//	return t;
}

  • Ronak,

    I have used this often when transmitting data in this manner:

    char hexarray[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}

    Then in your send_byte function, it would look something like this:

    send_bute(char data)

    {

    UCA0TXBUF = hexarray[data >> 4];

    while((UCA0STAT&UCBUSY)){}

    UCA0TXBUF = hexarray[data & 0x0f];

    while((UCA0STAT&UCBUSY)){}

    }

     

    This will give the hex value over the UART. So if the ADC value is 0x31, it would transmit  31.

    i just typed this up on the fly, might be some syntax type in there, but should point you in the right direction.

    -Jason

     

**Attention** This is a public forum