Author Topic: PIC12F629 interrupt not working  (Read 7310 times)

0 Members and 1 Guest are viewing this topic.

Offline Bibiricat

  • Contributor
  • Posts: 43
PIC12F629 interrupt not working
« on: September 30, 2013, 10:45:22 pm »
Hello.
I am having problems configuring  an interrupt on PIC12F629 on button press and i hope that someone can help me.
 The code in main loop is a button press test, and  all i want to do is toggle and LED on/off.

This is the code:
Code: [Select]
/*****************************************************/
/* PIC12F629 with hitech c compiler
/*****************************************************/

#include <htc.h>
#include <delays.h>

#define _XTAL_FREQ  4000000

__CONFIG(MCLRDIS & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);
 
// Config: int reset, no code protect, no brownout detect, no watchdog,
 //         power-up timer enabled, 4MHz int clock

/*******************************************************************/


interrupt isr( )
 {
if(INTF==1)
{
INTF = 0;
if(GPIObits.GPIO1==0)GPIObits.GPIO1=1;
else GPIObits.GPIO1=0;
}
 }


void main(void) {
TRISIO = 0b000100; // Configure all I/O.

// option_reg
INTEDG = 1; //1 = Interrupt on rising edge of GP2/INT pin

// INTCON
GIE = 1;    // GIE: Global Interrupt Enable bit
// 1 = Enables all unmasked interrupts
PEIE = 1; // PEIE: Peripheral Interrupt Enable bit
// 1 = Enables all unmasked peripheral interrupts
INTE = 1; // INTE: GP2/INT External Interrupt Enable bit
// 1 = Enables the GP2/INT external interrupt
INTF = 0; // INTF: GP2/INT External Interrupt Flag bit
// 1 = The GP2/INT external interrupt occurred (must be cleared in software)


while (1) {
if (GPIObits.GPIO2==0)
{
GPIObits.GPIO0=1;
}
else GPIObits.GPIO0=0;

// __delay_ms(1000);
}
}


« Last Edit: September 30, 2013, 11:09:22 pm by Bibiricat »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PIC12F629 interrupt not working
« Reply #1 on: September 30, 2013, 10:54:56 pm »
Quote
I am having problems

Obviously, the "problems" are top secret.


:)
================================
https://dannyelectronics.wordpress.com/
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #2 on: September 30, 2013, 11:36:58 pm »
Can you make the LED blink by itself so you know the chip is running?
 

Offline adam1213

  • Regular Contributor
  • *
  • Posts: 119
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #3 on: September 30, 2013, 11:37:25 pm »
Do you have an in circuit debugger (e.g. pickit)? If you do you can set a breakpoint in the interrupt and see if the interrupt is ever entered.

I wonder if the switch bouncing is part of the problem. It is probably not the main problem though as the LED would toggle at least some times.
 

Offline Bibiricat

  • Contributor
  • Posts: 43
Re: PIC12F629 interrupt not working
« Reply #4 on: October 01, 2013, 10:26:45 am »
The program never enters the interrupt, i changed so that an LED would just light up, and it doesn't.
The problems can be ether in the __config , or the interrupt config.
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #5 on: October 01, 2013, 10:38:25 am »
Show us how the chip is hooked up.  We're only guessing from your code.

You have stuff going on in the ISR as well as the main loop.  Do just one or the other for a start.

Do you have reset tied high?

Can you make the LED blink in the main loop with some delays?

Unless you start with these basic steps, nobody will be able to help further.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PIC12F629 interrupt not working
« Reply #6 on: October 01, 2013, 10:38:38 am »
Quote
The program never enters the interrupt

It does: put a scope on GPIO1 and you will see.

It is the rest of your code that's faulty.
================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PIC12F629 interrupt not working
« Reply #7 on: October 01, 2013, 10:43:13 am »
Quote
Unless you start with these basic steps, nobody will be able to help further.

