Connect with us

PIC 16F676 problem

Discussion in 'Microcontrollers, Programming and IoT' started by pebe, Jan 23, 2016.

Scroll to continue with content
  1. pebe

    pebe

    83
    11
    Sep 3, 2013
    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
    
     
  2. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    Post the circuit diagram please.
    Adam
     
  3. pebe

    pebe

    83
    11
    Sep 3, 2013
    It's a dual mode metal detector metal detector 10B.GIF

    Ignore R6
     
  4. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    I can't see that you have cleared the RAPU bit?
    Adam
     
  5. pebe

    pebe

    83
    11
    Sep 3, 2013
    I understand RAPU is bit7 of OPTION_REG, and I have cleared it.
     
  6. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    You could try clearing porta before you start your test routine. I have known porta to be a bit funny like that.
     
  7. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    So you say porta,2 is ok. You have had your test code working and turning on the LED yes?
     
  8. pebe

    pebe

    83
    11
    Sep 3, 2013
    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?
     
  9. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    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
    
     
  10. Arouse1973

    Arouse1973 Adam

    5,165
    1,087
    Dec 18, 2013
    I have had issues before with porta and have had to clear the port in the main code. Give it a try.
     
  11. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    It is a very insensitive circuit because the chip can only detect 1mV whereas a transistor can detect 0.001 mV.
     
  12. pebe

    pebe

    83
    11
    Sep 3, 2013
    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.
     
  13. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    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.
     
  14. pebe

    pebe

    83
    11
    Sep 3, 2013
    What received signal?
    Surely, that was what my test program did?
     
  15. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    "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.
     
  16. pebe

    pebe

    83
    11
    Sep 3, 2013
    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
     
  17. Colin Mitchell

    Colin Mitchell

    1,417
    313
    Aug 31, 2014
    "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.
     
  18. pebe

    pebe

    83
    11
    Sep 3, 2013
    Is specific information available?
     
  19. Amar Dhore

    Amar Dhore

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

    pebe

    83
    11
    Sep 3, 2013
    Both bits were cleared as digital in ANSEL.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...
Electronics Point Logo
Continue to site
Quote of the day

-