PDA

View Full Version : ADC Problem with PIC16F88 Configuration



robert.quaas
- 9th April 2009, 05:56
I am using PortA.0, PortA.1 and PortA.2 to read the individual values for my line following sensor. The sensor is a TCRT5000 device x 3.

Here is my code, any ideas please ?

@ DEVICE XT_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF

DEFINE LOADER_USED 1
Include "Modedefs.Bas"

Define OSC 4 ' We're using a 4MHz Resonator


define DEBUG_REG PORTB ' DEBUG PORT
DEFINE DEBUG_BIT 6
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_PACING 500

CLR CON 1 ' CLR LCD command
LINE1 CON 128 ' LCD line #1
LINE2 CON 192 ' LCD line #2
LINE3 CON 148 ' LCD line #3
LINE4 CON 212 ' LCD line #4
INS CON 254 ' LCD command mode parameter


DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds

TRISA = %11111111 'RA.0:3 Input
TRISB = %00000000 'all PORTB output

'ANSEL = %00000001 'only porta.0 is analogue
ADCON1 = %00000010 'right justify the result(top bits are read as zero)
CMCON = 7 'Turn off comparators

piezo var portb.7
LOW PIEZO

msensor var word
lsensor var word
rsensor var word

' motor driver logic to 0 - low all port pins
low portb.0
low portb.1
low portb.3
low portb.4
pause 500

init:

DEBUG INS,CLR
pause 30

DEBUG INS,LINE1,"L= R= " : PAUSE 30
DEBUG INS,LINE2,"M= " : pause 30

checkit:

ANSEL = %00000001 ' Only porta.0 is analogue
ADCON0 = %01000101 ' Control register 000 RA0/AN0

adcin porta.0,lsensor
DEBUG INS,LINE1+3, dec lsensor," " : pause 30

'--------------------------------------------------------------------

ANSEL = %00000010 'only porta.1 is analogue
ADCON0 = %01001101 ' Control register 000 RA1/AN1

adcin porta.1,msensor
DEBUG INS,LINE2+3, dec msensor," " : PAUSE 30

'--------------------------------------------------------------------

ANSEL = %00000100 'only porta.2 is analogue
ADCON0 = %01010101 ' Control register 000 RA1/AN1

adcin porta.2,rsensor
DEBUG INS,LINE1+11, dec rsensor," " : pause 30

goto checkit

mister_e
- 9th April 2009, 16:52
ADCIN must use the channel number, not the PIN name.

ADCIN 0, YourVar ---> read PORTA.0, AN0
ADCIN 2, YourVar ---> read PORTA.2, AN2
ADCIN 6, YourVar ---> read PORTB.7, AN6

HTH

mister_e
- 9th April 2009, 16:54
You don't need to redefine ADCON0 and ANSEL all the time, only once at the top, and ADCIN should handle everything for you.

robert.quaas
- 9th April 2009, 17:45
OK, I got it....

I ended up doing like this in the end :

@ DEVICE XT_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF

DEFINE LOADER_USED 1
Include "Modedefs.Bas"

Define OSC 4 ' We're using a 4MHz Resonator


define DEBUG_REG PORTB ' DEBUG PORT
DEFINE DEBUG_BIT 6 ' *** Debug pin Bit ***
DEFINE DEBUG_BAUD 2400 ' *** Debug Baud Rate ***
Define DEBUG_MODE 1 ' Set Serial Mode 0=True, 1=Inverted
Define DEBUG_PACING 500 ' Delay 'in Us' between characters sent

CLR CON 1 ' CLR LCD command
LINE1 CON 128 ' LCD line #1
LINE2 CON 192 ' LCD line #2
LINE3 CON 148 ' LCD line #3
LINE4 CON 212 ' LCD line #4
INS CON 254 ' LCD command mode parameter

' ** Port configurations **

TX VAR PORTB.5 ' Bootloader connection for TX
RX VAR PORTB.2 ' Bootloader connection for RX
PIEZO var PORTB.7 ' On board Piezo
IR_REC VAR PORTA.3 ' On board Infrared Receiver

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds


LOW PIEZO

' motor driver logic to 0 - low all port pins
low portb.0
low portb.1
low portb.3
low portb.4
pause 500

' *** Clear Serial LCD ***
DEBUG Ins,clr : PAUSE 30

' *** Setup Line 1 and Line 2 ***
DEBUG Ins,LINE1,"Left Mid Right" : PAUSE 30
DEBUG ins,LINE2," " : PAUSE 30

TRISA = %11111111 ' All PORTA Inputs
TRISB = %00000000 ' All PORTB output

ANSEL = %00000111 ' Set 0,1,2 to A/D, A/D clock = Frc

RESULT VAR BYTE[3] ' 3-BYTE ARRAY STORAGE FOR 3 A/D CONVERSIONS
C VAR BYTE ' ADC CHANNEL NUMBER BYTE VARIABLE

MAIN:
FOR C = 0 TO 2 ' 3-CHANNEL COUNTER LOOP
ADCIN C, RESULT[C] ' Read all 3 channels
PAUSE 10 ' Pause 10mS
NEXT C
PAUSE 250 ' PAUSE 250mS


DEBUG ins,LINE2,dec RESULT[0]," "
debug ins,line2+6,dec RESULT[1]," "
debug ins,line2+11,dec result[2]," "

GOTO MAIN ' DO IT AGAIN

END ' END PROGRAM

robert.quaas
- 9th April 2009, 17:46
Thank you for your help MR. E :cool: