LCD Bargraph (fuel gauge)

Status
Not open for further replies.

karenhornby

New Member
Hi
I've "designed" a program for a Pic 16F877A that will be the main part of my conversion from Diesel to Vegetable oil in my car.
What I want to do is using the a/d convertor in the 16F877A and the fuel sensor in the fuel tank (basically a variable resistor) drive line 3 of a 4 X 20 LCD display showing the word "DIESEL" and then the bar graph corrosponding to the level in the tank
and the same on line 4 but showing the word "OIL" and the same bar graph but obviously driven for a different fuel level sensor in the 2nd tank
I dont know the values of the "pots" (variable resistors) yet, but

Has anyone any idea how to write code to do this? PREFERABLY using Picbasic Pro, although if its in any other kind of basic I might be able to convert it ( or code all ready to use )

What I've got so far is this: although so far it's using a 2 X 16 lcd display
It works well, Inputs PORTC.0 and PORTC.3 are the triggers from 2 X DS1620's in thermostat modes which go high once preset temperatures are reached, this then triggers 2 solenoid valves on PORTE.0 and PORTE.1
When I've reached my destination I press button PORTD.1 which then gets rid of the oil out of the system for a set amount of time before switching back to Diesel
Feel free to use this if it's any use to anyone
Oh forgot to mention, the 2nd line of the LCD displays the temperature outside the car using a DS1820
Code:
'****************************************************************
'*  Name    : TEST1.BAS                                         *
'*  Author  : Karen                                             *
'*  Notice  : Copyright (c) 2008   .                            *
'*  Date    : 09/04/2008                                        *
'*  Version : 1.0                                               *
'*  Notes   :Program for Auto switching Diesel/Veg Oil on my car*
'*          :                                                   *
'****************************************************************
                    
' 
Define	LOADER_USED	1
@    __CONFIG _HS_OSC & _LVP_OFF
' Define various stuff
  DEFINE OSC 8
  DEFINE LCD_DREG PORTB       ' Set LCD Data port
        DEFINE LCD_DBIT 0           ' Set starting Data bit (0 or 4) if 4-bit bus .7
        DEFINE LCD_RSREG PORTB      ' Set LCD Register Select port
        DEFINE LCD_RSBIT 4          ' Set LCD Register Select bit 
        DEFINE LCD_EREG PORTB       ' Set LCD Enable port
        DEFINE LCD_EBIT 5           ' Set LCD Enable bit i.e, PORTE.4
        DEFINE LCD_BITS 4           ' Set LCD bus size ot 4 bit Upper Nibble (4 or 8 bits)
        DEFINE LCD_LINES 2         ' Set number of lines on LCD to 4 Lines
        DEFINE LCD_COMMANDUS 2000   ' Set command delay time in us
        DEFINE LCD_DATAUS 50        ' Set data delay time in us
        TRISC.1 = 1
       
        INPUT PORTC
       


' Allocate variables
command var     byte            ' Storage for command
i       var     byte            ' Storage for loop counter
temp    var     word            ' Storage for temperature
DQ      var     PORTA.5         ' Alias DS1820 data pin
DQ_DIR  var     TRISA.5         ' Alias DS1820 data direction pin


        ADCON1 = 7              ' Set PORTA and PORTE to digital

        Low PORTE.2             ' LCD R/W line low (W)
        Pause 100               ' Wait for LCD to start
        Lcdout $FE,1,"     DIESEL", $FE,$C0, "  Please Wait"      ' Display sign-on message
        Pause 3000
       


' Mainloop to read the temperature and display on LCD
mainloop:
      
        Gosub init1820          ' Init the DS1820

        command = $cc           ' Issue Skip ROM command
        Gosub write1820

        command = $44           ' Start temperature conversion
        Gosub write1820

        Pause 100              ' Wait 0.1 seconds for conversion to complete

        Gosub init1820          ' Do another init

        command = $cc           ' Issue Skip ROM command
        Gosub write1820

        command = $be           ' Read the temperature
        Gosub write1820
        Gosub read1820

        ' Display the decimal temperature
       
       IF PORTC.0 = 0 AND PORTC.3 = 0 then lcdout $FE,1,"     Diesel", $FE,$C0,"Temp", $fe, $C7, dec (temp >> 1), ".", dec (temp.0 * 5), $DF, "C":  LOW PORTE.0: LOW PORTE.1
       IF PORTC.0 = 1 AND PORTC.3 = 0 then gosub Tempcheck
       IF PORTC.0 = 0 AND PORTC.3 = 1 then gosub Tempcheck
       If PORTC.0 = 1 AND PORTC.3 = 1 then gosub Oil
       IF PORTD.1 = 1 then gosub Purging
        
       
