Need some advice on code structure

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
hgboy
Posts: 25
Joined: Wed Aug 13, 2008 9:35 pm
Location: Dayton, OH

Need some advice on code structure

Post by hgboy » Fri Jan 30, 2009 8:03 pm

I am very close to having the first bit of my GLCD dashboard completed. The main issue left to deal with is more of a structural problem (or even a style problem). I am reading 3 ADC channels, which are connected to sensors on my car. From what I understand, the basic ADCRead function will return a number ranging from 0-1024, depending on the voltage signal from the sensors. Would it be possible to have a function with a switch statement(or select case, I can't remember which basic uses...) That determines a value for a variable based on the result of ADCRead? Or more specifically, can I pass ADCRead to a function as a parameter? I will post what code I have so far, I have put in functions, but they are more or less placeholders right now. Basically, my overall goal is to use the adc results to determine a final x or y position for a bar graph type display(two vertically arranged, one horizontal).

Code:

Code: Select all

{
*****************************************************************************
*  Name    : FUELGAUGE.BAS                                                 *
*  Author  : Austin Cole                                                    *
*  Notice  : Copyright (c) 2009 Austin Cole                                 *
*          : All Rights Reserved                                            *
*  Date    : 1/29/2009                                                     *
*  Version : 1.0                                                            *
*  Notes   : Center module for XR4Ti gauge cluster project.  Displays       *
*          : Fuel level, Temperature level, and Turbo boost pressure as     *
*          : Bargraph style gauges. Also displays numerical readout for     *
*          : turbo boost levels.  XR4Ti logo is displayed in the center.    *
*          : Project utilizes PIC18F4550 processor and 128x64 GLCD with     *
*          : Toshiba T6963C controller.                                     *
*****************************************************************************
}
Program FUELGAUGE

Device = 18F4550
Clock = 8
Config FOSC = INTOSCIO_EC

#option GLCD_MODEL = T6963c   '<<<********* Select Toshiba driver

#option GLCD_DATA = PORTB        // data port
#option GLCD_CE  = PORTC.3        // chip Enable
#option GLCD_RD  = PORTC.5        // RD pin
#option GLCD_RW  = PORTC.4        // RW pin 
#option GLCD_CD  = PORTC.6        // Command/Data   High=Cmd , Low=Data
#option GLCD_RST = PORTC.7       // reset pin

Include "INTOSC8.bas"
Include "utils.bas"
Include "Graphics.bas"
Include "GLCD.bas"
Include "ADC.bas"
Include "Arial.bas"
Include "FUELIMAGE.bas"

//Variables to hold ADC values for fuel, temperature, and boost
Dim FuelValue As Word
Dim TempValue As Word
Dim BoostValue As Word
Dim XValue As Word
Dim Y1Value As Word
Dim Y2Value As Word

//Reads ADC value for the fuel level on channel 0
Function GetFuelValue() As Word
    result = ADC.Read(0)
End Function

//Reads ADC value for the temperature level on channel 0
Function GetTempValue() As Word
    result = ADC.Read(1)
End Function

//Reads ADC value for the boost level on channel 0
Function GetBoostValue() As Word
    result = ADC.Read(2)
End Function

//Calculates YValue based on Fuel ADC result
Function GetY1Value() As Word
End Function

//Calculates YValue based on Temperature ADC result
Function GetY2Value() As Word
End Function

//Calculates XValue based on Boost ADC result
Function GetXValue() As Word
End Function

//Start of program


//Small delay for GLCD warmup
DelayMS(200)

//Clear GLCD
GLCD.Cls	

//Display "background" bitmap image here
//GLCD.SetImage(0,0)
//Use lines, etc to draw backgraound image until setimage function is implemented

rectangle(0,0,127,63)
Rectangle(1,1,126,62)  //Outside border

Rectangle(4,3,15,59)//Fuel gauge main border
line(14,10,15,10)
line(14,17,15,17)
line(14,24,15,24)
line(14,31,15,31)
line(14,38,15,38)
line(14,45,15,45)
line(14,52,15,52) //Fuel gauge division marks

Rectangle(35,47,91,58)//Boost gauge main border
line(42,47,42,48)
line(49,47,49,48)
line(56,47,56,48)
line(63,47,63,48)
line(70,47,70,48)
line(77,47,77,48)
line(84,47,84,48)//Boost gauge division marks  

Rectangle(112,3,123,59)//Temperature gauge main border
line(112,10,113,10)
line(112,17,113,17)
line(112,24,113,24)
line(112,31,113,31)
line(112,38,113,38)
line(112,45,113,45)
line(112,52,113,52) //temperature gauge division marks

Rectangle(30,10,96,28)//Center logo outline
//X
line(32,12,35,12)
line(33,13,35,13)
line(34,14,36,14)
line(35,15,36,15)
line(36,16,37,16)
line(37,17,38,17) //Upper left portion
line(43,12,46,12)
line(43,13,45,13)
line(42,14,44,14)
line(42,15,43,15)
line(41,16,42,16)
line(40,17,41,17) //upper right portion
line(37,21,38,21)
line(36,22,37,22)
line(35,23,36,23)
line(34,24,36,24)
line(33,25,35,25)
line(32,36,35,26) //lower left portion
line(40,21,41,21)
line(41,22,42,22)
line(42,23,43,23)
line(42,24,44,24)
line(43,25,45,25)
line(43,26,46,26)  //lower right portion
line(38,18,40,18)
line(39,19,39,19)
line(38,20,40,20)  //center portion
//R
Line(48,12,48,26)
Line(49,12,49,26)
Line(50,12,50,26)
Line(51,12,58,12)
Line(51,13,60,13)
Line(58,14,61,14)
Line(60,15,62,15)
Line(61,16,62,16)
Line(61,17,62,17)
Line(60,18,62,18)
Line(51,19,61,19)
Line(51,20,60,20)
Line(54,21,57,21)
Line(55,22,58,22)
Line(56,23,59,23)
Line(57,24,60,24)
Line(58,25,61,25)
Line(59,26,62,26)
//4
Line(76,12,76,26)
Line(77,12,77,26)
Line(78,12,78,26)
Line(66,21,75,21)
Line(66,22,75,22)
Line(66,21,75,12)
//T
Line(80,12,94,12)
Line(80,13,81,13)
Line(80,14,80,14)
Line(93,13,94,13)
Line(94,14,94,14)
Line(86,13,86,25)
Line(87,13,87,25)
Line(88,13,88,25)
Line(85,26,89,26)
//i
Line(92,18,93,18)
Line(92,19,93,19)
Line(92,21,92,26)
Line(93,21,93,26)
Line(94,26,94,26)

//Display text markers for Gauges
GLCD.SetFont(ArialBold)
GLCD.WriteAt(18,12,"F")
GLCD.WriteAt(18,54,"E")
GLCD.WriteAt(104,12,"H")
GLCD.WriteAt(104,54,"C")
GLCD.WriteAt(40,45,"PSI:")

//Main program loop
While(true)

    //ADC read functions take place here
    
    //Draw rectangles based on ADC results
    
    //Fuel gauge rectangle
    Rectangle(6,57,12,Y1Value)
    
    //Temperature Gauge rectangle
    Rectangle(115,57,121,Y2Value)
    
    //Boost Gauge rectangle
    Rectangle(37,56,XValue,50)

    //Small delay
    DelayMS(50)





Wend





End

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Fri Jan 30, 2009 8:20 pm

From the SF manual here:
http://www.sfcompiler.co.uk/swordfish/d ... umentation
The Select…Case Statement
select expression
case condition {, condition}
{statements}

[else {statements}]
endselect
Although there is nothing technically wrong with using large if…then blocks, it can
sometimes be difficult to read them. The select…case statement is an alternative
to using a large or multiple if…then…elseif statement. For example,
select MenuChoice
case "Q", "q" Message = "Quit"
case "M", "m" Message = "Main Menu"
case "A", "a" Message = "Option A"
case "B", "b" Message = "Option B"
case "C", "c" Message = "Option C"
else
Message = "Error"
endselect

hgboy
Posts: 25
Joined: Wed Aug 13, 2008 9:35 pm
Location: Dayton, OH

Post by hgboy » Fri Jan 30, 2009 8:56 pm

I think I've gotten it figured out. The compiler doesn't somplain about having a function passed as a parameter for another, so I am assuming it will be ok to do so. Hopefully I can get this thing wired up on the breadboard soon and tested out. Thanks for the help.

Post Reply