Use of First Time and Persistent Variables

copyright, Peter H. Anderson, Baltimore, MD, Nov, '99


This program illustrates the use of the FirstTime() function and the use of persistent variables. Note that after fussing with the FirstTime() function for the BX24 for over an hour, I contacted Net Media and they indicated there was a problem with the command in Version 1.43. Frank Manning (Net Media) provided a work around which is included in this routine.

Note that this work around consists of testing location 30. If, non zero, it is the first time and the location is set to zero. If zero, it is not the first time after download.


' FirstTime.Bas
'
' If program is run the first time after download, two counters (long)
' are set to zero.  On each boot thereafter the BootCounter is 
' incremented.
'
' Each time a pushbutton (PB0) on Pin12 is set to zero, ButtonCounter is 
' incremented.
'
' Note that both counters are implemented as persistent variables
' and are thus, not affected by loss of power.
'
' This type of program might be used to determine the number of times
' a unit is reset and the number of times an action is performed. 
'
' Note that persistent variables may only be used in one module and must
' first be used in the same order as they are declared.
'
' Illustrates the use of FirstTime, Persistent variables and Do - 
' Loop Until.
'
' Build project with serial routines.
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99

Const PB0 as Byte = 12		' push button on terminal 12

Dim BootCounter as New PersistentLong
Dim ButtonCounter as New PersistentLong

Sub Main()

   Dim str as String
   
   If (FirstTime()) then
      BootCounter = 0
      ButtonCounter = 0
   Else
      BootCounter = BootCounter + 1
   End If
   
   Call OpenSerialPort(1, 19200)
   
      
   str = "BootCounter="		' display the boot counter
   Call PutStr(str)
   Call PutL(BootCounter)
   Call NewLine()  

   Do
      If (Button_Depressed(PB0)= 1) Then
         ButtonCounter = ButtonCounter + 1
         str = "ButtonCounter="
         Call PutStr(str)
         Call PutL(ButtonCounter)
         Call NewLine()
      End If
   Loop
End Sub

Function Button_Depressed(ByVal Pin as Byte) as Byte

   Dim RetVal as Byte
   Dim Y as Byte
   
   Call PutPin(Pin, 3)
   
   If (GetPin(Pin) = 1) then
      RetVal = 0
   Else
      RetVal = 1	' true
   End if

   Do
      Call PutPin(Pin, 3)	' now, wait for it to go back to a one
      Y = GetPin(Pin)
   Loop Until (Y=1)

   Call Sleep(0.1)
   Button_Depressed = RetVal

End Function

'-------------------------------------------------------
Public Function FirstTime() As Boolean
'
' Provided by Frank Manning at NetMedia

' This function applies to BX-24 systems ONLY, compiler
' version 1.43 or earlier. This function should NOT be
' use on BX-01 systems.
'
' The returned value signifies whether this program is
' running for the first time since it was downloaded.
'
' The first call returns true. All subsequent calls
' return false, even if  the system reboots in the
' meantime.

    Const FtAddress As Integer = 30

    If (PersistentPeek(FtAddress) = 0) Then
        FirstTime = False
    Else				' location 30 is non zero
        Call Sleep(100)
        Call PersistentPoke(0, FtAddress)	' make it zero
        FirstTime = True		
    End If

End Function
'-------------------------------------------------------