Altimeter/Barometer Picaxe Basic Program

These declarations contain all of the numerical values which determine the accuracy of this instrument.<br>They are based on readings of the ADC output at locations whose altitude was obtained from DeLorme Topo USA application. After correction for the reported barometric pressure that day to nominal pressure, a best fit curve was made in EXCEL using the official formula.<br>A set of 6 straight lines of slopes 12-17 were moved into best fit to portions of this curve. The intercepts of these lines provide the offset values. The ADC counts where the adjacent lines cross are the crossover values.

Instructions

This section concerns the two pushbuttons at the back of the instrument.<br>The left button is up, the right down. When pressed at power up, barometric pressure is displayed in inches of mercury or millibars respectively. During operation they select altitude, which is the default, or voltage.<br>If both are pressed during operation a "C" appears on the left of the display and the buttons adjuct a correction for weather. This mode times out, after showing the adjustment value

Declarations and Data

#rem "Altimeter.bas"

John Saunders 6/4/2015 more segments

#endrem

#PICAXE 14M2



symbol MinusFlag        = bit0

symbol SettingFlag    = bit1

symbol Iter            = b1

symbol OldDispMode    = b2

symbol SettingCounter    = b3

symbol BitPos        = b4

symbol Mask            = b5

symbol ReadingCounter    = b6

symbol DisplayMode    = b7        '0-5=Altimeter,6=Millibar,7=Inches,8=Voltmeter

symbol Multiplier        = b8

symbol Divider        = b9

symbol DataAddr        = b10

symbol BaroAdjust        = b11

symbol Digit        = b12

symbol Offset        = w8

symbol Reading        = w9

symbol AnalogValue    = w10

symbol Crossover        = W11


rem Input-Output

symbol Clock        = B.1        'Drives Display Controller MC14489

symbol Enable        = B.2

symbol Outdata        = B.3

symbol PressurePort    = C.0

symbol VoltPort        = C.4

symbol Upbutton        = PINC.1

symbol Downbutton        = PINC.2



rem Constants

symbol ThreeDigitCmd    = %0111    'For blanking 2 leading zeroes

symbol FourDigitCmd    = %0011    'For blanking 1 leading zero

symbol FiveDigitCmd    = %0001

symbol Cmd4MSB        = %0100

symbol BaroAdjMax        = 160

symbol BaroAdjMin        = 96

symbol Millibar         = 6

symbol Inches        = 7

symbol Voltmeter        = 8

symbol SettingMax        = 30



DATA 0,(8,4,2,1)                'Mask of bits for DispHex

symbol MultiplierAddr    = 4

DATA 4,(50,35,25)                'Multiplier modes 6 - 8

symbol DividerAddr    = 7

DATA 7,(11,26,25)                'Divider modes 6 - 8

symbol BaroAdjustAddr   = 10        

DATA 10,(128)                'Altitude Adjustment

symbol OffsetAddr        = 11

rem       11320   12205   12725   13225   13605   13885

DATA 11,($38,$2C,$AD,$2F,$B5,$31,$A9,$33,$25,$35,$3D,$36)    'Altitude offset 

rem      5893   1746      509

DATA 23, ($05,$17,$D2,$06,$FD,$01)    'Millibar,Inches,Volts offsets

symbol AltCrossoverAddr = 29

rem     1900    4000  5700 7700  9000    feet

rem      790     620   500  370   280    count

DATA 29,($16,3,$6C,2,$F4,1,$72,1,$18,1,0,0)    'Altitude crossover

rem  from Calc6-095


Init and  Main

init:

SETFREQ m4

High Enable                    'Low is active for MC14489

READ BaroAdjustAddr,BaroAdjust

IF BaroAdjust >  BaroAdjMax OR BaroAdjust <  BaroAdjMin THEN

     LET BaroAdjust = 128

ENDIF

LET DisplayMode = 0

IF Upbutton = 1 THEN

    LET DisplayMode = Inches

ENDIF

IF DownButton = 1 THEN

    LET Displaymode = Millibar

ENDIF

PAUSE 1000

LET SettingFlag=0

let OldDispMode = 15


main:

rem See if new mode

IF Upbutton = 1 AND SettingFlag = 0 THEN

    LET DisplayMode = 0

ENDIF    

IF Downbutton = 1 AND SettingFlag = 0 THEN

    LET DisplayMode = Voltmeter

ENDIF    


rem See if adjustment is set

IF Upbutton  = 1 AND Downbutton = 1 THEN    'To set for barometer correction

    LET DisplayMode = 0

    LET SettingFlag = 1

    LET SettingCounter = 0

