This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Decimal Conversion

Hi, I have just managed to successfully read ADC values and send as a service but they are in uint8 format. Is there an easy way to convert this to decimal within my adc handler? Thank you for any response.

  • Hi Kosidinma,

    I don't know if this answers your question or not, but I'll try. We make measurements from many different sensor types typically using an ADC to make the measurement. The "raw" ADC reading is what we store and report to our host SW. However we frequently need to display the measurement on an LCD as well so we have to convert it to a human readable value, typically floating point to achieve the needed precision. So we run the uint8/uint16 etc. value through a polynomial equation with floating point coefficients with the end result being a 32 bit floating point value which is then displayed on the LCD.

    You could do something similar and change your service to report a 4 byte value instead of a single byte. You may need to modify the size of the characteristic in your service to do that.

    John

  • Thanks for your contribution...so do you think its better converting on the master device instead of on the nrf51822 chip?? The master device is an android phone by the way.

  • I think that is going to be dependent on what you need to do with the value. In our case, we store the ADC raw value for the master to read out when it wants. This is to save memory as a 12 or 16 bit "raw" ADC value takes less space than a 32 bit float. However on some devices we also have to convert it on the slave to display it for a user on an LCD. If you only need to see the converted value on the master, then by all means convert it there as you can save a few bytes of over the air transport.

  • There is no way to know what the ADC measurement really means, so there is no way to convert it into "decimal".

    All the ADC measurement is telling you is the voltage as measured at the pin, according to the control parameters.

    So for example if you are using a 1200 mV reference, and a 2/3 prescaler then the pin voltage is

    pin voltage = (1200 * 3/2 * ADC_VAL/256)

    The pin itself might the be fed through a voltage divider or there might be other losses (eg diode drop) that must be compensated for.

    Be careful if you do this in an interrupt service routine because doing division can be slow (if you are not careful).

  • I'd also suggest converting the value on the device with the most processing power. Converting an int to a float on a Cortex M0 requires at the least linking in a load of floating point library code and as the chip has no FPU, every multiplication and division, addition and subtraction, of floats is emulated slowly and painfully. It's instructive to profile a piece of Cortex M0 code which does floating point and see just how many cycles are used up in just one single such operation. It's normally between 10 to 100 times the number of cycles as for an integer multiplication and one integer multiplication can be up to 32 cycles (does the Nordic Cortext M0 have the single cycle multiplication option which guarantees 1 cycle for a 32bit integer multiply, I actually don't know?).

    So if you don't need the float on the device itself, send it as an integer and convert it on the client.

Related