Circuit only works when touching pic (ADC)

Thread Starter

Alromaru

Joined Dec 10, 2017
4
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:
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
 

be80be

Joined Jul 5, 2008
2,072
_FOSC_INTRC_CLKOUT Hows that work there has to be a crystal on a 16F877A
What crystal do you have ?
That code will not work at all you got it for the wrong chip

should work
Code:
; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
;LIST P=16F877A
#include "p16f877a.inc"

; CONFIG
; __config 0xFF3A
__CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

MAIN_PROG CODE                      ; let linker place main program

START
BANKSEL TRISD
CLRF TRISD        ;SETS PORTD AS OUTPUT
BANKSEL PORTD
CLRF PORTD        ;SETS PORTD TO LOW
GOTO ADCON
;ADC
ADCON
;PORT CONFIG
BANKSEL TRISA
MOVLW B'00000001'
MOVWF TRISA          ;Set RA0 to input
;ADC VOLTAGE REFERENCE
BANKSEL ADCON1
MOVLW B'10001111'     ;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
 
Last edited:

Thread Starter

Alromaru

Joined Dec 10, 2017
4
_FOSC_INTRC_CLKOUT Hows that work there has to be a crystal on a 16F877A
What crystal do you have ?
That code will not work at all you got it for the wrong chip
]
My bad, I didn't notice that, thanks!! I changed the that part of the code, but I still have the same problem.

Code:
LIST P=16F887
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
 

Thread Starter

Alromaru

Joined Dec 10, 2017
4
He's using a PIC16F887 not a PIC16F877. I was that the chip was possibly not making a good connection with the socket.

Les.
It's well connected, because it only works when I touch it with my fingers,not with some plastic. I was thinking maybe it has something to do with grounding. I now got it to work by touching some of the PORTA pins. do I have to ground all PORTA pins even though not used in the code??
 

spinnaker

Joined Oct 29, 2009
7,830
I have to ground all PORTA pins even though not used in the code??
What does your datasheet say? This is what mine says for the current pic I am working on.

Unused I/O pins should be configured as outputs and
driven to a logic low state. Alternatively, connect a 1 kΩ
to 10 kΩ resistor to VSS on unused pins and drive the
output to logic low.
 

spinnaker

Joined Oct 29, 2009
7,830
It's well connected, because it only works when I touch it with my fingers,not with some plastic.
If you need to touch it to get it to work, t is a design issue or connection issue. Possible you might be a bad chip but unlikely. I would still like to know what "does not work" means.
 

AlbertHall

Joined Jun 4, 2014
12,345
If it only runs with your finger on the chip my first thought would be that the oscillator is not running. From the config bits you are using the internal oscillator. Please make sure which chip is actually in your circuit.
PIC16F887 has an internal oscillator.
PIC16F877 does not have an internal oscillator.
 

spinnaker

Joined Oct 29, 2009
7,830
If it only runs with your finger on the chip my first thought would be that the oscillator is not running. From the config bits you are using the internal oscillator. Please make sure which chip is actually in your circuit.
PIC16F887 has an internal oscillator.
PIC16F877 does not have an internal oscillator.
We don't know if the chip is running of not. All we know is "circuit doesn't work at all". That can mean anything. I don't see how a chip with no internal OSC could be caused to work if you touch it. Chip has to have an internal OSC.
 

be80be

Joined Jul 5, 2008
2,072
He's using a PIC16F887 not a PIC16F877. I was that the chip was possibly not making a good connection with the socket.

Les.
Read the top of his code it said list 16f877a
Screenshot from 2017-12-10 07-35-13.png

I kind of went with the list seeing it was at the top of his code but
yes a 16f887 has one inosc code still wouldn't work as posted for that chip too
 

AlbertHall

Joined Jun 4, 2014
12,345
We don't know if the chip is running of not. All we know is "circuit doesn't work at all". That can mean anything. I don't see how a chip with no internal OSC could be caused to work if you touch it. Chip has to have an internal OSC.
If the chip has no internal oscillator and the oscillator pins are not connected then they may pick up enough AC from the finger to drive the chip with some sort of clock signal.

There is clearly some confusion about the chip number so it makes sense to confirm what the chip number actually is.
Those two numbers are easily confused.
 

Thread Starter

Alromaru

Joined Dec 10, 2017
4
1. Post your schematic.
upload_2017-12-10_6-35-33.png
2. How is your pic connected? PCB? Perfboard? Breadboard? Zif socket ? Regular socket? SMD mouunted?
I'm using a breadboard
What does "not working at all" mean? Is the Pic running?
What I meant is i don't get any output in PORTD to light the LEDs.
If you need to touch it to get it to work, t is a design issue or connection issue. Possible you might be a bad chip but unlikely. I would still like to know what "does not work" means.
I got it to work now by grounding pin 36 (RB3/AN9/PGM/C12IN2-). My question now would be, why this specific pin? How can i know which pins are required to be grounded.
If it only runs with your finger on the chip my first thought would be that the oscillator is not running. From the config bits you are using the internal oscillator. Please make sure which chip is actually in your circuit.
PIC16F887 has an internal oscillator.
PIC16F877 does not have an internal oscillator.
I'm using a PIC16F887 and now get a reading of 1MHz.
upload_2017-12-10_6-35-33.png
 

spinnaker

Joined Oct 29, 2009
7,830
I got it to work now by grounding pin 36 (RB3/AN9/PGM/C12IN2-). My question now would be, why this specific pin? How can i know which pins are required to be grounded.
Read your datasheet. My guess when you grounded the pin you indirectly fixed something else that was actually wrong.

Just to clarify. It was running but the code was just not working the way you expected?? Did you use your debugger to figure out exactly what was wrong?
 

philba

Joined Aug 17, 2017
959
It's been a while since I've done PICs - does_MCLRE_OFF in CONFIG1 default to a 0 or 1? If the pin is config'd as MCLR then it shouldn't be flapping in the breeze. The described behavior seems consistent with a floating MCLR pin. Either pull it high or confiure MCLR as internal. Page 210 of the datasheet.
 
Top