how to convert bytes into ASCII in C language

**EDIT 6/19/2012***********************************************************

Ok, so after you added your code I have made a few changes for you to try (and to understand!)

First of all, we have declared our character array volatile so that the compiler knows it may change unexpectedly from an interrupt. Likewise, we have created a variable to keep track of our index position in the array:

/**************************************************
 *  Global Variables
 */
int trigger=0, length=0;
int i=0, n=0;
uint8_t data;
volatile char arr[8];
volatile char arr_idx = 0;

Next, i have edited your UART initialization code based on the ATMega16L Datasheet and your comments:

//USART SETUP
UCSRA = 0x00; //no Frame Error, no parity error, no Data OverRun
UCSRB = 0xD8; //receiver enable, transmitter enable (0x18)
UCSRC = 0x86; //Asynchronous USART mode, no parity, 1 stop bit, 8-bit character size, Data transmitted on rising edge and received on falling edge of the clock
UBRRL = 51; //Baud rate of 9600bps (8MHz clock?)
UBRRH = 0;

Look closely at the datasheet (page ~166). I believe I have correctly configures the UART now based on your comments. I will not go over each bit, but you should. Please ask questions if you think I made a mistake or you don't get something.

Finally, I have edited your ISR routine:

ISR(USART_RXC_vect){
    arr[arr_idx] = UDR;
    arr_idx++;
    arr_idx %= 7; //make sure index stays within array

    trigger = 1;
}

Note that, the ISR is executed every time a single byte (or character) is received. This means we want to process the single character (add it to our array), increment our index location, and get out of it quickly so that we are ready for the next character to come in.

You now need to look at the arr[] through whatever method you have been using and see if it is now receiving your characters correctly.

**END OF EDIT****************************************

One option is to just use one of the available libraries included in AVR Studio:

#include <stdlib.h>

and then somewhere in your code...

char num2[5]; //create a buffer for the ascii string
itoa(num1,num2,10); //convert the integer (num1) to an ascii string (num2) in base 10

If you are then displaying this on a character LCD, you can send the num2 array to the lcd print function that you are using.

If you are concerned about code space you can fairly easily write your own simple itoa (integer to ascii) function. You will need to grab each "tens" position from your integer using the "/" and "%" operators and convert it to ascii:

enter image description here
(source: asciitable.com)

From the table above, you can see that once you grab each digits value (0-9), just add 48 to it to convert it into the ascii character representation.

EDIT

Ok, so after you adding your information and assuming:

  • You have correctly initialized the UART baud rate etc on your ATMEGA
  • The android code is successfully sending the ascii bytes across the WiFly and out of its UART

Then the microcontroller should already be receive these characters as Ascii, and the ITOA function is not what is needed here and in fact would mess up your string data coming in.

It could still be a lot of different things though...How are you viewing the uart data that is being received by your microcontroller?


An ASCII character is simply an 8-bit number that is displayed as a character when you call functions like "printf" that are meant to display characters.

In C, a string is an array of these characters (or 8-bit numbers) that is terminated with the value 0 or "null-terminated".

So when you send a string like "Eclipse", you're actually sending numbers already. In fact, this is what it looks like when you send it:

[69, 99, 108, 105, 112, 115, 101, 0]

Passing this to printf, for example, will print "Eclipse". Note the null-terminator. Also note that you don't need any function to convert this.

itoa is a function used to convert numbers to their string representation. For example:

 char str[32];
 itoa(223, str, 10);
 printf("%s", str);

This would print out "223". The actual number values that reside in str[] that we know of for sure: [50, 50, 51, 0]. From your description, I'm pretty sure that this is not what you want.

Don't always assume ASCII encoding. Here's an extremely widely-used character encoding in use at the application level and web development: UTF-8 - These characters are not always 8-bits, some of them are 16-bits to represent characters in other languages for example.

EDIT:

There is something that you need to know about a typical UART peripheral on an AVR (not meant to be a blanket statement for all UART's on all uC's, just the family that you are using). They work like this: when a byte comes in, it gets put into a single-byte memory location: UDR - When you do a read from this register, it will be cleared. If new data is coming in on the UART and you haven't read from this register yet? It's going to get overwritten with the new value. Entire strings will not be placed here. Just single characters. All of this is described in the datasheet for your uC, I highly recommend getting familiar with it.

Hopefully, that will give you enough information to understand why strlen(data) won't work. If you're still not sure why, read over the datasheet, read up on what happens when you make this assignment in C data = UDR; and also read what strlen's parameters are, and note that one of them is a pointer to a string. If you don't have a good grasp on pointers, I highly suggest this: http://www.cplusplus.com/doc/tutorial/pointers/

If this all sounds like too much work to you, then I suggest picking up an Arduino instead. Don't get me wrong, it would be awesome if you could grasp all of this and be using plain C on your AVR, but if there are too many gaps in your knowledge at the moment, then it may be a bit overwhelming.

Now, on to what you want to accomplish. You mentioned in your comment that you wanted to loop until you get a full string out of the UART, storing it into a buffer, where you will then process it as a command. Ok. Totally do-able, in fact, a somewhat common technique.

Here's some psuedo code for you:

i = 0;
char str[32];

while(forever) {
   if (data_byte_is_available) {
        if (i == 31)
             i = 0;
        str[i] = UDR;
        i++;
   }
   print(str);
}

Try to figure out what you need to do for a data_byte_is_available function. The datasheet is your friend. Also, how are you displaying what is coming in on the uC's UART? Whatever that is, substitute the print I have up there with that. I hope there's no issues with whatever you're using there...