Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
As title goes,
I have
char temp = 128// this is the result of a A/D conversion from microcontroller and it could be a number between 0 and 1023.


I'm trying to send this value in temp through USART, which I have both functions


C#
void usart_pstr(char *s) {
    // loop through entire string
    while (*s) {
        usart_putchar(*s);
        s++;
    }
}
void usart_putchar(char data) {
	// Wait for empty transmit buffer
	while ( !(UCSR0A & (1<<UDRE0)) );
	// Start transmission
	UDR0 = data;
}


But on the receiving end , the serial port program only takes strings. So I'm wondering how do I convert "temp" into string "128\0" (instead of whatever 128 corresponds to in ASCII table) ?
Posted
Comments
Sergey Alexandrovich Kryukov 7-Jan-13 17:10pm    
The question is not "how"?, the first question is "why?". These days, way to many beginners tend to work with string representation of data instead of data itself... Does USART really require that? Well, bad USART... unfortunately, this would be a typical situation... Also, I'm not sure it requires characters byte by byte, not string representation of some integer of some size. Those are different things.
Anyway, what's the problem?
—SA
SandiegoSSD 7-Jan-13 17:15pm    
I used int (which it seems like I should for ADCW)
and itoa()

works now.
Receive 128 for 0.65V on the serial port
CPallini 8-Jan-13 3:11am    
Note if (as ytou stated) the ADC range is 0-1023 you shouldn't use one char to store this value (you need two of them).

Quote:
char temp = 128// this is the result of a A/D conversion from microcontroller and it could be a number between 0 and 1023.

I think you want

char temp[2] = { 128, 0};


Your comment is a little unsettling however. A 'char' as you have used here is signed and the range goes from -128 to +128. If you want a range from 0 to 255, use 'unsigned char'. If you want something that gives you 1023 or more values you probably want to use Unicode.
 
Share this answer
 
From the comments I know that you already found the itoa() function.

But with microcontrollers there is usually a limited amount of memory and execution time should be as short as possible. So it might be better to implement specific functions yourself rather than using the more general library functions. This example will produce strings with leading zeroes (e.g. '0128').
C++
// Convert unsigned decimal number to string with leading zeroes.
// Digits specifies the number of decimal digits to be printed
//  (e.g. 4 for values up to 1023).
// Size of buffer must be at least digits + 1.
// Avoids the usage of the % modulo operator for performance reasons.
char *utoa10(unsigned val, char *buffer, int digits)
{
    unsigned n;
    buffer[digits--] = 0;
    while (digits >= 0)
    {
        n = val / 10;
        buffer[digits--] = (char)(val - n * 10) + '0';
        val = n;
    }
    return buffer;
}

// Send ADC value in the range from 0 to 1023
void usart_adc(unsigned val)
{
    char buffer[5];
    utoa10(val, buffer, 4);
    usart_pstr(buffer);
}
 
Share this answer
 
Or in binary...
C++
short Voltage = 128;

usart_putchar(((unsigned char *) &Voltage)[0]);
usart_putchar(((unsigned char *) &Voltage)[1]);


Reading it back would work in a similar fashion, though you need to be aware that your processors could use different byte ordering. So you may need to swap the indexes on one side.
 
Share this answer
 
v2
Sorry, I don’t understand where is he problem.
Could you use?

C++
usart_putchar(tmp);
usart_putchar(0); // are you sure you need this?


directly, and not: usart_pstr(char *s)
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900