0
\$\begingroup\$

I am using pic16f1823 and MPLab as IDE. I want to have simple interrupt on RA2 and turn on LED, when it come. I've wrote This code, but it doesn't work!

#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

And for main app, we have :

#include <stdio.h>
#include <stdlib.h>
#include "main_10.h"



void interrupt tc_int(void)
{
    //if (INTCONbits.INTF == 1)
    if (INTCONbits.T0IF && INTCONbits.T0IE)    
    {
        LATCbits.LATC1 = 1;
        __delay_ms(1000);
        LATCbits.LATC1 = 0;
        __delay_ms(1000);
    }
    INTCONbits.INTF = 0;
}


/*
 * 
 */
void main (void)
{
    OSCCON = 0x76;//Very Important,this will command micro to use internal oscillator

    //prepare Interrupt on A2
    TRISAbits.TRISA2 = 1; //make interrupt port as input.
    OPTION_REGbits.INTEDG = 0; //make sense to falling edge.
    INTCONbits.INTE = 1;    //interrupt enable.
    INTCONbits.GIE = 1; //Globally enable interrupts.
    INTCONbits.INTF = 0;    //make interrupt flag clear.




    //prepare output on PORTC.bit1
    LATCbits.LATC1 = 0; //initialization of portc.0
    TRISCbits.TRISC1 = 0; //make portc.0 as output.

    while(1)
    {

    }

}

And we have Proteus simulation like this: enter image description here

where is the mistake? Any help would be appreciated!

\$\endgroup\$
  • \$\begingroup\$ Is this a complete schematic? MCLR seems to be enabled in your config, yet it is floating. \$\endgroup\$ – glen_geek Jul 27 '17 at 10:34
  • \$\begingroup\$ > where is the mistake? You didn't read the datasheet on how to turn that pin to GPIO. \$\endgroup\$ – dannyf Jul 27 '17 at 10:46
  • \$\begingroup\$ Many thanks @glen_geek, for your comment! Yes! you are right! I changed the schematic; but i think this is not the main problem of lack of interrupt \$\endgroup\$ – paradisal programmer Jul 29 '17 at 9:13
  • \$\begingroup\$ I've read the datasheet @dannyf, and that was just a simple mistake! \$\endgroup\$ – paradisal programmer Jul 29 '17 at 9:16
0
\$\begingroup\$

You are setting A2 as an output using TRISAbits.TRISA2 = 0; should be 1. Easy to remember: a zero looks like an "o" (output) and a 1 looks like an "I" (input).

\$\endgroup\$
  • \$\begingroup\$ Many thanks for your answer, i changed TRISAbits.TRISA2 to 1, but interrupt is not working, yet \$\endgroup\$ – paradisal programmer Jul 29 '17 at 8:36
  • \$\begingroup\$ I edited my code, and correct that mistake \$\endgroup\$ – paradisal programmer Jul 29 '17 at 8:43
  • \$\begingroup\$ @paradisalprogrammer To clarify: Do you still have the problem, even after you incorporated these changes? \$\endgroup\$ – pipe Jul 29 '17 at 9:48
  • \$\begingroup\$ Yes! @pipe , when i press push button on PORTA.2, LED on PORTC.2 doesn't blink \$\endgroup\$ – paradisal programmer Jul 29 '17 at 9:59
0
\$\begingroup\$

After alot of effort, I solved my problem. thanks to @JohnBirckhead. we encountered with two wrong bit set.

    1. TRISAbits.TRISA2 = 0

       //should be converted to:

       TRISAbits.TRISA2 = 1

    2. INTCONbits.T0IF && INTCONbits.T0IE

       //should be converted to (in if statement in isr function):

        INTCONbits.INTF == 1 && INTCONbits.INTE == 1
       // because we are working with external interrupt.
\$\endgroup\$

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.