PutTimeStamp and GetTimeStamp


' RTC_1.Bas
'
' Sets Real Time Clock using PutTimeStamp and then periodically reads
' using GetTimeStamp and displays in YY/MM/DD HH:MM:SS format.
'
' If the time is in the range of 00:01:00 to 00:03:00 an LED is turned
' on.
'
' Note that input pushbuttons might be used to modify the year, month, day,
' hour and minute so as to initialize to the current date and time.
'
' Note that a subroutine PutB_fixed is implemented to output a byte as
' either 3, 2 or 1 significant figures
'
' Compile with SerialPort.Bas
' 
' copyright, Peter H. Anderson, Baltimore, MD, Oct, '99

Sub Main()  

  Dim Year as Integer, Month as Byte, Day as Byte
  Dim Hour as Byte, Minute as Byte, Second as Single
  Dim SecByte as Byte

  Dim str as String 

  Year = 1999
  Month = 10
  Day = 12
  Hour = 23
  Minute = 59
  Second = 0.00

  Call OpenSerialPort(2, 9600)
  Call PutPin(21, 1)	' turn LED off

  Call PutTimeStamp(Year, Month, Day, Hour, Minute, Second)

  Do
     Call GetTimeStamp(Year, Month, Day, Hour, Minute, Second)
	' fetch the date and time

     Call PutI(Year)	' and display it
     str = "/"
     Call PutStr(str)
     Call PutB_fixed(Month, 2)
     Call PutStr(str)
     Call PutB_fixed(Day, 2)

     str = " "
     Call PutStr(str)
     Call PutB_fixed(Hour, 2)
     str = ":"
     Call PutStr(str)     

     Call PutB_fixed(Minute, 2)
     str = ":"
     Call PutStr(str)     

     SecByte = CByte(Second)
     
     Call PutB_fixed(SecByte, 2)
     Call NewLine()

     ' check time and if in range, turn on LED
     If ((Hour = 0) AND (Minute >= 1) AND (Minute <=2)) then
        Call PutPin(21, 0)	' turn on the LED
     Else
        Call PutPin(21, 1)	' otherwise, turn it off
     End if

     Call Sleep(2.0)      
  Loop

End Sub

Sub PutB_fixed(ByVal X as Byte, ByVal Sig as Byte)	
                ' output byte with sig figures

   Dim str as String 
   Dim Y as Byte
   
   If (Sig = 3) then 
      If (X <10) then
         str = "00"	' insert two leading zeros
         Call PutStr(str)
         Call PutB(X)
      ElseIf (X<100) then
         str = "0"	' insert one leading zero
         Call PutStr(str)
         Call PutB(X)
      Else  
         Call PutB(X)
      End if
   
   ElseIf (Sig = 2) then    
      X = X MOD 100		
      If (X < 10) then
         str="0"		' insert a leading zero
         Call PutStr(str)
         Call PutB(X)
      Else
         Call PutB(X)	' otherwise, it isn't necessary
      End If
   Else
      X = X MOD 10
      Call PutB(Y)
   End If
End Sub