PDA

View Full Version : Analog In for 16F88 with IR Sensor.



Kaeth05
- 1st November 2012, 22:24
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,



'----------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

norohs
- 2nd November 2012, 03:20
(newbe here)

Should that be High PORTB.4 and LOW PORTB.4?

I also don't see an ADCON1 being setup


if x > %10000000 then
PORTB.4 = 1
endif

if x < %00000111 then
PORTB.4 = 0
endif

dhouston
- 2nd November 2012, 10:23
There are two versions of the QRE1113 - one digital, one analog . Are you sure you have the analog version?

Kaeth05
- 5th November 2012, 02:46
(newbe here)

Should that be High PORTB.4 and LOW PORTB.4?

I also don't see an ADCON1 being setup


if x > 000000 then
PORTB.4 = 1
endif

if x < 000111 then
PORTB.4 = 0
endif

You're correct, for my purposes, the high / low settings should be reversed. I'm trying to read up more on how the ADConverter works, to get this circuit working. There are lots of examples of using it with a LCD on RA4, but I can't find anything more.. general.

And yes - I'm sure I have the Analog version. Also, looks like you can't edit old posts, I meant to set my thresholds at 128 on both sides, but forgot to edit it before copying in. The low threshold should be set at 128, not 7.

Kaeth05
- 5th November 2012, 03:20
I'm getting the impression that I need the following things for my 16F88


ADCON1.7 = 1

or


DEFINE ADC_BITS 10

Do I need both of these if I only want my A/D to be 8 bits? (IE: ADC_BITS 8)