Most of the times, it is because of people's unwillingness to read the datasheet, their inability to understand the datasheet or bad programming habit.
================================
https://dannyelectronics.wordpress.com/
 

Offline lapm

  • Frequent Contributor
  • **
  • Posts: 558
  • Country: fi
Re: PIC12F629 interrupt not working
« Reply #8 on: October 01, 2013, 10:55:07 am »
By my limited experience on controllers i would recommend reading datasheet again...

Understand on paper how interrupt system works, how timers, etc... effect on it. It not rocket science, but requires accuracy.

Common pitfalls on my programs in past:

-Timer set up incorrectly, was doing something else then what expected...
-Interrupt set up incorrectly, forgot to enable them...
-Interrupt routine itself, does it do what datasheet describes as minimal work to enable interrupt again for next time... Forgot this myself once... Took me a day to find, even when it was right there under my eyes in source code....
-Rest of the chip configured properly? Output pins set as outputs, etc...
Electronics, Linux, Programming, Science... im interested all of it...
 

Offline adam1213

  • Regular Contributor
  • *
  • Posts: 119
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #9 on: October 01, 2013, 11:01:31 am »
    Configuring an interrupt for a pin can be some times be difficult as there can be multiple issues. e.g. a pin configured as analogue / as an input / for another purpose / issues with resetting interrupts.

    Try setting INTF = 1 and see if the interrupt gets called due to INTF being 1.


    I am not entirely sure why interrupts are not working though there are some things in your code that could be improved. However doing so is unlikely to make interrupts work:
    • "interrupt isr( )" - this is an odd function definition. Normally c functions must have a return type specified. I would assume "interrupt" is a compiler directive rather than a return type. A better definition is "void interrupt isr(void)"
    • "if(INTF==1)" could be written as "if(INTF)"
    • "INTE = 1;  INTF = 0;" these lines are in the wrong order. Once the interrupt is enabled it will be called if INTF = 1. The code should clear the interrupt flag before enabling the interrupt. This assumes you wish to ignore prior interrupts * unless the datasheet says to do otherwise.
« Last Edit: October 01, 2013, 11:03:35 am by adam1213 »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PIC12F629 interrupt not working
« Reply #10 on: October 01, 2013, 11:32:12 am »
The issue here is that there are other peripherals on those pins and one of them happens to default to be on.

That particular peripheral will need to be turned off so the pins operate as gpio pins.
================================
https://dannyelectronics.wordpress.com/
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #11 on: October 01, 2013, 12:44:46 pm »
Probably so.  I was trying to get the OP to start again with something simpler and go from  there.   :)
 

Online AndyC_772

  • Super Contributor
  • ***
  • Posts: 3610
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: PIC12F629 interrupt not working
« Reply #12 on: October 01, 2013, 08:19:09 pm »
The issue here is that there are other peripherals on those pins and one of them happens to default to be on.

That particular peripheral will need to be turned off so the pins operate as gpio pins.

Geez, either help the poor guy with his problem or don't... it's obvious he's already read the data sheet and is 90% of the way there. Asking for help on a forum is a perfectly valid way to get help with a problem!

If you mean 'on many PICs, the Analogue-to-Digital converter is enabled by default and must be switched off in order to use pins as digital inputs', then it's OK to just say so!

Offline adam1213

  • Regular Contributor
  • *
  • Posts: 119
  • Country: au
