PIC12F629 interrupt not working - Page 1
Forum section: electronics / general

author: Bibiricat
author_rank: Contributor
timestamp: September 30, 2013, 10:45:22 pm
content: 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:/*****************************************************//* 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_regINTEDG = 1; //1 = Interrupt on rising edge of GP2/INT pin//INTCONGIE = 1; // GIE: Global Interrupt Enable bit// 1 = Enables all unmasked interruptsPEIE = 1;// PEIE: Peripheral Interrupt Enable bit// 1 = Enables all unmasked peripheral interruptsINTE = 1;// INTE: GP2/INT External Interrupt Enable bit// 1 = Enables the GP2/INT external interruptINTF = 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);}}

author: dannyf
author_rank: Super Contributor
timestamp: September 30, 2013, 10:54:56 pm
content: QuoteObviously, the "problems" are top secret.

author: David_AVD
author_rank: Super Contributor
timestamp: September 30, 2013, 11:36:58 pm
content: Can you make the LED blink by itself so you know the chip is running?

author: adam1213
author_rank: Regular Contributor
timestamp: September 30, 2013, 11:37:25 pm
content: 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.

author: Bibiricat
author_rank: Contributor
timestamp: October 01, 2013, 10:26:45 am
content: 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.

author: David_AVD
author_rank: Super Contributor
timestamp: October 01, 2013, 10:38:25 am
content: 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.

author: dannyf
author_rank: Super Contributor
timestamp: October 01, 2013, 10:38:38 am
content: QuoteIt does: put a scope on GPIO1 and you will see.It is the rest of your code that's faulty.

author: dannyf
author_rank: Super Contributor
timestamp: October 01, 2013, 10:43:13 am
content: QuoteMost of the times, it is because of people's unwillingness to read the datasheet, their inability to understand the datasheet or bad programming habit.

author: lapm
author_rank: Frequent Contributor
timestamp: October 01, 2013, 10:55:07 am
content: 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...

author: adam1213
author_rank: Regular Contributor
timestamp: October 01, 2013, 11:01:31 am
content: 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.

author: dannyf
author_rank: Super Contributor
timestamp: October 01, 2013, 11:32:12 am
content: 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.

author: David_AVD
author_rank: Super Contributor
timestamp: October 01, 2013, 12:44:46 pm
content: Probably so. I was trying to get the OP to start again with something simpler and go from there.

author: AndyC_772
author_rank: Super Contributor
timestamp: October 01, 2013, 08:19:09 pm
content: Quote from: dannyf on October 01, 2013, 11:32:12 amGeez, 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!

author: adam1213
author_rank: Regular Contributor
timestamp: October 01, 2013, 10:42:43 pm
content: Quote from: David_AVD on October 01, 2013, 12:44:46 pmThis 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 calledTest setting INTF manually and see if the interrupt is ever calledTags the forum insists on closing despite being closed:

author: dannyf
author_rank: Super Contributor
timestamp: October 01, 2013, 11:02:51 pm
content: QuoteMuch 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.

author: FrankBuss
author_rank: Unknown
timestamp: October 01, 2013, 11:23:35 pm
content: Quote from: AndyC_772 on October 01, 2013, 08:19:09 pmThe 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: October 01, 2013, 11:35:37 pm
content: These problems are usually sorted out buy reading the datasheet. At a quick glance for the 12F629The comparator module has to be turned off by setting the 3 LSB's in CMCON, so CMCON=7You 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

author: David_AVD
author_rank: Super Contributor
timestamp: October 02, 2013, 12:13:08 am
content: Quote from: AlfBaz on October 01, 2013, 11:35:37 pmIOC and INTx are two different interrupt sources and serve different purposes. One does not rely on the other. I'd also enable the global interruptafterthe 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: October 02, 2013, 01:00:27 am
content: Quote from: David_AVD on October 02, 2013, 12:13:08 amLOL... should follow my own advice regarding reading data sheets

author: Bibiricat
author_rank: Contributor
timestamp: October 02, 2013, 05:07:31 pm
content: I would like to say thank you for those who posted here, as my problem was thatCMCON=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.