• AVR Freaks

Hot!dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ?

Author
TS9
Super Member
  • Total Posts : 936
  • Reward points : 0
  • Joined: 2010/05/07 10:52:22
  • Status: offline
2021/02/24 09:34:00 (permalink)
0

dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ?

Hi,
 
I am using dsPIC33EP512MU810 . I need two CN Change Notice Feature on two different GPIOs .
 
How to write ISR for Multiple Change Notice(s) of Two RD0 and RA5 with polling technique  ?
But below Method Not Properly Working.

 

////////////////////////////
#define ZCD_INPUT_DIR TRISDbits.TRISD0
#define ZCD_INPUT_PIN PORTDbits.RD0
 
#define ZCD_INPUT_CHANGE_PIN_NOTIFICATION_ENABLE CNENDbits.CNIED0
 
#define ZCD_INPUT_PULL_UP_ENABLE CNPUDbits.CNPUD0
#define ZCD_INPUT_PULL_DOWN_ENABLE CNPDDbits.CNPDD0
 

#define ZCD_INPUT_DIR_2 TRISAbits.TRISA5
#define ZCD_INPUT_PIN_2 PORTAbits.RA5
 
#define ZCD_INPUT_CHANGE_PIN_NOTIFICATION_ENABLE_2 CNENAbits.CNIEA5
 
#define ZCD_INPUT_PULL_UP_ENABLE_2 CNPUAbits.CNPUA5
#define ZCD_INPUT_PULL_DOWN_ENABLE_2 CNPDAbits.CNPDA5
 
////////////////////////////
 

void ZCD_Configuration() ;
 
void ZCD_Configuration()
{

ZCD_INPUT_DIR = 1 ; // Configure Input
ZCD_INPUT_CHANGE_PIN_NOTIFICATION_ENABLE = 1 ;
ZCD_INPUT_PULL_UP_ENABLE = 1 ;
ZCD_INPUT_PULL_DOWN_ENABLE = 0 ;


ZCD_INPUT_DIR_2 = 1 ;
ZCD_INPUT_CHANGE_PIN_NOTIFICATION_ENABLE_2 = 1 ;
ZCD_INPUT_PULL_UP_ENABLE_2 = 1 ;
ZCD_INPUT_PULL_DOWN_ENABLE_2 = 0 ;
 
 
 
// Priority: 1
 
_CNIE = 1 ; // Enable CN interrupts
_CNIF = 0 ; // Clear interrupt flag
}
void __attribute__((__interrupt__, no_auto_psv)) _CNInterrupt(void)
{
if( _CNIF )
{

if(ZCD_INPUT_PIN)
{
LED1 = 0 ;

}
else
{
LED1 = 1 ;

}
 
if(ZCD_INPUT_PIN_2)
{
LED2 = 0 ;
}
else
{
LED2 = 1 ;
}
_CNIF = 0; // Reset for Next Round
}
}
 
 
 

 
Thanks..
 
--
TS9
post edited by TS9 - 2021/02/24 09:35:44
#1

9 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 12:35:40 (permalink)
    0
    How are you testing, and what happens when you do test?
    "But below Method Not Properly Working" is NOT a useful response.
     
    What are you definitions for "LED1" and "LED2" ?
     

    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!
    #2
    TS9
    Super Member
    • Total Posts : 936
    • Reward points : 0
    • Joined: 2010/05/07 10:52:22
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 17:36:20 (permalink)
    0
    ric
    How are you testing, and what happens when you do test?"But below Method Not Properly Working" is NOT a useful response. What are you definitions for "LED1" and "LED2" ? 

    Thanks ric.

    1) I am testing with feeding Square wave with different frequencies on CN pins.

    2) In ISR , the code is always going to both "else" on every interrupt which is not required.

    3) LED1 and LED2 are working as tested by blink in another program.

    4) In my application, I want output of square wave LED1 and LED2 as given on respective CN Pins
    which I am not getting.

    --
    TS9
    #3
    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 18:44:28 (permalink)
    0
    TS9
    ...
    3) LED1 and LED2 are working as tested by blink in another program.

    That does not answer the question that I asked.
    I did not ask "do the LEDs work".
    I asked "What are you definitions for "LED1" and "LED2" "
    I would expect your answer to show the code defining those two expressions.
     

    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
    TS9
    Super Member
    • Total Posts : 936
    • Reward points : 0
    • Joined: 2010/05/07 10:52:22
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 19:01:25 (permalink)
    0
    #define LED1 LATCbits.LATC0
    #define LED2 LATCbits.LATC1
    #5
    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 19:10:28 (permalink)
    4 (1)
    1) There is no need to check for the CNIF bit in the ISR - the ISR will only be called if that bit is set
    2) How do you know you are executing the 'else' clause every time?
     
    Also how fast are the square waves you are testing with? In other words are they slow enough (below about 5-10Hz) that you can actually see the LEDs going on and off?
     
    Susan
    #6
    TS9
    Super Member
    • Total Posts : 936
    • Reward points : 0
    • Joined: 2010/05/07 10:52:22
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/24 19:55:23 (permalink)
    0
    Hi @Susan,

    1) Square wave Frequencies in between 50 Hz to 60 Hz.

    2) I mean to say that as there is single ISR for multiple CN.

    Both should be independent to each other,
    How to write with other method as i have written in Msg #1.


    --
    TS9
    post edited by TS9 - 2021/02/24 21:28:44
    #7
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/25 07:37:14 (permalink)
    5 (1)
    Hi,
     
    Why is there Weak Pull-Up enabled for pins RA5 and RD0 ?
    If there are proper active square wave signals as inputs, then pull-up is not needed.
    If there is open collector, or other tricky signal network,
    then circuit diagram is needed to understand what you are doing.
     
    If you are testing in debugger without external signals, then pull-up  will cause test like:
            if (ZCD_INPUT_PIN_2)  

    to always come out with the same result, even if there is no change.
     
    To detect if there is a change since the previous event,
    you will need to keep a copy of the previous pin register value.
    Something like:
    void __attribute__((__interrupt__, no_auto_psv)) _CNInterrupt(void)
    {
               unsigned int CurrentA5,  CurrentD0;
        static unsigned int PreviousA5, PreviousD0;

        if ( _CNIF )
        {   CurrentD0 = ZCD_INPUT_PIN;
            if (CurrentD0 != PreviousD0)    /* Test if actual change */
            {   if (ZCD_INPUT_PIN)          /* .RD0 */
                {
                    LED1 = 0 ;
                }
                else
                {
                    LED1 = 1 ;
                }
                PreviousD0 = CurrentD0;
            }
            CurrentA5 = ZCD_INPUT_PIN_2;
            if (CurrentA5 != PreviousA5)    /* Test if actual change */
            {
                if (ZCD_INPUT_PIN_2)        /* .RA5 */
                {
                    LED2 = 0 ;
                }
                else
                {
                    LED2 = 1 ;
                }
                PreviousA5 = CurrentA5;
            }
            _CNIF = 0; // Reset for Next Round
        }
    }  

     
        Mysil
     
    #8
    NorthGuy
    Super Member
    • Total Posts : 6587
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/25 11:18:21 (permalink)
    5 (1)
    TS9
    2) In ISR , the code is always going to both "else" on every interrupt which is not required.



    How do you know that?
     
    LEDs will look like they're on if you feed a square wave to them.
     
    A good test would be to light up a LED at startup then remove the code which lights them up in the ISR. This way, if LED switches off, you know than "then" has executed.
     
    If you use a debugger, the result depends on where you set your breakpoint. If you set it in the beginning of the ISR, it'll be too much time between the interrupt and the debugging halt to judge what would've happen if you didn't halt. Instead, set a breakpoint in "then" and run. If the debugger stops, then the "then" code has executed.
     
    #9
    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: dsPIC33EP : How to Write ISR For Multiple Change Notice (CN) Functionality on GPIOs ? 2021/02/25 18:42:38 (permalink)
    5 (2)
    TS9
    1) Square wave Frequencies in between 50 Hz to 60 Hz.

    Then you will not 'see' the LEDs flashing - they will look like a steady but slightly dimmer glow.
    You either need to slow them down to something like 10Hz or less so your eyes can actually see the LED going on or off, or use a scope/logic analyser on the output pins.
    Susan
    #10
    Jump to:
    © 2021 APG vNext Commercial Version 4.5