Use of the RCtime Command - Capacitor Discharge and Charge

' Program RCTIME_2.BAS
'
' Charges external capacitor to near +5.0 VDC.  On executing the RCtime
' command, the IO pin goes to a high impedance and capacitor discharges
' toward ground (the state of pin P25) with a time constant RC.  When 
' the voltage decays below  the logic threshold (V_thresh) such that 
' it is seen as a zero by the  BasicX the time is passed back to 
' the calling function in T_fall.
'
' Capacitor is then discharged to zero.  On executing the RCtime
' command, the IO pin goes to a high impedance and the capacitor charges
' toward +5VDC (the state of pin P25) with a time constant RC.  When 
' the voltage rises above the logic threshold (V_thresh) such that 
' it is seen as a logic one by the  BasicX the time is passed back to 
' the calling function in T_rise.
' 
' Thus;
'
'    T_tot = RC (ln (5.0/V_thresh) + ln (5.0/(5.0-V_thresh))
' or T_tot = RC * k
'
' The use of this technique of measuring both the dscharge and charge 
' times drastically reduces errors due to variation in V_thresh from 
' one device to another.  For example, if V_thresh is a bit low, T_fall 
' will be longer, but T_rise will be shorter.
'
' Copyright, Peter H. Anderson, Baltimore, Sept, 99
'

Public Const P24 as byte = 24	' RCTime
Public Const P25 as byte = 25	' Used on Resistor side
				' 0 for discharge, 1 for charge

Sub Main()

   Dim T_rise as Single
   Dim T_fall as Single
   Dim T_tot as Single

   Call OpenSerialPort(2, 9600)		' For debugging

   Do
      ' measure T_fall
      Call PutPin(P25, bxOutputLow)	' steady state val is ground
      Call PutPin(P24, bxOutputHigh)	' exert a one
      Call Sleep(1.0)			' give it time to charge to +5

      Call RCtime(P24, 1, T_fall)	' measure T_fall

      ' now measure T_rise
      Call PutPin(P25, bxOutputHigh)	' steady state val is +5
      Call PutPin(P24, bxOutputLow)	' exert a zero
      Call Sleep(1.0)			' give time for C to discharge

      Call RCtime(P24, 0, T_rise)	' measure T_rise

      T_tot = T_fall + T_rise

      Call PutS(T_rise)			' display the results
      Call PutByte(32)
      Call PutS(T_fall)
      Call PutByte(32)
      Call PUTS(T_tot)
      Call NewLine()

   Loop
End Sub