I want to calculate a voltage using ADC peripheral of PIC18F14K50. The result ranges between 0-1023 (10-bit). So I used this simple calculation:
uint16_t voltage = ADC_Result * 5000 / 1023;
However, the results are incorrect. I guess an arithmetic overflow happened. I tried many combination of parentheses, changing order of elements, etc.
The best result was 4088 when ADC_Result
was 1023 using below code; which is really far from 5000.
uint16_t voltage = ADC_Result * (5000 / 1023);
What should I do to get better results in above calculation? Please don't suggest floating points as they cause disaster in MCUs! They use a lot of resources without any real benefit.