1
\$\begingroup\$

When pin RB5 changes between high and low it should trigger interrupt. Interrupt happens when IC gets powered and does not occur afterwards when RB5 gets connected or disconnected from vdd. Why so?

I am using PIC16F677-I/P

#include <xc.h>
#include <stdbool.h>

#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

void main(void)
{
    // Select 8 MHz internal oscillator
    IRCF2 = 1;
    IRCF1 = 1;
    IRCF0 = 1;

    // High Frequency Internal Oscillator is stable
    HTS = 1;

    // Use internal oscillator for system clock
    SCS = 1;

    // Global interrupt enable
    GIE = 1;

    // PORTA/B change interrupt enable
    RABIE = 1;

    RABIF = 0;

    // RB5 as digital input
    ANS11 = 0;

    // RB5 as input
    TRISB5 = 1;

    // RB5 interrupt on change
    IOCB5 = 1;

    // RC6 as output
    TRISC6 = 0;

    RC6 = 0;

    while (true)
    {

    }
}

void interrupt myintrout(void)
{
    if (RABIF)
    {
        RC6 = RC6 ? 0 : 1;
        RABIF = 0;
    }
}
\$\endgroup\$
8
  • \$\begingroup\$ Not sure why the interrupt is not immediately re-triggering. You must clear the miss-match condition by either reading or writing PORTB prior to clearing RABIF per page 69 of the PIC16F677 Datasheet \$\endgroup\$
    – Tut
    Nov 4, 2015 at 21:28
  • \$\begingroup\$ @Tut I have tried RB5 = 0; before RABIF = 0; if that is what you mean. \$\endgroup\$ Nov 4, 2015 at 21:32
  • \$\begingroup\$ Read the datasheet and then try reading PORTB. Note RB5 is an input not an output. Leave it in there because you will need it whether that is the only thing wrong or not. \$\endgroup\$
    – Tut
    Nov 4, 2015 at 21:35
  • \$\begingroup\$ Not related to the question, but RC6 = !RC6; is more readable than RC6 = RC6 ? 0 : 1;. I had to think about that for a few seconds before I saw what you were doing. \$\endgroup\$
    – AaronD
    Nov 4, 2015 at 21:35
  • \$\begingroup\$ @AaronD Yea, thanks, I do not know why I used this form when posted question. \$\endgroup\$ Nov 4, 2015 at 21:37

1 Answer 1

1
\$\begingroup\$

When RC6 = !RC6; is used, first high on RB5 makes RC6 high. At this point no changes can be made to RC6 after making RB5 high or low.

When I use RC6 = RB5; I can observe interruptions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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