PIC16F676 troubles


Closed Thread
Results 1 to 8 of 8
  1. #1

    Default PIC16F676 troubles

    I've chosen the 16F676 for size and cost and it also has an ADC which I also need. I need to use all the I/Os and I need to use PORTA.3 (MCLR) as a digital input to be used with a button. The "big" program isn't working so I've broken it down into smaller parts to make sure everything is working correctly. Below is the code to use the button to toggle an LED on and off. The start up works and the button turns the LED on, but only one time. Once the LED is toggled on for the first time, it gets stuck. What am I doing wrong?

    Code:
    #config 
        __CONFIG _CP_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF
    #endconfig
    
    
    TRISA = 001000
    
    
    LED var PORTA.1
    PUSHBUTTON var PORTA.3
    
    
    '***********************start up just to make sure LED is working**************************
    led = 1
    pause 100
    led = 0
    pause 100
    led = 1
    pause 100
    led = 0
    '***************************************************************************************
    main:
    
    
        if PUSHBUTTON = 0 then 
            
            toggle led
            pause 25
            
        endif
        
    goto main
    end

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    Well actually, If you were to put a scope on the led pin you should see it toggling with a period of 50 Milliseconds while the push button is being pressed. I assume you have a pullup resistor connected to +5 volts and the Porta.3 pin?
    Dave Purola,
    N8NTA
    EN82fn

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    I see your point. But the flaw in the code is not the underlying problem. It should flicker while the button is held down. And then land on ON or OFF 50:50, right? I changed the pause to 1000 and it's still the same. Yes, I have the a 10k pull up resistor to Vdd. Does the config section look correct? For some reason, the TRIS reg didn't cut and past completely. It should be

    TRISA = %11001000

    Does that look correct?
    Do I need to do something with the C registry?

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    I don't see where you are configuring the analog select register ANSEL.
    By not explicitly setting the value of this register the PIC is using the default value, which might be enabling some pins as analog.

    Check the Datasheet to verify how you want them set and make sure you add the appropriate ANSEL = xxx statement to the beginning of your program.

    That may be causing you a problem.
    I have found it is usually a good idea to check and set the registers for all of the hardware modules.

    Might be worth checking.
    Regards,
    TABSoft

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    I've tried several things with the registers, but no luck. I've also added a sub to make it behave the way it should:

    Code:
    #config 
        __CONFIG _CP_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT& _MCLRE_OFF
    #endconfig
    
    ADCON1 = %00000000
    ANSEL = %00000100
    CMCON = 0      
    
    TRISA = %11001100
    TRISC = %00000000
    
    LED var PORTA.1
    PUSHBUTTON var PORTA.3
    
    
    led = 1
    pause 100
    led = 0
    pause 100  
    led = 1
    pause 100
    led = 0
    
    main:
    
        if PUSHBUTTON = 0 then 
            toggle LED
            pause 25
            gosub buttonrelease
            pause 25
        endif
        
    goto main
    end
    
    buttonrelease:
    
        do until PUSHBUTTON = 1
            pause 10
        loop
        
        return
    It still does the same thing. The LED flashes on power up. The button works once. And then it locks up. And I've tried several other "lower level" ways of making the LED toggle instead of the TOGGLE statement. It's always the same. Here's something interesting, though. If I change the code to just make the LED come on when the button is pressed instead of trying to toggle it like so:

    Code:
    main:
    
           if PUSHBUTTON = 0 then
                  LED = 1
           elseif PUSHBUTTON = 1 then
                  LED = 0
           endif
    
    goto main
    end
    Everything works perfectly! So I'm assuming the problem isn't with the MCLR pin or the registries. Do I need to define the oscillator?

  6. #6
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    I think the issue is the Comparator module is still turned on.

    Your CMCON = 0 sets the comparator to Reset with RA0 and RA1 connected to the Comparator module.
    Have a look at the DS Figure 6-2 which shows the possible 8 modes of the comparator.
    You should try setting CMCON = %00000111
    Regards,
    TABSoft

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    That did the trick. Thanks, Tabsoft!

  8. #8
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: PIC16F676 troubles

    Good deal.

    I suspect with the Comparator module set to 000 whenever you perform a Toggle command this actually reads the port then writes the new value to it. This issue is that the port will always read 0 when it is configured in Reset mode.
    Last edited by Tabsoft; - 28th May 2015 at 23:06.
    Regards,
    TABSoft

Similar Threads

  1. PIC16F676 RS232 baud rate
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2010, 14:16
  2. PIC16F676 Problem
    By Atom058 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 19th April 2007, 16:05
  3. Interfacing RTC (DS1302) with pic16F676
    By Andrew_A in forum Off Topic
    Replies: 1
    Last Post: - 14th February 2006, 08:49
  4. PIC16F676 I/O Problems.
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 7th March 2005, 16:37
  5. PIC16F676 Troubles
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 27th February 2005, 10:01

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts