I'm having difficulty getting the following code to work. I have it wired such that the Sensor (QRE1113 Sparkfun) inputs to RA1/AN1, and outputs to a LED on RB4. I have verified the functionality with a digital signal (pushbutton), but cannot get it to work in Analog.
Even now, my push button works, using the following code, but the sensor does not. Using a Voltmeter, my sensor currently provides a range of about 1.7-4.6 Volts, so hopefully it should switch whenever it crosses 2.5.
I feel that it may be one or a combination of the following:
- Not Defining my Analog controls correctly / completely
- Not having a reference voltage (maybe?)
- Not understanding how to define 8 bit thresholds properly.
Any assistance appreciated, and thank you for your time,
Code:
'----------Variables------------
x var byte ' Byte for potentiometer input
'----------Initialization---------
TRISB = %00000000
TRISA = %00000010
'DEFINE OSC 8 ' Sets the internal clock speed to 2 MHz
'OSCCON.4 = 1
'OSCCON.5 = 0
'OSCCON.6 = 1
ANSEL = %00000010 ' Leaves AN1 in analog mode, all else digital.
'--------Main Code--------
myloop:
adcin 1, x ' Read analog voltage on AN1 and
' convert to 8-bit digital value
' and store as x.
if x > %10000000 then
PORTB.4 = 1
endif
if x < %00000111 then
PORTB.4 = 0
endif
goto myloop ' Go to loop label
end
Bookmarks