1
\$\begingroup\$

Hello I am using PIC18F26K83 and I have a button on RC7 pin. That is why I cannot use a normal interrupt. But according to datasheet interrupt on change can be used in this pin. I could not make it work so I tried to make a code with interrupt on change which was supposed to make the LED light. But the code never enters the interrupt routine. Here is the code:

   #define switch PORTC.B7
#define led1 LATB.B0
#define led2 LATB.B1
#define led3 LATB.RB5
int counter;

 void Clk_8Mhz(){
   //8 MHz clock
          //  OSCCON1 REGISTER
   NOSC2_BIT=1;
  NOSC1_BIT=1;
   NOSC0_BIT=0;//HF INTERNAL OSC
   //OSCFRQ REGISTER HFINTOSC FREQ. SELECTION


OSCCON1 =0b01100000; //HFINTOSC, Divider =1;
    FRQ3_BIT=0;
   FRQ2_BIT=0;
   FRQ1_BIT=1;
   FRQ0_BIT=1;    // 0011 :8MHz     //0100 = 16 Mhz
  }
void main (){
TRISC.RC7=1; //Button pin is output
ANSELC.RC7=0; //Button pin is digital

TRISB=0x00; //B port is output.
LATB.B=0x00; //All B port is initially zero.
//RC7 IOC

//Interrupt initialisation
INTCON0.B5=0; //Interrupt priority disabled
IOCCN7_bit=1; //RC7 pin is negative edge
IOCCF=0x00; //Clear the Interrupt on Change Flag
PIE0.B7=1; //Interrupt on change enabled
INTCON0.B7=1; //Global high priority interrupt enable


   Clk_8Mhz();


while(1){
delay_ms(10);//Wait for interrupt

}

}
void interrupt(void)  {

led1=1;
led2=1;
led3=1;
}

Probably I am missing something very obvious. Edit: I use Micro C Edit2: I changed to code like above. Still interrupt does not occur unfortunately.

These are 2 images of the schematic, I could not find the whole part but schematic of PIC and switch is in here:

Schematic of the button Schematic of the PIC

\$\endgroup\$
  • \$\begingroup\$ You know switch is a reserved word? (Used in #define switch PORTC.B7) \$\endgroup\$ – Huisman May 6 '19 at 9:19
  • \$\begingroup\$ I did not know that, but again I did not use it in anywhere. I just defined it. \$\endgroup\$ – Günkut Ağabeyoğlu May 6 '19 at 10:08
  • 1
    \$\begingroup\$ which compiler? In xc8 the interrupt specifier uses two underscores.Have a look at the manual. And btw. never use a delay function in an ISR. \$\endgroup\$ – Mike May 6 '19 at 10:57
  • 1
    \$\begingroup\$ Possible issue: Ensure switch is properly pulled up or down - either by an internal pullup or externally. (No, I didn't check the code, you'll know if you have considered this or not). \$\endgroup\$ – Russell McMahon May 6 '19 at 12:23
  • \$\begingroup\$ You need to read PORTB before clearing the interrupt flag but since the interrupt is never executed this is not the main reason for the fault \$\endgroup\$ – Dorian May 9 '19 at 8:50
2
\$\begingroup\$

You have to set your PPS Registers (page 265/266) also see 9.9 Page 122. This could possibly be the reason for your problem.

\$\endgroup\$
  • \$\begingroup\$ Thanks, I just set RC7 as RC7PPS= 0b00010111, but problem still continues. \$\endgroup\$ – Günkut Ağabeyoğlu May 7 '19 at 14:21
1
\$\begingroup\$

PPS should not matter for Interrupt on change, especially if the peripheral is disabled.

You shouldn't need a pull up as that seems to be done in hardware. You can disable the weak pull up (below)

WPUC.WPUC7 = 0

Another thing you should do is read up on MikroC using their manual page 121

Interrupts can be easily handled by means of reserved word `interrupt`. mikroC
PRO for PIC implictly declares function interrupt which cannot be redeclared. Its
prototype is:

void interrupt(void)

not

void _interrupt(void)

Set config bit MVECEN to OFF to make sure Mikro C is not looking for a vectored interrupt.

Also INTCON0.IPEN = 0; // Disable Interrupt Priority Vectors (16CXXX Compatibility Mode)

Update your compiler. See this forum post for issues regarding MikroC and K42/K83 pic series.

You also forgot to set ANSEL for the LED but I don't think this matters for output

ANSELB.B0=0;
ANSELB.B1=0;
ANSELB.B5=0;
\$\endgroup\$
  • \$\begingroup\$ Changed both but still no luck \$\endgroup\$ – Günkut Ağabeyoğlu May 9 '19 at 8:44
  • 1
    \$\begingroup\$ Hi, If you can also post a schematic on your question it might help in case there might be electrical issues. thanks. \$\endgroup\$ – pm101 May 9 '19 at 9:06
  • 1
    \$\begingroup\$ You can disable the weak pull up as that is already in hardware. Maybe you should try to organise your code a bit better so it is more easier to read (in case it is a software issue it might be easier to find) \$\endgroup\$ – pm101 May 9 '19 at 9:40
  • 1
    \$\begingroup\$ ps. I noticed you used WPUC.B7 = 1; instead of WPUC.C7 = 1; in your updated code, I guess that's an error in your stackoverflow code (Again please try to format your code) \$\endgroup\$ – pm101 May 9 '19 at 10:43
  • 1
    \$\begingroup\$ That doesn't seem to make sense if it is PORTC. It should be WPUC.WPUC7 (I don't know what it is in MikroC). Try setting config bit MVECEN to OFF to make sure Mikro C is not looking for a vectored interrupt. and also Disable Interrupt Priority Vectors (16CXXX Compatibility Mode) INTCON0.IPEN = 0; \$\endgroup\$ – pm101 May 9 '19 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.