eem2am
Banned
intio pic12f675 gpio4
Hello,
I am writing in MPASM for PIC12F675.
My code builds fine but does not work properly.
I am using the ADC to read analog inputs on AN0 and AN1 in a loop.
Eg….Basically kind of like…..
-----------------------
Loop:
Read AN0
Read AN1
Goto Loop
-----------------------
Anyway, what actually should happen is that if the Analog conversion value in ADRESH is greater than 0x7F for the AN0 reading, then GPIO2 is taken high….if lower, GPIO2 is taken low
And if the analog conversion value in ADRESH is greater then 0x7F for the AN1 reading, then GPIO4 is taken high….if lower, GPIO4 is taken low
---------------------
However, the readings are always taken from AN1, and even though I change the channel to AN0 it still only reads AN1.
I know the AN0 channel works as I got it working in a different bit of code.
So does any reader know why I can’t change channels from AN1 to AN0 and read the analog input there in my loop ?
Here is my code…………….
-------------------------------
I am very grateful for your attentions
Hello,
I am writing in MPASM for PIC12F675.
My code builds fine but does not work properly.
I am using the ADC to read analog inputs on AN0 and AN1 in a loop.
Eg….Basically kind of like…..
-----------------------
Loop:
Read AN0
Read AN1
Goto Loop
-----------------------
Anyway, what actually should happen is that if the Analog conversion value in ADRESH is greater than 0x7F for the AN0 reading, then GPIO2 is taken high….if lower, GPIO2 is taken low
And if the analog conversion value in ADRESH is greater then 0x7F for the AN1 reading, then GPIO4 is taken high….if lower, GPIO4 is taken low
---------------------
However, the readings are always taken from AN1, and even though I change the channel to AN0 it still only reads AN1.
I know the AN0 channel works as I got it working in a different bit of code.
So does any reader know why I can’t change channels from AN1 to AN0 and read the analog input there in my loop ?
Here is my code…………….
Code:
;******************************************
;PIC12F675
;MPASM assembler
;MPLAB 8.10
;Programmer = Pickit2
;Application version 2.11.00
;Device File Version 1.12.00
;OS Firmware version 2.20.01
;Internal Oscillator at 4MHz
;--------------------------------------
;PIN USAGE
;AN0 = analog input;
;AN1 = analog input;
;RA2 = digital output;
;RA4 = digital output;
;RA5 = digital input;
;--------------------------------------
#include <p12f675.inc>
LIST P=PIC12F675
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BODEN_OFF & _CPD_OFF)
cnt0 equ 0x20
cnt1 equ 0x21
cnt2 equ 0x22
cnt3 equ 0x23
ccd0 equ 0x24
val0 equ 0x25
val1 equ 0x26
ded0 equ 0x27
ded1 equ 0x28
BANK0 macro
bcf STATUS,RP0
endm
BANK1 macro
bsf STATUS,RP0
endm
FET_ON macro
bsf GPIO,2
endm
FET_OFF macro
bcf GPIO,2
endm
ENABLE macro
bsf GPIO,4
endm
DISABLE macro
bcf GPIO,4
endm
ENABLE_ADC macro
bsf ADCON0,0
nop
endm
DISABLE_ADC macro
bcf ADCON0,0
nop
endm
ADC_CH0 macro
bcf ADCON0,3
nop ;nop to avoid RMW problem
bcf ADCON0,2
nop
endm
ADC_CH1 macro
bcf ADCON0,3
nop ;nop to avoid RMW problem
bsf ADCON0,2
nop
endm
DO_ADC macro
bsf ADCON0,1
nop
endm
org 0
goto Start
Start:
FET_OFF
DISABLE
BANK1
;Set up ANSEL register (set analog/digi ports
;..set up AD conversion clock)
movlw 0x63
movwf ANSEL
;Sort comparator reference out even though
;i dont want to use the comparator
movlw 0x00
movwf VRCON
movlw 0x2B
movwf TRISIO
BANK0
;Disable interrupts as i don't want to use them
bcf INTCON,GIE
bcf PIR1,ADIF
;Sort comparator out even though i dont
;want to use it.
movlw 0x06
movwf CMCON
;The comparator ref must still be enabled
;even though i dont want to use the comparator
BANK1
bsf VRCON,VREN ;Enable comparator ref
BANK0
;Set up ADCON0 = Ljust; Vdd = ref; xx; CHS = 00; GODONE; Disable
movlw 0x00
movwf ADCON0
ENABLE_ADC
FET_OFF
DISABLE
;VVVVVVVVVVVVVVVVVVVVV
;* MAIN PROGRAM LOOP *
loop:
;...........................
;Do ADC on channel 0
bcf PIR1,ADIF
nop
DISABLE_ADC
ADC_CH0 ;Change ADC channel
ENABLE_ADC
call ADC_channel_changeover_delay
D0_ADC
egg:
btfsc ADCON0,1
goto egg
;............................
movf ADRESH,0
movwf val0
movlw 0x7F
subwf val0,0
btfsc STATUS,C
FET_OFF
movlw 0x7F
subwf val0,0
btfss STATUS,C
FET_ON
;............................
;............................
;----------------------------
;Do ADC on channel 1
bcf PIR1,ADIF
nop
DISABLE_ADC
ADC_CH1 ;Change ADC channel
ENABLE_ADC
call ADC_channel_changeover_delay
DO_ADC
dog:
btfsc ADCON0,1
goto dog
;............................
movf ADRESH,0
movwf val1
movlw 0x7F
subwf val1,0
btfsc STATUS,C
DISABLE
movlw 0x7F
subwf val0,0
btfss STATUS,C
ENABLE
;----------------------------
goto loop
;* END OF MAIN PROGRAM LOOP *
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ADC_channel_changeover_delay:
movlw 0x0F
movwf ccd0
fff:
decfsz ccd0,1
goto fff
retlw 0x00
end
I am very grateful for your attentions