# A/D conversion PIC18F24K20

#### Peet19

##### Member
Hi All!
I would like to ask how, is it possible to solve that if there is 1.6V on an analog input, then the value of the register should be 0, and if it is 3V then 255? This is the temperature sensor voltage range. So, the lower voltage limit should not be 0V but 1.6V, and the upper limit should be 3V.

#### Nigel Goodwin

##### Super Moderator
There are limits on how far you can adjust the A2D reference limits, the usual answer is to simply use an opamp to move the analogue voltage to the correct range. The A2D is also 10 bit, not 8 bit - and 'may' even be 12 bit (some 18F devices are).

Consult the datasheet, it tells you all the specs.

• Peet19

#### ronsimpson

##### Well-Known Member
Because the ADC is 10 bit you have some options.
1) Connect Vref- to 0V and Vref+ to 3.3V. Read in 10 bit mode and subtract off 1.6V. You will have more that 8 bit accuracy. If you need 8 bits, then shift twice to lose two LSBs.
2) Connect Vref- to a 1.6V source and hope it is good. Hope that 1.6V from the sensor matches the 1.6V reference you made.

• Peet19

#### Peet19

##### Member
I see, thank you. I will make it 10 bits, I only wrote 255 so that it fits in 1 register. I don't think it's possible to produce a very stable, reliable 1.6V reference voltage, but it's a good idea.

Nigel, thank you. This is the amplified voltage. The original is 0 Celsius -> 290 mV, 280 Celsius -> 540 mV.

Then I'll stick with the traditional method. GND and the supply voltage. Maybe I'll amplify it to a higher voltage range.

#### Nigel Goodwin

##### Super Moderator
I see, thank you. I will make it 10 bits, I only wrote 255 so that it fits in 1 register. I don't think it's possible to produce a very stable, reliable 1.6V reference voltage, but it's a good idea.

Nigel, thank you. This is the amplified voltage. The original is 0 Celsius -> 290 mV, 280 Celsius -> 540 mV.

Then I'll stick with the traditional method. GND and the supply voltage. Maybe I'll amplify it to a higher voltage range.

Apply an offset as well as amplification, and increase the amplification if need be.

• Peet19

#### Peet19

##### Member
Ok thanks I will do it.

#### rjenkinsgb

##### Well-Known Member
Using 10 bit ADC mode, with direct input from the sensor:

If you used 5V power and the ADC reference to 0V and 5V:
Subtract 327 and the result would be 0 - 287 for 1.6 - 3.0V

Multiply by 228 and use the high byte of the result for 0-255

With a 3.3V supply, subtract 496 and the result would be 0 - 434

Multiply by 151 and use the high byte.

Or, connect the VREF- external input to a precision 1.25V reference IC.
With 5v supply & 5v VREF+:
Subtract 95, result 0 - 382 for 1.6V - 3.0V

Multiply by 171 and use the high byte.

With 3.3V supply and 1.25V VREF-: Subtract 172, result = 0 to 699 for 1.6V - 3.0V

Multiply by 93 and use the high byte (that gives 0-253)

The actual values depend on the ADC input and reference voltage accuracies.

If you just need a reasonable range and not exactly 0-255, you can use the subtraction part of each calculation & ignore the multiplication part.

• Peet19

#### Peet19

##### Member
rjenkisgb: Do not fully understand. That's how I calculated it.
In the case of 0 - 5V in 10-bit resolution, the value of the register increases by 1 if there is a change of 5 / 1024 = 4.88mV.
Thus, at 1.6V, the value of the register will be 1.6 / 4.88*10-3 = 327.
and at 3V it will be 3 / 4.88*10-3 = 614.
So the total range will be 614 - 327 = 287.
Since I measure from 0 to 280 degrees Celsius, the value of the range is just right. 1 degree change = by increasing the value of the register.
What do I multiply by 228?

#### Peet19

##### Member
Danadak: This is how I solved the voltage gain. #### rjenkinsgb

##### Well-Known Member
What do I multiply by 228?
That's if you wanted to scale it to 0-255 rather than 0-287

Multiplying (for that value) by 228 then using the high byte is a quick way of re-scaling it to 0-255, without any floating point calculations, if you wanted the 0-255 result you mentioned in the first post.

• Peet19

#### Peet19

##### Member
I understand now, thanks, that's a good idea #### transconductance

##### Member
Certainly it is possible to create a stable 1.6 volt reference. Or any other voltage for that matter.
Check "The Art of Electonics" In the third edition, they're in chapter 9, section 10.

Replies
41
Views
4K
Replies
17
Views
2K
Replies
48
Views
4K
Replies
2
Views
700
Replies
2
Views
587 