Re: PIC12F629 interrupt not working
« Reply #13 on: October 01, 2013, 10:42:43 pm »
    Probably so.  I was trying to get the OP to start again with something simpler and go from  there.   :)

    This is a good way to approach the problem. Ways to break this problem down to work out what is working and what is not:

    Test I/O:
    • Test reading the INT I/O pin (GPIO2?)
    • Test writing to GPIO1 (the LED)
    • Test setting GPIO1 to GPIO2 using a button and a LED (the button and LED should correlate in terms of being  pressed / on)

    Test the interrupt:
    • Test pressing a button connected to GPIO0 and see if the interrupt handler is ever called
    • Test setting INTF manually and see if the interrupt is ever called
    Tags the forum insists on closing despite being closed:[/list][/list]
    « Last Edit: October 01, 2013, 10:46:12 pm by adam1213 »
     

    Offline dannyf

    • Super Contributor
    • ***
    • Posts: 8229
    • Country: 00
    Re: PIC12F629 interrupt not working
    « Reply #14 on: October 01, 2013, 11:02:51 pm »
    Quote
    This is a good way to approach the problem.

    Much simpler than that: just read the datasheet, :).

    I would comment out the if statement in the while loop and watch the GPIO1 pin when INT goes high: GPIO1 goes high on the first and only the first rising edge of INT.

    That tells you that the isr is working.
    ================================
    https://dannyelectronics.wordpress.com/
     

    Offline FrankBuss

    • Supporter
    • ****
    • Posts: 2319
    • Country: de
      • Frank Buss
    Re: PIC12F629 interrupt not working
    « Reply #15 on: October 01, 2013, 11:23:35 pm »
    If you mean 'on many PICs, the Analogue-to-Digital converter is enabled by default and must be switched off in order to use pins as digital inputs', then it's OK to just say so!
    The PIC12F629 doesn't have an ADC, but right, this is sometimes a problem with other PICs, like the PIC12F675. I guess it could be a problem with the analog comparator, see chapter 6.2 "Comparator Configuration", so adding "CMCON=7" might be the solution.
    So Long, and Thanks for All the Fish
    Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
     

    Offline AlfBaz

    • Super Contributor
    • ***
    • Posts: 2030
    • Country: au
    Re: PIC12F629 interrupt not working
    « Reply #16 on: October 01, 2013, 11:35:37 pm »
    These problems are usually sorted out buy reading the datasheet. At a quick glance for the 12F629
    The comparator module has to be turned off by setting the 3 LSB's in CMCON, so CMCON=7

    You have enabled interrupt on change but there's another register, the IOC (Interrupt-On-Change) which enables/disables which GPIO pin will cause an interrupt. They are all off after a power on reset
     

    Offline David_AVD

    • Super Contributor
    • ***
    • Posts: 2607
    • Country: au
    Re: PIC12F629 interrupt not working
    « Reply #17 on: October 02, 2013, 12:13:08 am »
    You have enabled interrupt on change but there's another register, the IOC (Interrupt-On-Change) which enables/disables which GPIO pin will cause an interrupt. They are all off after a power on reset

    IOC and INTx are two different interrupt sources and serve different purposes.  One does not rely on the other.  I'd also enable the global interrupt after the individual ones.

    The comparator is likely the culprit.  I hadn't mentioned it earlier as I thought there was more learning to be had by simplifying the code first.

    Starting with a flashing LED program is invaluable as it will tell you if the device is running and if it's at the right speed.  I still do this whenever I write code for a new chip type.
     

    Offline AlfBaz

    • Super Contributor
    • ***
    • Posts: 2030
    • Country: au
    Re: PIC12F629 interrupt not working
    « Reply #18 on: October 02, 2013, 01:00:27 am »
    IOC and INTx are two different interrupt sources and serve different purposes.  One does not rely on the other.

    LOL... should follow my own advice regarding reading data sheets
     

    Offline Bibiricat

    • Contributor
    • Posts: 43
    Re: PIC12F629 interrupt not working
    « Reply #19 on: October 02, 2013, 05:07:31 pm »
    I would like to say thank you for those who posted here, as my problem was that CMCON=7;
    I would like to add that i read the datasheet, i did test all the I/O ports with LED ,  the delay functions, and i did have a button press test in main loop (witch was working fine), but somehow that CMCON escaped me.
    Thanks again and hope that my posting my problem here will help others with PIC12F629.
     


    Share me

    Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
    Smf