PDA

View Full Version : 2 Decimal Numbers



fowardbias
- 19th April 2006, 02:52
I have a 20x2 LCD display and using PBP can write any info to the unit using the LCDOUT command. I'm making a 0-30V power supply and plan using a voltage divider to feed the on board AD converter of a 16F877 to read the 30V output. I am going to use a hall sensor as a current detector to feed the second AD and use this to read the current output. How do I get the AD's to output to the display so that the voltage and current readouts are locked into two decimal places? Such as 30.00Volts (high end) and 0.00Volts (low end) and the same for current. I want the display to show a readout two decimal places wide. Can this be done with PBP with some simple code?

Melanie
- 19th April 2006, 07:56
If we assume you have a value between 0 and 3000 in your WORD variable (representing 0-30.00 volts), you can use DIG (see PBP manual) to outout and format your data. Here's a couple of examples...

Example 1 (Data Formatted from "Volts=0.00 " to "Volts=30.00 ")


LCDOut $FE,$80,"Volts="
If MyWord>999 then LCDOut #MyWord DIG 3
LCDOut #MyWord DIG 2,".",#MyWord DIG 1,#MyWord DIG 0," "

in the above example, we don't print the leading figure if the voltage is less than 10.00. To prevent the LCD 'blinking' when the voltage is updated, we don't clear the display, but overwrite the previous content. You need a trailing Blank to remove the least significant figure from the previous display if your voltage drops below 10.00. As you will know, $FE,$80 just positions the cursor at the start of Line 1, Column 1.

Example 2 (Data is Fixed in the same spot Formatted " 0.00v" to "10.00v" with leading zero surpession - note here the leading figure is this time replaced by a blank)


LCDOut $FE,$80
If MyWord<1000 then
LCDOut " "
else
LCDOut #MyWord DIG 3
endif
LCDOut #MyWord DIG 2,".",#MyWord DIG 1,#MyWord DIG 0,"v"

Code simple enough?

fowardbias
- 19th April 2006, 15:25
Excellent! I shall read about the DIG command. Thanks.

fowardbias
- 23rd April 2006, 04:02
I made a voltage divider for my project. A 22k and 3.3k resistor with the AD tap across the 3.3k into RA0. The supply varies from 0v to 25.15v. When the math is run, 3.37v across the 3.3k resistor has to equal 25.15v read out on the LCD. This is where I'm hitting the brickwall. How do I scale this to work correctly? I can get a proper readout if I keep the input to the AD at or below 5V. (no voltage divider) I can't go above this because of the voltage limit of the AD. Is this a matter of a equating something to something else using PBP?

Melanie
- 23rd April 2006, 08:31
If your maximum voltage is 25.15v, then chose Resistors to give you as close to 5v as possible at your ADC. Right now you're only using 2/3rds of your ADCs scale and limiting your final result accuracy.

Simple maths from then on (forget the actual voltage across the Resistor)...

If say a 10-bit ADC reading of 1010 is equal to 25.15v (your maximum) at your potential divider input, then...

1010*0.0249 will give you 25.149...

So let's scale everything up for Integer math...

MyADCWORD is a variable of your ADC reading
MyVoltageWORD is a variable containing the voltage to be displayed
TempWORD is just a variable

TempWORD=MyADCWORD*249
MyVoltageWORD=DIV32 10

This above code snippet will give you a result of 25149 which you can then use the DIG command to display upto five significant figures ie 25.149 or if you used DIV32 100 you would have four significant figures ie 25.14.

To work out the constant (in our above case 249) simply take...

Constant=Input Voltage/ADC Reading

...and just scale everything up accordingly so there are NO DECIMAL PLACES. Ignore the fact we are going to spill out of a WORD variable when we do our multiplication, DIV32 will take care of the final result. Use DIV32 to scale us back so the final Results fits in a WORD variable to the maximum number of significant places you need.

Finally, remember that DIV32 doesn't like interrupts as they may corrupt the result. So if your are using interrupts, the two lines of code that first multiply and then use DIV32 must be enclosed within an DISABLE/ENABLE pair. If you are not using interrupts, then you needent bother with this.

fowardbias
- 23rd April 2006, 16:44
I am a sponge for now, must absorb your good info.
Thans again Ms. M.

fowardbias
- 9th May 2006, 05:09
I ran the code Ms M. suggested and after a few setup line changes everything started to come around. I have the voltage divider set for exactly 5 volts to the AD. Here is the bummer: the display says Volts = 0.00 when the power supply is at zero volts, this is OK, but when the voltage is run up the display numbers are showing strobe dimming and are totally incorrect and unstable. The "Volts =" is steady and OK. The numbers vary from above 0.00 to 65.05 volts which is way off since the power suppply only runs up to about 25 volts. Can this be worked out by additional code or by filtering? My digital bench meter is showing a good clean output across the input to the AD which is 0 to 5volts.

Melanie
- 9th May 2006, 06:51
More likely you have the wrong JUSTIFICATION if you are using a WORD variable with your ADC. Check your DATASHEET for your PICs ADC Register settings and the ADCIN defines for the correct number of BITS you have set. If you are using the 16F877 (your first post)... you should have a line something like...

ADCON1.7=1

...somewhere in the initialisation section of your program. To find out in detail what this means, look in the ADC Section of the Datasheet for your PIC. But when using a 8-Bits from your ADC, the Result should be LEFT Justified, whilst when using more than 8-Bits, the ADC Result should be RIGHT Justified (which without seeing any code from you is what I think you should be doing).

Another reason for your results jumping about is that your sampling time could be too short.