Measuring Light Intensity using Input Capture


' Program Light_Intensity.BAS. ' ' Uses Texas Instruments TSL235 light to frequecy converter to measure ' light intensity. For a data sheet, see http://www.ti.com. ' ' Uses the InputCapture command to measure the period. ' ' TSL235 ------------------------> InputCapture (term 31) ' ' In Sub MeasPeriod, 50 transitions (25 periods) are captured in ' CaptureArray. These are then averaged by summing each element and ' dividing by 25.0 periods. The resolution is one count per 135.6 nanosecs. ' ' In Sub Main, the frequecy is then calculated as 1.0 / period. ' ' The intensity in uW/cm_squared is then calculated and the result is ' displayed. This is an excellent demonstration of the floating point ' capabilities of the BasicX. ' ' Note that as the InputCapture Command uses Timer 1 which is also used by ' COM2. Thus, COM1 was used. Note that COM1 is also tied to the network IC ' on the emulator board and terminal 14 must be brought high to enable COM1. ' ' If the light intensity is above 10.0 uW/cm_squared, an LED on pin 38 is ' turned on. If it falls below 5.0 uW/cm_squared, the LED is turned off. ' ' Compile with SerialPort.Bas. ' ' copyright, Peter H. Anderson, Baltimore, MD, Oct, '99 Public Const LED as byte = 38 ' LED pin Sub Main() Dim TSL_235_period as Single Dim TSL_235_freq as Single Dim Intensity as Single call PutPin(14, 1) ' to enable COM1 Call OpenSerialPort(1, 9600) ' term 11, use PIC-n-LCD Call PutPin(LED, 0) ' turn off brightness LED Do TSL_235_period = MeasPeriod() ' measure average period 'Call PutSci(TSL_235_period) 'Call NewLine() TSL_235_freq = 1.0 / TSL_235_period ' compute frequecy 'Call PutS(TSL_235_freq) 'Call NewLine() Intensity = exp(log(TSL_235_freq) - 7.2644) ' my best guess from the log - log plot in the data sheet Call PutS(Intensity) Call NewLine() If (Intensity > 10.0) then Call PutPin(LED, 1) ' turn on an LED if bright ElseIf (Intensity < 5.0) then Call PutPin(LED, 0) ' turn off if dark End if Call Sleep(1.0) Loop End Sub Function MeasPeriod() as Single Dim CaptureArray(1 to 50) as New UnsignedInteger Dim sum as Single Dim N as Integer Call InputCapture(CaptureArray, 50, 0) sum = 0.0 For N = 1 to 50 ' sum the 50 samples sum = sum + CSng(CaptureArray(N)) 'Call PutSci(sum) 'Call Sleep(0.2) 'Call NewLine() Next MeasPeriod = sum / 25.0 * 135.6e-9 ' compute average period in seconds End Function