Jump to content
Paolino

Interrupt-on-change On A Pic12f675

Recommended Posts

Hello guys.

Here is my code for a PIC12F675:

 

void main()
{

// comparator off
cmcon = 00000111b; //0x07;

//  ADC module off
ansel = 00000000b; //0x00;

// I/O TRISTATE config
trisio = 00111011b; //0x3B;


// TMR0 enabling
option_reg = 01000111b; //0x87; 

// TMR1 enabling
t1con = 00110100b; //0x34;

// waek pull-up on GPIO4 and GPIO5
wpu = 0x30;

// Interrupt-on-change: GPIO4 and GIPO5
ioc = 00110000b; //0x30;

// interrupt config
set_bit(intcon,GPIE);
set_bit(pie1,TMR1IE);
//  interrupt enabling
set_bit(intcon,GIE);
set_bit(intcon,PEIE);


tmr1l=0;
tmr1h=0;

while(1);
}

 

and this is the ISR routine:

 

void interrupt (void)
{
// INTERRUPT-ON-CHANGE 	
if (test_bit(intcon,GPIF))
{
	clear_bit(intcon,GPIF);
			// do something else...
	}

if (pir1.TMR1IF)// TMR1 OVERFLOW 	
{
	pir1.TMR1IF=0;				
	clear_bit(t1con,TMR1ON);	
	}
}

 

The strange thing, simulated in MPLAB 8.10 with MPSIM, is the following:

void interrupt (void)

{

if (test_bit(intcon,GPIF))
0032  1283	  BCF STATUS, RP0
0033  1C0B	  BTFSS gbl_intcon,0
0034  2850	  GOTO	label6

{
	clear_bit(intcon,GPIF);
0035  100B	  BCF gbl_intcon,0
...
...
...
0050		label6

	{

004B  1105	  BCF gbl_gpio,2

 

The compiled code does not clear immediately the GPIF flag; it take two consecutive runs in the ISR routine! THe INTCON register value is 0x4D when the interrupt occurs and after, only after, the second run (without exit!!!!) it becomes 0x4C (GPIF is cleared).

 

I am going crazy since I can not figure out why it happens. Could it be a bug? BoostC's or mine?

Thank you.

 

Paolo.

Share this post


Link to post
Share on other sites

Have you tried reading the GPIO ports before clearing the interrupt flag GPIF.

 

The port has to be read to end the change mismatch condition otherwise GPIF will just set itself again (page 21 of data sheet).

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Have you tried reading the GPIO ports before clearing the interrupt flag GPIF.

 

The port has to be read to end the change mismatch condition otherwise GPIF will just set itself again (page 21 of data sheet).

 

Cheers

 

Reynard

 

Hello,

 

I had same problem but on a other device. I read to port as you said and that solve my problems but there is a other problem when I power up the device and all inputs on interrupt-on-change is low everything is ok, but when I power up with on of the inputs high the program does not start. Any suggestion what is wrong?

 

Best regards /Jörgen

Share this post


Link to post
Share on other sites

As part of your startup and configuration of the device, you should read the GPIO and reset any interrupt flags before you start your main process loop.

 

The change register has to be set to match the initial input port values so you have to read the port to make the current value the old latched value.

 

I hope that makes sense!

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
As part of your startup and configuration of the device, you should read the GPIO and reset any interrupt flags before you start your main process loop.

 

The change register has to be set to match the initial input port values so you have to read the port to make the current value the old latched value.

 

I hope that makes sense!

 

Cheers

 

Reynard

 

Thanks I got it working now I had to play around with startup/init rutines

 

Best regards

 

/Jörgen

Share this post


Link to post
Share on other sites
Have you tried reading the GPIO ports before clearing the interrupt flag GPIF.

 

The port has to be read to end the change mismatch condition otherwise GPIF will just set itself again (page 21 of data sheet).

 

Cheers

 

Reynard

 

Thank you Reynard, once again! Problem solved reading the GPIO ports before clearing GPIF flag. Great.

 

Paolo.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...