• AVR Freaks

AnsweredHot!PIC24FJ128GA204 Interruption with a button INT1Interrupt

Author
NCosta
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2021/01/12 10:31:14
  • Location: 0
  • Status: offline
2021/01/14 05:09:44 (permalink)
0

PIC24FJ128GA204 Interruption with a button INT1Interrupt

I'm new at microcontrollers in general and I'm still learning. 
I'm trying to create a code that causes the RC7 led to flash every 1 seconds and when I press the RC9 button the RC7 led will have an inverval of +1second. That is, if I press once it will flash every 2 seconds, if I press it twice it will flash every 3 seconds. And I'm trying to ignore the buttom noise with a interruption function but it's not working. 
when I run the code is triggered to int1 regardless of the RC9 button.

(Note: Fcy=2MHz)
 
#include "xc.h"

void SetTimer(void);
void SetButtonInterrupt(void);

int main(void) {
SetTimer();
SetButtonInterrupt();
TRISC|=(1<<9);
TRISC&=~(1<<7);
LATC&=~(1<<7);

while(1){
if(!(PORTC&(1<<9))){
PR1= PR1+0x1E84;
}

}
}

 

void SetTimer(void)
{
T1CON = (3<<4);
TMR1 = 0x00; //Clear contents of the timer register
PR1 = 0x1E84; //Load the Period register with the value 0x1E84
IPC0bits.T1IP = 0x01; //Setup Timer1 interrupt for desired priority level
// (This example assigns level 1 priority)
IFS0bits.T1IF = 0; //Clear the Timer1 interrupt status flag
IEC0bits.T1IE = 1; //Enable Timer1 interrupts
T1CON|= (1<<15); //Start Timer1 with prescaler settings at 1:256 and
//clock source set to the internal instruction cycle
}

void SetButtonInterrupt(void) //interrupt to avoid buttom noise
{
T2CON = (3<<4); // prescale 1:264
TMR2 = 0x00; //Clear contents of the timer register
PR2 = 0x2000; //Load the Period register with the value 0x2000
IPC5bits.INT1IP = 0x02;

INTCON2= (1<<1); // Interrupt on negative edge
RPINR0=(5<<8); //if press pin5 (RC9) active int0interrupt

IFS1bits.INT1IF = 0;
IEC1bits.INT1IE = 1;
T2CON|= (1<<15);
}


void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
LATC^=(1<<7);
IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR
}

void __attribute__((__interrupt__, __shadow__)) _INT1Interrupt(void)
{

IFS1bits.INT1IF = 0;
}

#1
Aussie Susan
Super Member
  • Total Posts : 3827
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/15 19:48:58 (permalink)
5 (2)
At the coding level, your ISR for the external interrupt does nothing other than clear the IF bit.
As a matter of coding style, I must admit that I find the use of setting and clearing bits using '&=' and '!=' with bit shifts on the right-hand side to be very hard to read. For example 'LATC ^= (1<<7);' is not that 'intuitive' (at least to me) as opposed to 'LATCbits.LATC7 ^= 1;' and a lot less error prone. 
In my experience I have only ever once needed to alter the default interrupt priority levels (and that was for a very complex multi-tasking system that also used freeRTOS). I know the capability is there but, especially in your situation where the ISRs are short, there is no advantage in having one ISR being able to interrupt another.
I'm not sure how you are using the ISR to debounce the button. I see that you start Timer 2 in the 'SetButtonInterrupt' function and set it running continuously but you do reference it else where and there is no ISR associated with it.
Further, an external interrupt is a very poor way to debounce a button.
A far better way to debounce a button (in fact any number of external buttons/switches) is to set up a (say) 1mSec timer and in the ISR test the state of the port(s). If it is NOT the same as the last time you sampled it, then set a register to (say) 10; if it IS the same then decrement the register if it is greater than 0 and if it turns 0 then set an internal flag that you can test elsewhere.
In your main loop, you test for RC9 being 0 and, if it is, then you increment the Timer 1 PR register. However that main loop is very small and will execute many, MANY times each microsecond (depending on your oscillator setup which you have not shown). You really need to use one of the more standard approaches to detecting external button presses that you can find on the Internet and in these forums.
If you are just starting out then I suggest you go back a step and get a LED to flash at the required speed. This way you will make sure that you have set up everything else you need to (oscillators, analog pins, timers etc.). Then do something as simple as connecting a button and a LED so that each push of the button toggles the LED - this will show you how to debounce a button as well as introduce you to the proper uses of interrupts.
My general advice is to keep away from interrupts unless you need to use them - at least while you are learning to use these devices.
Susan
 
Edit: Correct LATC bit reference
post edited by Aussie Susan - 2021/01/17 18:43:15
#2
MBedder
Circuit breaker
  • Total Posts : 6969
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/16 00:48:39 (permalink)
0
LATCbits.RC7LATC7
post edited by MBedder - 2021/01/16 03:37:24
#3
ric
Super Member
  • Total Posts : 29867
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/16 01:46:37 (permalink)
0
MBedder
LATCbits.RC7LATB7

LATC7 !
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
MBedder
Circuit breaker
  • Total Posts : 6969
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/16 03:04:44 (permalink)
0
Thanks, corrected.
#5
Aussie Susan
Super Member
  • Total Posts : 3827
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/17 18:42:09 (permalink)
0
Thanks all for the corrections to my original response - typing without engaging the brain!
Susan
#6
NCosta
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2021/01/12 10:31:14
  • Location: 0
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/18 04:05:14 (permalink)
0
I probably wasn't explicit. My goal was to use an external interrupt to remove the noise from the button by increasing timer1. As you said i have things that i should do in another way but how am i starting i dont know many ways x)

Anyways, thanks for the help.
And if you know of a book that you suggest me to evolve with pic24, I'm very grateful.
#7
NCosta
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2021/01/12 10:31:14
  • Location: 0
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/19 08:35:36 (permalink)
0
In a small version, just trying to use an external interrupt and understanding the mechanics, I coded it to turn on a led with a switch button but the program never enters the interruption. I tried to change the input of the switch button to an external impulse but nothing works.
 

#include "xc.h"
int main(void) {
SetInterrupt();
while(1){

}
}
void SetInterrupt(void){ 
TRISC|=(1<<9); //input switch botton
TRISC&=~(1<<8); //output led
LATC&=~(1<<8); // logic value 0 to led
RPINR0bits.INT1R = 5; //set the switch botton as the trigger of interruption
// RC9 corresponds to pin 5 
INTCON2bits.INT1EP = 1; // Interrupt on negative Edge
IFS1bits.INT1IF = 0; //clear flag
IEC1bits.INT1IE = 1; //ennable interrupt
}
void __attribute__((__interrupt__, __shadow__)) _INT1Interrupt(void)
{
LATC^=(1<<8); // change de logic value of led
IFS1bits.INT2IF = 0;
}

#8
Howard Long
Super Member
  • Total Posts : 887
  • Reward points : 0
  • Joined: 2005/04/04 08:50:32
  • Status: offline
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/19 12:17:54 (permalink) ☼ Best Answerby NCosta 2021/01/20 04:36:54
0

RPINR0bits.INT1R = 5; //set the switch botton as the trigger of interruption
// RC9 corresponds to pin 5 
[/code]
 
No. RC9 correspond to RP25. PPS on this device maps to RP## not to the physical pin number.
 

RPINR0bits.INT1R = 25; //set the switch botton as the trigger of interruption
// RC9 corresponds to RP25 
[/code]
 
See: https://www.microchip.com/forums/m1163703.aspx
#9
ric
Super Member
  • Total Posts : 29867
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC24FJ128GA204 Interruption with a button INT1Interrupt 2021/01/19 12:24:42 (permalink)
4 (1)
RPINR0bits.INT1R = 5; //set the switch botton as the trigger of interruption
// RC9 corresponds to pin 5

The argument for the RPIN registers is not the physical pin number, it must be the
"RP#" number, which for RC9 is RP25, so you should use:
RPINR0bits.INT1R = 25; //interrupt source from RC9/RP25

 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
Jump to:
© 2021 APG vNext Commercial Version 4.5