PDA

View Full Version : Using div32 to get decimal numbers



nkwankwe
- 18th April 2010, 05:23
Ladies and Gentlemen,

I have a small problem: I need to display the volume of water that is being fed by a flow transducer to PIC16F877A.The transducer outputs square pulses at 65 pulses per litre,meaning 1 pulse= 0.0154 litres.I can successfully read 65pulses/litre and display on LCD sreen.However,I need more accuracy than that,so I need to count at resolution of 1l1pulse per litre.Now the problem is the floating points.
I need help using Div32 to count and display
Thank you

Clinton

Acetronics2
- 18th April 2010, 12:53
hi, clinton

no difficulties...

suppose you won't count more than 1008 liters ( 65535 pulses ...); liters is a WORD !

1) calculate liters

liters = pulses/65 ... obvious

2) calculate decimals

Tmilliliters = ((pulses // 65) *10000)/65
centiliters = DIV32 100

here we take the pulses count remainder ( less than 1 liter ... ), multiply by 10000 and divide per 65 to get 1/10,000 liters; this number is an integer and respects the sensor conversion ratio.

then result is truncated to centiliters to keep a 0/ -1 digit ( or centiliter ) calculation precision ... and resolution.

Why centiliters ??? ... simply because sensor counts per 1.54 centiliter increment ... shown milliliters would only be virtual ... hence FALSE ! ( but our calculation is true !!!)

3 ) print result

Pulsout $FE,1 ,dec Liters,"." dec3 centiliters

here it is ...

Alain

nkwankwe
- 19th April 2010, 03:01
Thank you Alain
I'll try the coding

nkwankwe
- 19th April 2010, 03:45
I have written the code as follows below,but the output is so fast,I think its giving me millilitres rather than litres...How do I modify it





W VAR BYTE
W1 var WORD
W2 VAR WORD
W3 VAR WORD
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
W=0
W1=0
W3=0
PAUSE 1000
MAIN:
IF PORTC.2=1 THEN
W=1
FOR W = 0 TO 65
NEXT W
W1= W1 + (W/65)
W2= ((W1 // 65) *10000)/65
W3= DIV32 100
LCDOUT 254,1
LCDOUT DEC W1, ".", DEC W3, "L"
ENDIF
GOTO MAIN

HenrikOlsson
- 19th April 2010, 06:17
Hi,
Not sure what your trying to do but it looks like perhaps your NEXT W should be moved to between the LCDOUT and the ENDIF statements. Then, if you want to see it count on the display you'll probably need a short Pause in there a well.

/Henrik.

Acetronics2
- 19th April 2010, 08:35
Hi, Clinton

@ first your counting method is wrong ... you should count transitions ( L> H i.e.) of your output sensor.
@ second ... what's the reason of the " For w = 0 to 65 ..." loop ???
@ third ... ----------------d°----- " W1 = W1 + W/65 " ???

once that tied ... display should slow down a bit ...

Alain

nkwankwe
- 20th April 2010, 05:08
ok..
1- the loop, For W = 0 To 65, I meant to count pulses for 1 litre(65 pulses=1litre)
so the value I would be storing in W1 will be(0+1), followed by (1+1), then (2+1) and so on.. every 65 pulses.

The objective is to count litres and display the value on LCD.I realised that count by litres is not good since, e.g. 1 cup of water is not a litre,so I would miss that count.
That is why I wanted to count at a better resolution of 1 pulse(=0.0154litres).

I hope you understand my objective better now...
Thanks for the input, it is appreciated.
Looking forward to more help

nkwankwe
- 9th May 2010, 09:45
ok..
1- the loop, For W = 0 To 65, I meant to count pulses for 1 litre(65 pulses=1litre)
so the value I would be storing in W1 will be(0+1), followed by (1+1), then (2+1) and so on.. every 65 pulses.

The objective is to count litres and display the value on LCD.I realised that count by litres is not good since, e.g. 1 cup of water is not a litre,so I would miss that count.
That is why I wanted to count at a better resolution of 1 pulse(=0.0154litres).

I hope you understand my objective better now...
Thanks for the input, it is appreciated.
Looking forward to more help

hey..i cant seem to get anything right,maybe if you could write me a sample code,I'ld appreciate that