Electronic – how to convert bytes into ASCII in C language

cmicrocontrolleruart

I am receiving bytes from my UART as uint8. How do I convert these bytes into ASCII? I am using Atmel 6.0 and my microcontroller is ATMega16L.

EDIT

I am using an android phone to send and receive information to a Wifi module (RN-131) from Roving networks. The wifi module connects to the microcontroller (atmega16L) through UART. Whatever the wifi module receives from the phone is transmitted out its UART. Similarly, whatever is written to its UART is transmitted out through wifi.

Now, what I'm trying to do is send a string from the phone which gets converted into bytes. The code is shown below:

PrintWriter out = new PrintWriter(new BufferedWriter(new outputStreamWriter(socket.getOutputStream())), true);
out.println("Eclipse");

here, the 'BufferedWriter' – Constructs a new BufferedWriter, providing out with a buffer of 8192 bytes. and 'PrintWriter' – Constructs a new PrintWriter with out as its target writer. The parameter autoFlush determines if the print writer automatically flushes its contents to the target writer when a newline is encountered.

now to program the microcontroller, I'm using AVR studio 6.0

uint8_t data;
char arr[32];
data = UDR;
length = strlen(data);
itoa(data, arr, 10);

But what I receive is "50, 53, 52" even when I'm sending "Eclipse".

EDIT 20th June 2012

Here is my code till now. Note: the code is written in AVR Studio 6.0 so it looks a bit different to other codes written in AVR Studio 4.18.

/**************************************************
 *  Includes
 */
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "../../../../../../Program Files (x86)/Atmel/Atmel Studio 6.0/extensions/Atmel/AVRGCC/3.4.0.65/AVRToolchain/avr/include/avr/iom16.h"

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

/**************************************************
 *  Defines
 */
#define LED_PORT PORTA
#define RED_ON (PORTA | 0x01)
#define RED_OFF (PORTA & ~0x01)
#define YELLOW_ON (PORTA | 0x02)
#define YELLOW_OFF (PORTA & ~0x02)
#define GREEN_ON (PORTA | 0x04)
#define GREEN_OFF (PORTA & ~0x04)

/**************************************************
 *  Function Prototypes
 */
void initialize(void);                      
void time_display(void);

int main(void){
    initialize();

    while(1){
        //TODO:: Please write your application code

    }
}

void initialize(void){

    DDRA = 0x07; //PORTA pins 0,1,2 set as outputs
//  DDRD = 0x02; //PORTD pin 1 is output (TX) and pin 0 is input (RX)

    //Timer SETUP
    TCCR1A = 0x80; //OCR1A enable in CTC mode
    TCCR1B = 0x0C; //CTC mode, 256 prescale 
    TIMSK = 0x10; //CTC on OCR1A enabled, overflow disabled
    OCR1A = 2000; //OCR value set to get an interrupt every 1 second (15624)

    //USART SETUP
    UCSRA = 0x00; //no Frame Error, no parity error, no Data OverRun
    UCSRB = 0xE0; //receiver enable, transmitter enable (0x18)
    UCSRC = 0x8E; //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
    UBRRH = 0;

    sei(); //Global interrupt enable
}

//timer capture interrupt
ISR(TIMER1_COMPA_vect){

}

ISR(USART_RXC_vect){
    LED_PORT = RED_ON;
    _delay_ms(100);

    for(int x=0; x<5; x++){
        arr[x] = UDR;
    }

    LED_PORT = RED_OFF;
    _delay_ms(100);

    trigger = 1;
}

ISR(USART_UDRE_vect){
    if(trigger >= 1){
        LED_PORT = GREEN_ON;
        _delay_ms(200);
        LED_PORT = GREEN_OFF;
        _delay_ms(200);

        UDR = data; //transmit the data
        trigger = 0;
    }
}

Best Answer

**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?