0

I have a microcontroller and I am sampling the values of an LM335 temperature sensor. The LCD library that I have allows me to display the hexadecimal value sampled by the 10-bit ADC. 10bit ADC gives me values from 0x0000 to 0x03FF.

What I am having trouble is trying to convert the hexadecimal value to a format that can be understood by regular humans.

Any leads would be greatly appreciated, since I am completely lost on the issue.

user3147707
  • 3
  • 1
  • 3
  • This isn't clear. "Hex" is just a representation. Presumably your ADC just gives you an integer *value*? In which case, I'm not sure what the problem is here? – Oliver Charlesworth Dec 30 '13 at 22:14
  • The problem is trying to display the HEX representation to a decimal representation. I hope I make myself clear. – user3147707 Dec 30 '13 at 22:18
  • What I'm saying is, unless your ADC gave you a string, you don't have a *representation*, you have a *value* (in an `int` or something). There is no "conversion from hex" required. You now need to look at the documentation for your LCD to see how to drive it. I'd guess you send it a sequence of ASCII values corresponding to a string you want to display, in which case you just need to create a string (using `sprintf` or similar). – Oliver Charlesworth Dec 30 '13 at 22:20
  • Got it, I was confused but not I understand it. Thanks – user3147707 Dec 30 '13 at 22:51

4 Answers4

1

You could create a "string" into which you construct the decimal number like this (constants depend on what size the value actually, I presume 0-255, whether You want it to be null-terminated, etc.):

char result[4];
char i = 3;
do {

    result[i] = '0' + value % 10;
    value /= 10;
    i--;
}
while (value > 0);
Roman Hocke
  • 4,137
  • 1
  • 20
  • 34
0

Basically, your problem is how to split a number into decimal digits so you can use your LCD library and send one digit to each cell.

If your LCD is based on 7-segment cells, then you need to output a value from 0 to 9 for each digit, not an ASCII code. The solution by @Roman Hocke is fine for this, provided that you don't add '0' to value % 10

Another way to split a number into digits is to convert it into BCD. For that, there is an algorithm named "double dabble" which allows you to convert your number into BCD without using divisions nor module operations, which can be nice if your microcontroller has no provision for division operation, or this is slower than you need.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
0

"Double dable" algorithm sounds perfect for microcontrollers without provision for the division operation. However, a quick oversight of such algorithm in the Wikipedia shows that it uses dynamic memory, which seems to be worst than a routine for division. Of course, there must be an implementation out there that are not using calls to malloc() and friends.

0

Just to point out that Roman Hocke's snippet code has a little mistake. This version works ok for decimals in the range 0-255. It can be easily expand it to any range:

void dec2str(uint8_t val, char * res)
{
    uint8_t i = 2;
    do {
        res[i] = '0' + val % 10;
        val /= 10;
        i--;
    } while (val > 0);
    res[3] = 0;
}