# How to return ADC_Read() as characters in PIC ? Using mikroC PRO?

We have a function:

unsigned int ADC_Read(unsigned char channel) {
if (channel > 7) //If Invalid channel selected
return 0; //Return 0
Delay_ms(2);
while (ADCON0.GO_DONE); //Wait for A/D Conversion to complete
{
}
}


It returns the binary value from ADRESH register as an Integer. But we need it as a character, since our Soft_UART_Write() function only accepts characters as parameters, and we need to print the values to the UART. So how can we make this function return characters? For example, if we get input as 01000010, it should return 'B' Refer here. I also tried functions like itoa(), but I get:

There is not enough ROM space adcuart.c

Since I'm using UART Library, there seems to be no space. So I'm looking for a direct or more efficient way to convert it.

• A char is 8bits. Ascii characters are 8 bits. The adc output is more than 8bits. There will be adc values that don't have a mapping to a character. If you can only use the most significant 8 bits of your adc then you can convert them to a char easily with a cast. – efox29 Feb 13 at 5:01
• @efox29 Thanks for your info, I read values only between 0 and 1023. Do you mean return (char)((ADRESH << 8) | ADRESL)? – Reejesh PK Feb 13 at 5:05
• No. Do you know how many bits your adc is? How many bits would be returned from that function? How many bits is a char? Do you see where I am going with this line of questioning? – efox29 Feb 13 at 5:08
• @efox29 yes, my ADC is a 10 bit one. So it doesn't fit into the character range? – Reejesh PK Feb 13 at 5:19
• yup thats right. So you have to either have to have some type of mapping where 2 characters = 10bit adc value or you only use 8bits of your 10 bit ADC and then character = 8bit adc value. – efox29 Feb 13 at 5:24

The itoa() function can be used but since it's not a part of neither native C nor C++, it may not be defined in your compiler. Anyway, here's how it's used:

#include <stdlib.h>
...


If itoa() is not defined in your compiler then you need to convert each digit to ASCII char:

char* myitoa(int num, char* str)
{
int i = 0;
char t;

// Handle 0 explicitely, otherwise empty string is printed for 0
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}

// Process individual digits
while (num != 0)
{
int rem = num % 10;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/10;
}

str[i] = '\0'; // Append string terminator

return str;
}


Another solution can be sprintf() function:

#include <stdio.h>

• Yea, I tried itoa, but i get the errro: There is not enough ROM space adcuart.c, since im using the UART Libarary also, so I'm trying to return it as a character itself so that i need not convert. – Reejesh PK Feb 13 at 5:00