Here is a longer way of looking at what is happening. Not as nice as Darrel's but it may help you understand a bit. This works for sensors that do not start on zero.
The old formula of
y = mx-b
y = output 'amps in your case
m = slope
x = input units
b = offset
You are wanting to read form 0 to 30 amps.
Start off by looking at the units as being volts. You have a 2 volt span. This means that every 1 volt input will equal 15 amps. Slope will equal 30 / 2 = 15..
m = 15
The offset. Starting at 2.5 volts. 2.5 volts = 0 amps. the offset is 2.5 * 15 = 37.5
b = 37.5
Plug all this into the formula and lets say the input is 4.5 volts.
y = (15 * 4.5) - 37.5
y = 30 amps
Now convert this to an 8 bit PIC resolution.
Like Darrel did I will assume a 0 to 5 volt reference.
2.5 volts = 127
At 8 bit resolution each volt is 51 steps.
Spanning 2 volts or 102 steps will have 4.5 volts equaling 229.
New value for m is 30 / 102 = 0.2941
m = 0.2941
New offset value is 127 * 0.2941 = 37.35
b = 37.35
y = (ADC * 0.2941) - 37.35
ADC = 229
y = 29.99 amps
Possible code...
Code:
ADC = 229
z = ADC * 2941 ' equals 673489
z1 = DIV32 10000 ' z1 = 67
y = z1 - 37 ' Did you want precision???
I am sure something is wrong but it may help you understand.
Bookmarks