ENDIF


rem See if there is an adjustment now

IF SettingFlag = 1 AND  Downbutton  = 1 AND BaroAdjust < BaroAdjMax THEN

    INC BaroAdjust

    LET SettingCounter = 0

ENDIF

IF SettingFlag = 1 AND  Upbutton  = 1 AND BaroAdjust > BaroAdjMin THEN

    DEC BaroAdjust

    LET SettingCounter = 0

ENDIF


IF SettingFlag = 1 THEN

    INC SettingCounter

    IF SettingCounter >= SettingMax THEN

        LET SettingFlag = 0

        WRITE BaroAdjustAddr, BaroAdjust

    ENDIF

ENDIF


rem Get the Altitude Display Mode

IF DisplayMode < 6 THEN

    READADC10 PressurePort,Reading

    FOR Iter = 0 TO 5

        LET DataAddr = 2*Iter + AltCrossoverAddr

        READ DataAddr,WORD Crossover

        IF Reading > Crossover THEN

            LET DisplayMode = Iter

'            sertxd (#Displaymode,",",#Reading,",",#Crossover,13,10)

            EXIT

        ENDIF

    NEXT

ENDIF


rem Get Multiplier.Divider, Offset if DisplayMode has changed

IF OldDispMode <> DisplayMode THEN

    IF DisplayMode < 6 THEN

        LET Multiplier = 12 + DisplayMode

    ELSE

        LET DataAddr = DisplayMode + MultiplierAddr - 6

        READ DataAddr, Multiplier

    ENDIF

    IF DisplayMode > 5 THEN

        LET DataAddr = DisplayMode + DividerAddr - 6

        READ DataAddr, Divider

    ENDIF

    LET DataAddr = 2 * DisplayMode + OffsetAddr

    READ DataAddr, WORD Offset

    LET OldDispMode = DisplayMode

ENDIF


rem Measure the sensors

LET AnalogValue = 0

FOR Iter = 1 TO Multiplier

    IF DisplayMode    = Voltmeter THEN    

        READADC10 VoltPort,Reading

    ELSE 

        READADC10 PressurePort,Reading

    ENDIF

    IF DisplayMode <> Voltmeter THEN

        LET Reading = Reading + BaroAdjust - 128

    ENDIF

    LET AnalogValue = AnalogValue + Reading

NEXT


rem Calculate the value to be displayed

IF DisplayMode > 5 THEN

    LET AnalogValue = AnalogValue / Divider + Offset

ELSE

    IF Offset > AnalogValue THEN

        LET AnalogValue = Offset - AnalogValue

    ELSE

        LET AnalogValue = AnalogValue - Offset

        LET MinusFlag = 1

    ENDIF

ENDIF


rem Briefly display the adjustment

IF SettingFlag = 1 AND SettingCounter > 10 THEN

    IF BaroAdjust < 128 THEN

        LET AnalogValue = 128 - BaroAdjust

    ELSE

        LET AnalogValue = BaroAdjust -128

        LET MinusFlag = 1

    ENDIF

ENDIF



rem Display the measurement

Low Enable

LET Digit = Cmd4MSB

GOSUB SendHex

LET Digit = ThreeDigitCmd

IF AnalogValue < 1000 OR MinusFlag = 1 THEN SendCmd

LET Digit = FourDigitCmd

IF AnalogValue < 10000 THEN SendCmd

LET Digit = FiveDigitCmd

SendCmd:

GOSUB SendHex            'This blanks leading zeroes by using sprcial decode

PULSOUT Enable,10

IF SettingFlag = 1 THEN 'Not a numerical display - used for leds or dps

    LET Digit = 7     'Displays a "C" in setting Mode by energising segments a,d,e,f

ELSE

    LET Digit = 0     'Blank     

ENDIF

GOSUB SendHex

FOR Iter = 0 to 4

    LET Digit = AnalogValue // 10

    LET AnalogValue = AnalogValue / 10

    IF Iter = 4 AND MinusFlag = 1 THEN

        LET Digit = 13        ' minus sign

    ENDIF

    GOSUB SendHex

NEXT

High Enable

LET MinusFlag = 0


GOTO main

Sub-programs

SendHex:                'Input is in digit

FOR BitPos = 0 TO 3        'Faster than the shiftout workaround in the manual

    READ BitPos,Mask

    LET Mask = Digit & Mask 

     LOW Outdata

    IF Mask = 0 then Charbitislow

    HIGH Outdata

Charbitislow:

     PULSOUT Clock,1

NEXT

RETURN