PDA

View Full Version : Adding Decimal Point



markscotford
- 23rd August 2009, 11:24
I struggle with maths (you may ask what am I doing here) and need help.
I am using a PIC with a 10 bit ADC as a volt meter, on a battery monitoring circuit. I need to read up to 100 volts DC, and display it on a LCD. I have got the circuit to run nicely, with 100 volts showing up as decimal 1024 (or near on), on the display after a divide by 20 resistor chain. What I actually need the display to show is 100.0, can anybody please point me in the right direction, to some example code that will do that for me? TIA

Mark in Spain.

Melanie
- 23rd August 2009, 13:38
If an ADC of 1023 (you can't have 1024) equals 100.0 then it is reasonable in maths to calculate...

1023/1023*100 = 100.0

But we can't do that in INTEGER maths so (and not knowing what version of PBP you've got) let's do this in a version that only has WORDS and BYTES...

Let's assume the following variables...

MyADC var WORD
Temp var WORD
VoltsResult Var WORD

So... after your ADC reading, MyADC contains say a value of 1023... let's do something like this...

Temp=MyADC*1000
VoltsResult=DIV32 1023

Now VoltsResult contains 1000 (representing 100.0), with the last (rightmost) digit being the tenths of volts... so we can extract and display what we want in any number of ways... here's just one...


Temp=VoltsResult/10
LCDOut $FE,$80,#Temp,".",#VoltsResult DIG 0," "

The displayed result will be "100.0".

You may need some additional display formatting... for example if you want to display the voltage right or left justified at a particular spot etc. The example above will display the result at the top-left of a typical LCD. The two blanks (spaces) being written to the LCD after the main display digits simply erase anything left on the LCD from a previous display that might be of a greater length than what is currently being written.

markscotford
- 23rd August 2009, 22:04
Thanks Melanie, you are a star worked first time, love you to bits.

markscotford
- 24th August 2009, 08:08
Would like to add some sort of Auto calibration to the system if possible, to iron out any variation is resistor tolerances across different PCB boards. The PIC I am using has some on-chip EEPROM memory. I was thinking of something on the lines of, at first power up, as part of the manufacturing process, the board is connected to a known set voltage (lets say 90.0 volts for example) and the firmware calculates the required divison/multiplication to get the display to read bang on 90.0 Volts. Any suggestions would be very appreciated.

Mark in Spain.

Melanie
- 24th August 2009, 16:12
That's quite simple... say on power-up a Button is held for 5 Seconds... the LCD Displays...

"Adjust for 90v"
"And Press SET"

When the Button is pressed, the ADC WORD value (containing the 90.0v reading) is then saved as a HighBYTE and LowBYTE component into two seperate EEPROM locations (since each EEPROM location can only save a BYTE).

When you power-up normally, rather than jumping into the Calibration routine (described above), your program instead loads the CAL value from EEPROM, and you would use the CAL value in your math with a modified routine, rather than the 1023 value routine previously exampled earlier in this thread...

0.1% Resistors are probably cheaper than paying the Labour to run through a CAL routine at manufacture. One of your divider Resistors could instead be a multi-turn high-stab precision POT (so you could also Calibrate for 90.0v using that method instead).