CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Interrupt-On-Change not working in PIC12F1822

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ccs2015



Joined: 18 Mar 2015
Posts: 2

View user's profile Send private message

Interrupt-On-Change not working in PIC12F1822
PostPosted: Wed Mar 18, 2015 8:24 am     Reply with quote

Hello, if I run this code on a PIC12F1822 it works, the LED toggles when there is a falling-edge in BUTTON pin:

Code:
#include <12F1822.h>
#device adc = 10
#use delay(int=8000000)
#fuses NOWDT,NOPROTECT,NOCPD,NOMCLR,PUT,INTRC_IO,NOBROWNOUT,NODEBUG,NOWRT

#define LED      PIN_A2
#define BUTTON   PIN_A1
int1 flag;

void main()
{
   setup_adc_ports(NO_ANALOGS);

   while(1){
      if(!input(BUTTON))
         output_toggle(LED);
   }
}


But when I want to use interrupt-on-change doing these changes:

Code:

void main()
{
   setup_adc_ports(NO_ANALOGS);
   enable_interrupts(INT_RA1_H2L);
   enable_interrupts(GLOBAL);

   while(1){
      if(flag){      
         output_toggle(LED);
         delay_ms(500);
         flag = 0;
      }
   }
}

#INT_RA
void int_ioc()
{
   if(!input(BUTTON))
      flag = 1;
}


The LED does not blink. I have tried with and without an external pull-up in BUTTON pin, and configuring enable_interrupts(INT_RA1) instead of enable_interrupts(INT_RA1_H2L).

What could be the problem?

I am using MPLAB 8.92, CCS 4.140 and the ICD2 programmer.

Regards.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Wed Mar 18, 2015 8:44 am     Reply with quote

These chips have an extra bit that has to be cleared to exit the interrupt.

With the standard 'interrupt on change', two things have to be done in the interrupt to clear it. Read the port, and clear the interrupt flag (you do the first, and the compiler does the second). However on these chips, you also have to clear the mask bit that says which bit has interrupted. So:
Code:

#byte IOCAF = getenv("SFR:IOCAF")

#INT_RA
void int_ioc()
{
   bit_clear(IOCAF,1); //For RA1
   if(!input(BUTTON))
      flag = 1;
}


Without this bit cleared, the interrupt flag won't clear, so the interrupt will never exit....

You clear the bit corresponding to the interrupt you are using.
Since you are not using any others you could just use:

IOCAF=FALSE;

Which clears them all.
ccs2015



Joined: 18 Mar 2015
Posts: 2

View user's profile Send private message

PostPosted: Wed Mar 18, 2015 8:51 am     Reply with quote

Thanks a lot Ttelmah, it is working now.
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Wed Mar 18, 2015 10:30 am     Reply with quote

It's one of those 'once you know about it, dead easy, but designed to confuse' things. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group