I made a circuit on a pic16f887 where I read an analog input and get a digital output in PORTD with leds. The problem is the circuit doesn't work at all, unless i am touching the top of the pic. can someone tell me what's going on and what can I do to fix this? Thanks in advance.
Concerning the circuit, I'm connecting a voltage divider with a pot to AN0 and connecting both Vdd and Vss in the pic and the PORTD output to leds and resistors.
this is my code:
Concerning the circuit, I'm connecting a voltage divider with a pot to AN0 and connecting both Vdd and Vss in the pic and the PORTD output to leds and resistors.
this is my code:
Code:
LIST P=16F877A
include "p16f887.inc"
; CONFIG1
; __config 0xF0D5
__CONFIG _CONFIG1, _FOSC_INTRC_CLKOUT & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF & _LVP_ON
; CONFIG2
; __config 0xFFFF
__CONFIG _CONFIG2, _BOR4V_BOR40V & _WRT_OFF
ORG 0X00
BANKSEL TRISD
CLRF TRISD ;SETS PORTD AS OUTPUT
BANKSEL PORTD
CLRF PORTD ;SETS PORTD TO LOW
GOTO ADCON
;ADC
ADCON
;PORT CONFIG
BANKSEL TRISA
BCF TRISA ;Set RA0 to input
BANKSEL ANSEL
BSF ANSEL,0 ;SET A0 AS ANALOG INPUT
;ADC VOLTAGE REFERENCE
BANKSEL ADCON1
MOVLW B'00000000' ;LEFT JUST, VREF VDD VSS
MOVWF ADCON1
GOTO ADC1
ADC1
;CHANNEL SELECTION/CONVERSION CLOCK
BANKSEL ADCON0
MOVLW B'11000001' ;Fosc, AN0, ADC ENABLED
MOVWF ADCON0
BSF ADCON0,1 ;STARTS CONVERSION
BTFSC ADCON0,1 ;SALTA SI BIT 1 ES CERO
GOTO $-1 ;TESTS AGAIN
MOVF ADRESH,0 ;SETS ADRESH TO WREG
MOVWF PORTD;
goto ADC1
END