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.

Parents
  • 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

  • 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.

Reply
  • 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.

Children
No Data
Related