Goto mainloop           ' Do it forever



Tempcheck:
lcdout $FE,1,"    TEMP LOW", $FE,$C0,"Still on  DIESEL"
LOW PORTE.0: LOW PORTE.1      
 return
 
Purging:

LCDOUT $FE,1," PURGING Please",$FE,$C0,"      WAIT"
HIGH PORTE.0
LOW PORTE.1

Pause 10000
         gosub Stop1 

Goto mainloop
 
Stop1:
LOW PORTE.0
LOW PORTE.1
LOW PORTD.1
LCDOUT $FE,1,"    Turn OFF",$FE,$C0,"   Engine NOW" 
while PORTD.1=0           'Give driver chance to turn engine off or wait for button to be pressed again
wend
pause 1000                 'so a very quick button press is NOT recognised and the program stays paused
Goto Tempcheck 

OIL:
LCDOUT $FE,1,"     Veg-Oil", $FE,$C0,"Temp", $fe, $C7, dec (temp >> 1), ".", dec (temp.0 * 5), $DF, "C"
HIGH PORTE.0
HIGH PORTE.1
return 


' Initialize DS1820 and check for presence
init1820:
        Low DQ                  ' Set the data pin low to init
        Pauseus 500             ' Wait > 480us
        DQ_DIR = 1              ' Release data pin (set to input for high)

        Pauseus 100             ' Wait > 60us
        If DQ = 1 Then
                Lcdout $fe, 1, "DS1820 not present"
                Pause 500
                Goto mainloop   ' Try again
        Endif
        Pauseus 400             ' Wait for end of presence pulse
        Return


' Write "command" byte to the DS1820
write1820:
        For i = 1 to 8          ' 8 bits to a byte
                If command.0 = 0 Then
                        Gosub write0    ' Write a 0 bit
                Else
                        Gosub write1    ' Write a 1 bit
                Endif
                command = command >> 1  ' Shift to next bit
        Next i
        Return

' Write a 0 bit to the DS1820
write0:
        Low DQ
        Pauseus 60              ' Low for > 60us for 0
        DQ_DIR = 1              ' Release data pin (set to input for high)
        Return

' Write a 1 bit to the DS1820
write1:
        Low DQ                  ' Low for < 15us for 1
@       nop                     ' Delay 1us at 4MHz
        DQ_DIR = 1              ' Release data pin (set to input for high)
        Pauseus 60              ' Use up rest of time slot
        Return


' Read temperature from the DS1820
read1820:
        For i = 1 to 16         ' 16 bits to a word
                temp = temp >> 1        ' Shift down bits
                Gosub readbit   ' Get the bit to the top of temp
        Next i
        Return

' Read a bit from the DS1820
readbit:
        temp.15 = 1             ' Preset read bit to 1
        Low DQ                  ' Start the time slot
@       nop                     ' Delay 1us at 4MHz
        DQ_DIR = 1              ' Release data pin (set to input for high)
        If DQ = 0 Then
                temp.15 = 0     ' Set bit to 0
        Endif
        Pauseus 60              ' Wait out rest of time slot
        Return

        End
 
Last edited:
blueroomelectronics said:
Isn't it illegal to run a vehicle on vegetable oil?

Is that a new law? I've known a few vehicles which have gone cross-Canada on used french-fry oil and such. Never got a ride in one myself though.

Bummer if the buggers went and passed a law against it.


Torben
 
I’m confused with your fuel (Density) & level sensors does all variable resisters?

You said for the fuel sensor using a V/R

You said different fuel level sensor used to detect level

It will work on test bench but doesn’t the resistance varies with the temperature?
 
blueroomelectronics said:
You could just run your diesel car on home heating oil if it wasn't illegal, you're avoiding the road tax. If you don't take your vehicle on public roads you can do what you want.
https://en.wikipedia.org/wiki/Diesel

