Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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
 

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 7-Jan-13 11:00am
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Sorry, I don’t understand where is he problem.
Could you use?
 
usart_putchar(tmp);
usart_putchar(0); // are you sure you need this?
 
directly, and not: usart_pstr(char *s)
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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').
// 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);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Or in binary...
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.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 921
1 OriginalGriff 425
2 CPallini 255
3 George Jonsson 181
4 Richard Deeming 175
0 OriginalGriff 5,450
1 Sergey Alexandrovich Kryukov 4,532
2 CPallini 4,500
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100