I'm trying to program codes to read two analog signals, but becomes rubbish when I read in from more than one port.
only ned 8bit so I read from ADRESH only.
the analog signal is volt signal form 2 potmeters. 0-3.8volt
do I have to gnd on the not in use ports on PORTA?
read for one of the inputs is ok.
only ned 8bit so I read from ADRESH only.
the analog signal is volt signal form 2 potmeters. 0-3.8volt
do I have to gnd on the not in use ports on PORTA?
read for one of the inputs is ok.
Rich (BB code):
list p=16f876A ; list directive to define processor
#include <p16f876A.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
ADCon0 EQU H'001F'
ADCon1 EQU H'009F'
AA equ 0x2B
BB equ 0x2C
BIN equ 0x2D
#define CARRY STATUS,C
#define ZERO STATUS,Z
#define NEG FLAGS,0
#define ENABLE PORTC,0
#define RIGHT PORTC,1
#define LEFT PORTC,2
;******************************************
HP:
; Start at the reset vector
org 0x000
goto Start
org 0x004
Interrupt
retfie
Start:
bsf STATUS,RP0 ;bank 1
bcf STATUS,RP1
movlw B'00011000'
movwf TRISC ;portc [3-0 6-8] outputs
movlw H'00'
movwf TRISB ;portb [7-0] outputs
clrf ADCon1 ;left justified, all inputs a/d
bcf STATUS,RP0 ;bank 0
movlw B'11000001' ;Fosc/8 [7-6], A/D ch0 [5-3], a/d on [0]
movwf ADCon0
clrf PORTB
clrf PORTC
;******************************************
HPLOOP:
call ad_porta0
call ad_porta1
goto HPLOOP
;******************************************
ad_porta0:
movlw B'11000001' ;Fosc/8 [7-6], A/D ch0 [5-3], a/d on [0]
movwf ADCon0
bsf ADCon0,GO ;Start A/D conversion
Wait
btfsc ADCon0,GO ;Wait for conversion to complete
goto Wait
movf ADRESH,W ;write A/D result to PORTB
movwf AA ;register
return
ad_porta1:
movlw B'11011001' ;Fosc/8 [7-6], A/D ch0 [5-3], a/d on [0]
movwf ADCon0
bsf ADCon0,GO ;Start A/D conversion
Wait1
btfsc ADCon0,GO ;Wait for conversion to complete
goto Wait1
movf ADRESH,W ;write A/D result to PORTB
movwf BB ;Register
movwf PORTB ;LEDs
return
Last edited by a moderator: