Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 13


 void byte_to_ascii_hex(unsigned char value_to_convert, char converted_value[])
{
char upper_nibble;
char lower_nibble;


// Get each hex digit and convert it to a character code.

upper_nibble = (value_to_convert & 0xf0) >> 4;

if ((upper_nibble >= 0) && (upper_nibble <= 9))
upper_nibble += 48;
else
{
// The value is between 10 (a) and 15 (f).

upper_nibble += 87;
}
lower_nibble = value_to_convert & 0x0f;

if ((lower_nibble >=0) && (lower_nibble <= 9))
lower_nibble += 48;
else
{
// The value is between 10 (a) and 15 (f).

lower_nibble += 87;
}
converted_value[0] = upper_nibble;
converted_value[1] = lower_nibble;
}
The ascii_hex_to_byte function converts two ASCII hex bytes (upper_nibble
and lower_nibble) to the binary value they represent:

 ascii_hex_to_byte:


' Set converted_byte to the value represented by ASCII Hex
' characters upper_nibble and lower_nibble
' Set success = 1 on success, 0 on failure.

success = 1
Free download pdf