I have been sent this PM.
Hello
I saw your post about the voltmeter. I'm trying to understand the Math. I see that it works in the sim and in a chip but I don't understand how.
In the Math statement {IntAnswer=IntValue_read*5/(1023/10)} isn't the value of (1023/100) a real number and not a Int (10.23)?
I have been struggling with similar scaling and working really hard to keep all the math as only integers. I thought (apparently incorrectly)the answer to 1023/100 as a int would return an answer of 10 not 10.23 and so the math would not work out correctly.
If you can please explan why it work correctly that would be great
You are correct! (1023/100) is treated as a real number = 10
So here is how it's done:
Value of sampled Voltage = INT = 100 times sampleADC (0 -5V) or 300 times sampleADC (0 to 15V)
E.g on 0-15V range with 4.5V on i/p. Value of adc_level variable = 1023/5*4.5= 920
Converting to 3 times i/p voltage:
920 *5/(1023/100)*3 = 1380 Note: with pic maths, since using integers: 1023/100 = 10 and not 10.23. so I could of used voltage *5/10 * 3 = 1380 so get the same answer!
(Reason I used 1023/100 instead of just 10 was to show that max int read was 1023, so it is used in the calculation. If I just used 10, then people could be puzzled why I used it.)
Convert to a string = “1380”
Now use Left$ and Right$ to display Voltage.
I.e For Voltage whole part:
Voltage_Unit = Left$(Volts, 2) = 13
For the hundredths part:
Votlage_Hundreths = Right$(Volts, 2) = 80
Then send to display Voltage_Unit = 13
A decimal point
Then hundredths = 80
= 13.80 Volts
If you follow the flowchart, it can be seen that a lower voltage e.g less than 1V requires a different format of left$ and right$.
E.g again on 0 - 15V range:
with 0.12V on i/p. Value of adc_level variable = 1023/5*0.12= 24
Converting to 3 times i/p voltage:
24 *5/10*3 = 36
Convert to a string = “36”
Now use Left$ and Right$ to display Voltage.
I.e For Voltage whole part:
Voltage_Unit = "0"
For the hundredths part:
Votlage_Hundreths = Right$(Volts, 2) = 36
Then send to display Voltage_Unit = “0”
A decimal point
Then hundredths = “36”
= 0.36 Volts
Remember these are 3 times measured voltage since on 0 – 15V
So for last example approximate voltage on pin = 0.36/3= 0.12 There will be an error factor since not using floating point.