I'm noodling (finally) with an SRF02 ultrasonic rangefinder, communicating via I2C with a 16F688.
I've used a similar program for an IR rangefinder, but I can't get this to work.
Blinky tests: Done
SRF02 indicator: Flashes about once per second
Scope: Shows both SDA and SCL lines active about once per second
Ultrasonic output: The SRF02 is "pinging", tested with an ultrasonic receiver connected to the scope.
Everything external appears to work except getting the comparison done and/or the yellow LED to so indicate
I've tried several variations, always coming back to a straightforward adaptation of code posted by jellis00 some time ago (thread is archived).
Where have I gone wrong?
Code:@ __config _INTOSCIO & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF & _CP_OFF & _CPD_OFF ' INPUTS: MODE VAR PORTA.3 ' MODE jumper; default is HIGH (MODE B) ' I2C INTERFACE: SDA VAR PORTA.5 ' Serial data SCL VAR PORTA.4 ' Serial clockS ' OUTPUTS: RELAY VAR PORTA.0 ' Relay driver GLED VAR PORTC.1 ' LED status indicator, green YLED VAR PORTC.2 ' LED status indicator, yellow ' VARIABLES: KLIX VAR BYTE ' Generic counter variable FAR VAR BYTE ' Analog value, MAX setting, PORTA.1 NEAR VAR BYTE ' Analog value, MIN setting, PORTA.2 RANGE0 VAR BYTE RANGE1 VAR BYTE DEFINE ADC_BITS 8 ' Number of bits in ADC result DEFINE ADC_CLOCK 3 ' Set ADC clock source to RC DEFINE ADC_SAMPLEUS 25 ' Set ADC sampling time in microseconds ANSEL =%00000110 ' Enable ADC channels AN2-AN1 CMCON0=%00000111 ' Disable the comparators OSCCON=%01100000 ' Configure oscillator 4MHz ($60) TRISA =%00001110 ' Set ports A3-A2-A1 as inputs TRISC =%00000000 ' Set all ports C as outputs RELAY=0 : KLIX=0 ' Set relay LOW and clear counter value GLED=0 : YLED=0 ' Set LEDs to OFF IF MODE=0 THEN ' Wait 5 seconds to stabilize detector FOR KLIX=1 TO 5 ' During this time: GLED=1 : PAUSE 500 ' Show MODE A by flashing green GLED=0 : PAUSE 500 NEXT KLIX ' or ELSE FOR KLIX=1 TO 5 ' Show MODE B by flashing yellow YLED=1 : PAUSE 500 YLED=0 : PAUSE 500 NEXT KLIX ENDIF GLED=1 ' Set green LED ON for "Ready" ' DETECTOR MAIN: I2CWRITE SDA,SCL,$E0,0,[80] ' Request range in inches PAUSE 100 ' Wait for ranging to finish I2CREAD SDA,SCL,$E0,2,[RANGE1,RANGE0] ' Read range ADCIN 2,FAR ' Get the maximum setting ADCIN 1,NEAR ' Get the minimum ("window") setting IF ( RANGE0 < FAR AND RANGE0 > NEAR ) THEN GLED=0 : YLED=1 ELSE GLED=1 : YLED=0 ENDIF PAUSE 1000 GOTO MAIN END






Bookmarks