Road tax? I wasn't aware of anything like that in Canada (or the US) as far as biodiesel/veggie oil fuel goes. If you try to use farm gas (marked gas) in a road vehicle and get caught, then yes: you're committing tax fraud because you're buying a fuel ostensibly intended for one use (farm or industrial) at a reduced tax rate but then using it for another (road driving). I'm not sure that applies to vegetable oil. I grew up on a farm so I'm familiar with marked gas.

According to Wikipedia (https://en.wikipedia.org/wiki/Straight_vegetable_oil#Taxation_of_fuel ), there has been an excise tax exemption in Canada since 2003. For most jurisdictions it appears that there is nothing illegal about vegetable oil as a fuel per se as long as you know what taxes you should be paying on it. Environment Canada has a page on biodiesel and vegetable oil fuels which mentions no tax or legality issues other than to note that with no tax exemptions in place to help defray the costs of its production, it can be more expensive than fossil-fuel diesel.

That said I'd need to do more research before I decided to use it.


Torben
 
That exemption is for BioDiesel that's not the same thing as vegatable oil.
The Government of Canada exempted biodiesel from the federal excise tax on diesel in the March 2003 budget.

You're right about the taxes, can you imagine how they plan on collecting road tax on electric cars?
 
blueroomelectronics said:
That exemption is for BioDiesel that's not the same thing as vegatable oil.

True, that's why I need to read more on it--to find out where they draw the line. I've just never heard of anyone who's done it getting hassled or charged, and one or two of those have been bands on tour making lots of noise about being "green" tours. Maybe they're tightening the noose in Ottawa lately though. I've found a few articles about people getting fined in the US though.

It does appear to me more likely to be a problem than I thought. Definitely makes sense to find out first what the local government thinks, I agree.


Torben
 
blueroomelectronics said:
Isn't it illegal to run a vehicle on vegetable oil?
No it's not illegal or breaking the law, i guess it depends on where you'r from but in the UK it's allowed and not only dont you pay duty (taxes on it) if you use less than 2500 litres a year, but it looks like ( according to new regulations just released) that if we bought biodiesel or veg oil to use in the car in the past year and paid taxes we might be able to get a refund
see https://customs.hmrc.gov.uk/channel...ntent&propertyType=document&id=HMCE_CL_000205
section 4.2.1
Over here they changed the regulations last year so that anyone using making less than 2500 litres in a year is tax exempt from fuel duty, the exemption applies not only to bio diesel but also new oil as you buy in a shop, or waste oil you would collect and filter from a business and no It's fine to use in any diesel car as long as A it's filtered properly, 2 if it's waste oil been dewatered, and 3 you start the car on diesel and dont switch over to oil until the fuel injector has reached a minimum of 50ºC and that's measured at the OUTPUT from the fuel injector, and the oil a minimum temperature of 70ºC by using a heat exchanger and heating the oil from the engine coolant.

There are a couple of exceptions where you can use oil with no heating but almost every car since the late 1990's and all the ones before that which use a Lucas puel injection pump will NEED the oil heated first or you're going to have a major bill for a new fuel injection pump.

Back to my project now

The variable resistors that will measure the fuel level are the senders in the fuel tank, 2 of those, one in each tank
They work fine with diesel and petrol (gas for you americans) and will work fine with oil, they dont vary resistance over time or anything, and All I want to end up with is one line of a LCD display saying either "Diesel or OIL"( depending on which tank it's measuring) then the LETTER E then a bargraph corrosponding to the amount of fuel in the tank, and at the end of the line the lettter F the letters E and F stand for Empty and Full
There will be NO fuel Density sensors, ONLY FUEL LEVEL just exactly like the fuel sender in the fuel tank of any car
 
Last edited:
blueroomelectronics said:
Isn't it illegal to run a vehicle on vegetable oil?
Bill...
Forgot to say thanks for all the help a couple of months ago, I went with the easypic5 in the end, mainly because of the extremely high import duties and the stupid length of time the post takes to come from Canada (upto 6 weeks) or I would have bought one of yours instead as they look extremely good. now if only you had a supplier in the UK, or Europe....
 
Suppliers are difficult to get their attention if you're a small company like mine.
Are you the person from EDA board who bought the EasyPIC5?
Hmm I ship plenty to the UK, no complaints so far and it ususally takes about a week or two.

You could drop a 18F4620 into that EasyPIC5's 16F877A socket and try your hand with Swordfish BASIC, IMO it's superior to PICBASIC Pro.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…