' {$STAMP BS2} ' {$PBASIC 2.5} '-----Define variables----- x VAR Word cnt VAR Word pulw VAR Word scale VAR Nib '-----Constant declarations------ HIMARK CON 8000 'trip points for autoranging, there is nothing magical about the values LOMARK CON 200 'chosen for HIMARK and LOMARK, these values may be optimized for the application S0 PIN 0 'Sensitivity selection pins S1 PIN 1 IN CON 2 'TSL230 output pin '-----Program Start----- LOW S0 'set pins to output by initalizing them low LOW S1 scale = %0010 'Set sensitivity pins (S0,S1) to x10 IF scale.BIT1 THEN HIGH S1 ELSE LOW S1 IF scale.BIT0 THEN HIGH S0 ELSE LOW S0 '-----Main program loop----- FOR x = 1 TO 2000 'autoranging code PULSIN IN, 0, pulw 'measure pulse width IF pulw > HIMARK OR pulw = 0 THEN 'if pulse width is greater than 16ms increase sensitivity scale = (scale + 1) MAX 3 IF scale.BIT1 THEN HIGH S1 ELSE LOW S1 IF scale.BIT0 THEN HIGH S0 ELSE LOW S0 ELSEIF pulw < LOMARK THEN 'if pulse width is less than 400us decrease sensitivity scale = (scale - 1) MIN 1 IF scale.BIT1 THEN HIGH S1 ELSE LOW S1 IF scale.BIT0 THEN HIGH S0 ELSE LOW S0 ENDIF 'measurement code COUNT IN, 100, cnt 'COUNT on pin 3 FOR 100 ms, store value in cnt 'display results DEBUG DEC cnt 'Send value of cnt, as a decimal to the computer 'display multiplication factor, multiply this by the value to obtain a measurement valid over entire range SELECT scale CASE %01 DEBUG " x 100", CR CASE %10 DEBUG " x 10", CR CASE %11 DEBUG CR ENDSELECT NEXT 'Do it all over again, 2000 times END