There is a strange behavior on the analogue inputs of the PIC16F887 I am using.
I wrote a program to read an analogue value.
If I read only one analogue value, all goes well. The analogue value is read and stored in the corresponding register.
In the example the value at RA1 read by AN1 and is stored in register named AnalogueValue01
Analogue value at => read by => stored in
RA1 => AN1 => AnalogueValue01
Now when I read more than one analogue value, the analogue value is readout by the next analogue 'port' and the value is not stored in corresponding register.
In the example I get:
Analogue value at => read by => stored in
RA1 => AN2 ??? => AnalogueValue02
RA2 => AN3 ??? => AnalogueValue03
RA3 => AN1 ??? => AnalogueValue01
Who can explain this strange behavior?
Thanks,
Nijn
I wrote a program to read an analogue value.
If I read only one analogue value, all goes well. The analogue value is read and stored in the corresponding register.
In the example the value at RA1 read by AN1 and is stored in register named AnalogueValue01
Analogue value at => read by => stored in
RA1 => AN1 => AnalogueValue01
Now when I read more than one analogue value, the analogue value is readout by the next analogue 'port' and the value is not stored in corresponding register.
In the example I get:
Analogue value at => read by => stored in
RA1 => AN2 ??? => AnalogueValue02
RA2 => AN3 ??? => AnalogueValue03
RA3 => AN1 ??? => AnalogueValue01
Who can explain this strange behavior?
Thanks,
Nijn
Code:
#include <p16F887.inc>
;external clock AT 20MHz
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V
cblock 0x20
ActivePort01
AnalogueValue01
ID01
Delay_1
endc
cblock 0x30
ActivePort02
AnalogueValue02
ID02
endc
cblock 0x40
ActivePort03
AnalogueValue03
ID03
endc
org 0
Start:
bsf STATUS,RP0 ; select Register Bank 1
movlw 0xFF
movwf TRISA ; Make PortA all input
clrf TRISB ; Make PortB all output
clrf TRISD ; Make PortD all output
movlw 0x00 ; Left Justified, Vdd-Vss referenced
movwf ADCON1
bsf STATUS,RP1 ; select Register Bank 3
movlw 0xFF ; we want all Port A pins Analog
movwf ANSEL
bcf STATUS,RP0 ; back to Register Bank 0
bcf STATUS,RP1
Main
Part1
movlw .01
movwf ID01
movlw 0x85 ; configure A2D for Fosc/32, Channel 0 (RA0), and turn on the A2D module
movwf ADCON0
movwf ActivePort01
bsf PORTB, 4 ; check delay
movlw .10 ; wait minimal 5µs to load the internal condenator( .10 = about 10µs)
call DELAY
bcf PORTB, 4 ; check delay
bsf ADCON0,GO_DONE ; start conversion
btfss ADCON0,GO_DONE ; this bit will change to zero when the conversion is complete
goto $-1
movf ADRESH,w ; Copy the display to the LEDs
movwf AnalogueValue01
movwf PORTD
movlw .100
call DELAY
; goto Main = test only one analogue value
Part2
movlw .02
movwf ID02
movlw 0x89
movwf ADCON0
movwf ActivePort02
bsf PORTB, 4
movlw .10
call DELAY
bcf PORTB, 4
bsf ADCON0,GO_DONE
btfss ADCON0,GO_DONE
goto $-1
movf ADRESH,w
movwf AnalogueValue02
movwf PORTD
movlw .100
call DELAY
Part3
movlw .03
movwf ID03
movlw 0x8d
movwf ADCON0
movwf ActivePort03
bsf PORTB, 4
movlw .10
call DELAY
bcf PORTB, 4
bsf ADCON0,GO_DONE
btfss ADCON0,GO_DONE
goto $-1
movf ADRESH,w
movwf AnalogueValue03
movwf PORTD
movlw .100
call DELAY
goto Main
DELAY
movwf Delay_1
LOOP
nop
decfsz Delay_1, f ; delay
goto LOOP
return
end
Last edited: