dsPIC30F4013 start

Status
Not open for further replies.
Wow interrupts are simple here:
Code:
#include "p30F4013.h"
//#include <p30fxxxx.h>

_FOSC(FRC_PLL4);
_FWDT(WDT_OFF)
_FBORPOR(MCLR_EN & PWRT_OFF)  
_FGS(CODE_PROT_OFF)    


void INT_Init(void);
void __attribute__((__interrupt__)) _INT0Interrupt(void);

#define SW1 PORTAbits.RA11
char isSet;

int main(void)
{
    isSet = 0;    
    TRISB = 0;
    LATB = 0;

    INT_Init();

    while(1)
    {
        if(isSet == 1)
            LATBbits.LATB0 = 1;
        else
            LATBbits.LATB0 = 0;

    }
}

void INT_Init(void)
{
        INTCON2 = 0x0001;       //Setup INT0 pin to interupt on falling edge
        IFS0bits.INT0IF = 0;    //Reset INT0 interrupt flag
        IEC0bits.INT0IE = 1;    //Enable INT0 Interrupt Service Routine

        TRISAbits.TRISA11 = 1;
}

void __attribute__((__interrupt__)) _INT0Interrupt(void)
{
        while (SW1 == 1); 

        if(isSet == 0)
            isSet = 1;
        else
            isSet = 0;

        IFS0bits.INT0IF = 0;
}
 
I'm not sure how the while() inside the interrupt is wise ; actually, I'm pretty sure it is not a good idea.. if you want the interrupt to trigger on "button release" then change INTCON2 so that interrupt is triggered on rising edge and not on falling edge and remove the while from the interrupt routine
 
the while() is just a debounce

its not going to debounce much .. set the interrupt to trigger on rising edge (button release) in the start of the interrupt routine disable interrup, and before you leave enable it again .. duration of the interrupt routine will do the "debouncing"

even that is not needed as you clear IFS0bits.INT0IF at the end of the routine .. so any "bounces" will not trigger the interrupt again until it ends...

the wile() will just pick up the "first bounce" ...

when you are not doing this in interrupt then some delay is needed in order for software debounce to work properly

Code:
while(!pressed);
delay(1ms); //wait a bit for input to stabilize
while(pressed);

as for example this do not work:
Code:
count = 0;
while(1){
  while(!pressed1);
  while(pressed1);
  ++count;

  if (pressed2) break;
}
printf("%i", count);

you'd expect this to count how many times "pressed1" button was pressed from start until you press "pressed2" ... what happens is that for each time you press "pressed1" button the count will increment between 1 and 30 times because of the "contact bounce"...

in order to make it work properly - add a small delay between press and release check
Code:
...
while(!pressed);
delay(1ms);
while(pressed);
...

hope that helps, the hw debounce is easy so I prefer it .. but .. you can do this way too ... but no need to do it in interrupt as interrupt itself is debouncing it...
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…