# [SOLVED]does ADRESH register of ADC in PIC MCU has hex value or decimal value?

Status
Not open for further replies.

#### Deexith Hasan

##### Advanced Member level 4
does ADRESH register of ADC in PIC18F4520 MCU has hex value or decimal value?

Staff member
It's binary.

Brian.

#### Deexith Hasan

##### Advanced Member level 4
so i have to do hex to decimal conversion before printing in serial port ?

#### Vbase

##### Full Member level 6
To read the ADC result you have to transmit to the serial port of your target (PC) 2 bytes ADRESH and ADRESL and in your target convert them to either decimal or hex.
MCU has simple brain, it can understand only binary. When we write a program with hex numbers the compiler converts them to binary so the MCU can understand them. Only people understand decimal or hex, most people who have 10 fingers are more comfortable with decimal system.

#### betwixt

##### Super Moderator
Staff member
You don't HAVE to convert to decimal before sending to a serial port, a serial port can handle binary perfectly well.

The problem comes when you want to display the value on the screen, some binary values have equivalent ASCII characters, some will be interpreted as control codes. If you want to send the binary value so it can be read on the screen you have to convert the value from binary to hex or decimal, whichever you want to display the value in. For example, to send a binary value as ASCII hex to the serial port you have to split the 8 bits into 4 upper bits and 4 lower bits, add 0x30 to each of the results, add an extra 7 if the result is > 0x39 then send the the two results one after the other to the serial port.

Brian.

#### Deexith Hasan

##### Advanced Member level 4
if adc value is 0xFF and i want to send this to serial port

unsigned char adc=ADRESH;
unsigned char lsb=adc%10;
adc=adc/10;
unsigned char mmb=adc%10;
unsigned char msb=adc/10;

lsb=0x30|lsb;
mmb=0x30|mmb;
msb=0x30|msb;

and lcdprint or serial print??

or i have to conver ADC value to decimal and then use the above code?

i have done adc value to serial port in proteus and got the result but in development board i received 000 .......i also send the adc value to the port which is connected to array of led the leds changed with respect to adc value but displayed 000 in serial port....

when i convert adcvalue from hex to dec and perform above code the protues shows the same result..... proteus sends adc value perfectly for both i.e when i convert ADRESH to ASCII directly and when i convert ADRESH to DEC and to ASCII.... y?

#### betwixt

##### Super Moderator
Staff member
Well, the first thing to bear in mind is that the serial port does not care what the value is, you can send any 8-bit data through it.

This is code I use to convert a 4-bit binary number to ASCII
Code:
//*******************************************************************************
//  convert a binary value to it's equivalent ascii hex character
unsigned char BinToAsc(unsigned char BinValue)
{
BinValue &= 0x0F;
if(BinValue > 9) BinValue += 7;
return(BinValue + '0');
}

As you can see, it's a function but you can work out the math it uses. For 8-bit values like the one you get from ADRESH, call it with:
SendToSerial(BinToAsc(ADRESH >> 4));
SendToSerial(BinToAsc(ADRESH));

Use your own serial routine instead of 'SendToSerial()'.

For sending an 8-bit binary value as decimal ASCII, try this:

Code:
Value = ADRESH;
Temp = Value / 100;
SendToSerial(Temp + '0');  // send hundreds

Value -= (Temp * 100);
Temp = Value / 10;
SendToSerial(Temp + '0'); // send tens

Value -= (Temp * 10);
SendToSerial(Value + '0');

You can also do it using string functions:
sprintf(ASCIIString,"%03d",ADRESH);
then send 'ASCIIString' to the serial port.

Brian.

Deexith Hasan

### Deexith Hasan

Points: 2
Helpful Answer Positive Rating
Status
Not open for further replies.