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!

asked Jul 27, 2017 at 8:16
\$\endgroup\$
4
  • \$\begingroup\$ Is this a complete schematic? MCLR seems to be enabled in your config, yet it is floating. \$\endgroup\$ Commented Jul 27, 2017 at 10:34
  • \$\begingroup\$ > where is the mistake? You didn't read the datasheet on how to turn that pin to GPIO. \$\endgroup\$ Commented Jul 27, 2017 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\$ Commented Jul 29, 2017 at 9:13
  • \$\begingroup\$ I've read the datasheet @dannyf, and that was just a simple mistake! \$\endgroup\$ Commented Jul 29, 2017 at 9:16

2 Answers 2

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).

answered Jul 27, 2017 at 15:26
\$\endgroup\$
4
  • \$\begingroup\$ Many thanks for your answer, i changed TRISAbits.TRISA2 to 1, but interrupt is not working, yet \$\endgroup\$ Commented Jul 29, 2017 at 8:36
  • \$\begingroup\$ I edited my code, and correct that mistake \$\endgroup\$ Commented Jul 29, 2017 at 8:43
  • \$\begingroup\$ @paradisalprogrammer To clarify: Do you still have the problem, even after you incorporated these changes? \$\endgroup\$ Commented Jul 29, 2017 at 9:48
  • \$\begingroup\$ Yes! @pipe , when i press push button on PORTA.2, LED on PORTC.2 doesn't blink \$\endgroup\$ Commented Jul 29, 2017 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.
answered Jul 29, 2017 at 14:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.