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!
-
\$\begingroup\$ Is this a complete schematic? MCLR seems to be enabled in your config, yet it is floating. \$\endgroup\$glen_geek– glen_geek2017年07月27日 10:34:28 +00:00Commented 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\$dannyf– dannyf2017年07月27日 10:46:29 +00:00Commented 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\$paradisal programmer– paradisal programmer2017年07月29日 09:13:35 +00:00Commented Jul 29, 2017 at 9:13
-
\$\begingroup\$ I've read the datasheet @dannyf, and that was just a simple mistake! \$\endgroup\$paradisal programmer– paradisal programmer2017年07月29日 09:16:30 +00:00Commented Jul 29, 2017 at 9:16
2 Answers 2
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).
-
\$\begingroup\$ Many thanks for your answer, i changed TRISAbits.TRISA2 to 1, but interrupt is not working, yet \$\endgroup\$paradisal programmer– paradisal programmer2017年07月29日 08:36:21 +00:00Commented Jul 29, 2017 at 8:36
-
\$\begingroup\$ I edited my code, and correct that mistake \$\endgroup\$paradisal programmer– paradisal programmer2017年07月29日 08:43:58 +00:00Commented Jul 29, 2017 at 8:43
-
\$\begingroup\$ @paradisalprogrammer To clarify: Do you still have the problem, even after you incorporated these changes? \$\endgroup\$pipe– pipe2017年07月29日 09:48:10 +00:00Commented 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\$paradisal programmer– paradisal programmer2017年07月29日 09:59:00 +00:00Commented Jul 29, 2017 at 9:59
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.