PIC 16F676 problem

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
I am programming a PIC 16F676 and have come across a problem. I am using a 20MHz ceramic as the oscillator, but the problem persists even if I use the internal 4MHz one.

You can see from the initialisation code below that I want to set PORTA 1 & 2 as inputs with pull-ups. PORTA,2 works OK, but PORTA,1 does not function at all. It uses a pushbutton to ground and I wrote the bit of code at the end to test it.

I used a digital probe to test the pins. It has two LEDs for high or low states and no LEDs lit indicates no connection. PORTA,1 shows as no connection. I fitted an external pull-up and the probe showed the operation of the pushbutton at the pin to be OK. But still the test does not work.

I have two 16F676 chips and they both behave the same. So either I have two chips with identical faults or I have a fault in my program. Any help would be most welcome.
Code:
main  ;initialise ****** set up at switch on
     bcf     STATUS,5  ; select Bank 0   
   clrf      PORTA       ;init   
 ;set up CMCON comparitor mode
     ;input to CIN+
     ;use int ref, and output to COUT     
   movlw   b'00000100'  
movwf   CMCON
 ;set up ANSEL   
   bsf     STATUS,5     ;Select Bank 1
; select bits for analogue (PA,0 and PC,3) (CIN+ & AN7)
   movlw   b'10000001'   
     ;1 = analogue;  0 - digital
   movwf   ANSEL
       
;set PORTA  0, 1, 2, 3 , 5 as input.  set 4  as output for Xtal    
   movlw    b'101111'   ;set port A in/out
   movwf   TRISA
   movlw   b'000110'
   movwf   WPUA   ;pullups on 1 & 2
   
;set PORTC  0, 3, as input.  Set the rest as outputs
     bcf     STATUS,5  ;select Bank 0     
     clrf      PORTC       ;init     
   bsf     STATUS,5     ;Select Bank 1   
   movlw    b'001001'   ;set port C  in/out
   movwf   TRISC

;set up OPTION timer0 prescaler  
   bsf     STATUS,5     ;Select Bank 1   
   movlw   b'00000001'   
   ;   b'0-------'     ;pullups enabled  
   ;   b'----0---'     ;prescaler to timer mode   
   ;   b'-----001'     ;set prescaler = 4,  
   movwf   OPTION_REG
     
 ;set up VRCON comparitor ref voltage
   bsf     STATUS,5     ;Select Bank 1   
   movlw   b'10001111'
   ;   b'1x-x----'     ;CV power on
   ;   b'-x0x----'     ;high range
   ;   b'-x-x1111'     ;set at 3.59V  
   movwf   VRCON       

 ;set up for A/D    ;(ADCON0)
   bcf     STATUS,5     ;select Bank 0
   movlw   b'00011100'
   ;   b'—x111--'     ;select AN7 as input
   movwf   ADCON0

;set up Timer0  
   bcf     STATUS,5  ; select bank 0  
   movlw   b'10100000'     ;
   ;   b'1-------'     ;enable interrupt
   ;   b'--1-----'     ;enable TMR0 interrupt
   movwf   INTCON
   
 ;set up Timer1
   bcf  STATUS,5  ; select Bank 0   
   movlw  b'01000000'
   ;   b'x1------'     ;timer gate enable
   movwf   T1CON    

setup   ;test  for fault
   btfss   PORTA,1
   goto   setup1
   goto   setup2   
setup1
   bsf     PORTC,2     ;high   
   goto   setup
setup2
   bcf     PORTC,2    ;low
   goto    setup
 

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
I understand RAPU is bit7 of OPTION_REG, and I have cleared it.
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
You could try clearing porta before you start your test routine. I have known porta to be a bit funny like that.
 

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
The LED has not been fitted yet. I was testing the output at PortC,2 pin where the audio output would be taken from. Toggling the voltage at PortA,1 did not toggle the output at PortC,2,

But when i changed the test to bit test PortA,2 then PortC,2 toggled OK.

I cleared PortA in the second line of the initialisation. Do you think I need to do it again before the test?
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
Code:
bsf     STATUS,5     ;Select Bank 1
movlw    b'101111'   ;set port A in/out
movwf   85h
movlw   b'000110'
movwf   95h             ;pullups on 1 & 2
bcf     STATUS,5     ;select Bank 0
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
The LED has not been fitted yet. I was testing the output at PortC,2 pin where the audio output would be taken from. Toggling the voltage at PortA,1 did not toggle the output at PortC,2,

But when i changed the test to bit test PortA,2 then PortC,2 toggled OK.

I cleared PortA in the second line of the initialisation. Do you think I need to do it again before the test?

I have had issues before with porta and have had to clear the port in the main code. Give it a try.
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
It is a very insensitive circuit because the chip can only detect 1mV whereas a transistor can detect 0.001 mV.
 

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
Adam,
Tried clearing PortA immediately before the test. It made no difference.
This exercise is too time consuming at the moment, so I am now going to use PortC,0 with external pull-up as input from the push button.
I'll try and sort the problem out later.

Colin,
T2 is pulsed. When it turns off the coil generates a +ve pulse whose voltage is limited by the internal zener in T2. Thereafter the energy decays via R4 and R5. The comparitor effectively measures the voltage across the coil.
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
We are not worrying about pulsing the coil.
We are worrying about picking up a received signal.
The first thing you do is create a program that just turns on a LED when the switch is pressed.
 

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
We are worrying about picking up a received signal.
What received signal?
The first thing you do is create a program that just turns on a LED when the switch is pressed
Surely, that was what my test program did?
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
"What received signal?"
I thought you said this what supposed to be a metal detector.

"Surely, that was what my test program did?"
I thought you said the LED was not fitted.
 

pebe

Sep 3, 2013
83
Joined
Sep 3, 2013
Messages
83
A PI metal detector fires a pulse into a damped coil. It normally measures the amplitude of a response some time after the pulse has ended to indicate the presence of metal.

There is another school of thought that says the rate-of-change of voltage across the coil changes if metal is present. So this experimental detector is to test out that theory by measuring the time taken for the voltage across the coil to reduce to some specific level,

Re. the LED. I measured the voltage directly at the PIC's pin8
 

Colin Mitchell

Aug 31, 2014
1,416
Joined
Aug 31, 2014
Messages
1,416
"There is another school of thought that says the rate-of-change of voltage across the coil changes if metal is present. So this experimental detector is to test out that theory by measuring the time taken for the voltage across the coil to reduce to some specific level,"

That's for a big lump near the coil
We are talking about a nugget 15cm from the coil.
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
Did you see if PORTA 1 & 2 are analog pins. If they are, it will not function unless you clear the analog bits.
 
Top