Hot!(EDIT) Writing To The Interrupt Vector Table in Assembly

Page: 12 > Showing page 1 of 2
Author
asmeng
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2017/08/18 07:50:35
  • Location: 0
  • Status: offline
2017/08/29 20:05:04 (permalink)
0

(EDIT) Writing To The Interrupt Vector Table in Assembly

Hi everyone,
 
I'm trying to use an interrupt to turn on an LED when a switch is pressed using assembly but I can't seem to get the interrupt to work. When debugging, I found that the change interrupt flag is being set when the switch is pressed but the processor won't enter the ISR. Can anyone please help?
 
EDIT: I've figured out that I need to set up the IVT. My question now is; how do I write a GOTO instruction directly to the input change interrupt vector using assembly?
 
;*******************************************************************************
    
    .include "p24fj64ga002.inc"

    .global __reset
    
    .text
    
    __reset:
    
;*******************************************************************************
    
    ;Clear WREG
    CLR WREG
  
    ;Configure input and output pins
    BSET TRISB,#5 ;Set RB5 as input
    BCLR TRISB,#15 ;Clear RB15 as output
    BSET PORTB,#15 ;Set LED4 LOW
    
    ;Enable input change notification for CN27
    BSET CNEN2,#11
    
    ;Configure interrupt control registers
    BSET INTCON1,#15 ;Set NSTDIS control bit to disable nested interrupts
    MOV #0x7000,W0
    IOR IPC4 ;Set input change notification priority to highest
    BCLR IFS1,#3 ;Clear input change notification flag
    BSET IEC1,#3 ;Set input change notification interrupt enable
 
;*******************************************************************************
    
    ;Clear WREG. WREG is used to compare PORTB values to see if the input
    ;has actually changed.
    CLR WREG
    
    ;Move PORTB value into W1 register since PORTB,#5 will initially be high
    MOV PORTB,WREG
    
LOOP:
    GOTO LOOP
    
    .global __CNIInterrupt
__CNIInterrupt:
    BCLR IFS1,#3
    CPB PORTB
    BTSC SR,#1
    BTG PORTB,#15
    RETFIE
    
    .end

post edited by asmeng - 2017/08/31 11:21:57
#1

21 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 6235
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/08/29 20:22:18 (permalink)
    +1 (1)
    Don't you have to set up an IVT (Interrupt Vector Table) to use interrupts on a PIC24?
     
    For reading pushbuttons, it's actually much nicer to poll the button with a timer interrupt, which lets you easily implement software debouncing.
    Using change interrupts for reading pushbuttons can cause havoc when you get rapid switch bounce.
     
     
    #2
    asmeng
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2017/08/18 07:50:35
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/08/30 09:12:43 (permalink)
    0
    qhb
    Don't you have to set up an IVT (Interrupt Vector Table) to use interrupts on a PIC24?
     
    For reading pushbuttons, it's actually much nicer to poll the button with a timer interrupt, which lets you easily implement software debouncing.
    Using change interrupts for reading pushbuttons can cause havoc when you get rapid switch bounce.
     
     


    I remember reading that you have to program a 'GOTO ISR' command at the vector address but how do you do this? The reason I'm using an interrupt for this is because I'm just trying to learn how to use and program interrupts.
    #3
    MBedder
    Circuit breaker
    • Total Posts : 6279
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 01:13:48 (permalink)
    +2 (4)
    asmeng:
    You do not need to create a headache with absolute address jump, although it's done easily since the CN interrupt vector (#19) is located at 00003Ah (IVT) and 00013Ah (AIVT). You just need to remove an extra "I" from the vector name - __CNInterrupt instead of your wrong __CNIInterrupt. Enjoy Smile
     
    ermoser:
    Do not teach others the things you have absolutely no idea about.
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1473
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 06:06:19 (permalink)
    +2 (2)
    Easy typo.
     
    You don't don't have to modify the IVT unless you need to do that at runtime, not at compile time since the .globl __CNInterrupt will cause the linker to write the ivt.
     
    8bit is not relevant to this.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    NorthGuy
    Super Member
    • Total Posts : 4511
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 06:46:16 (permalink)
    +2 (2)
    IVT doesn't contain "gotos" or any other instructions, just addresses of where to jump.
    #6
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1473
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 08:51:03 (permalink)
    +1 (1)
    __CNIInterrupt:
        BCLR IFS1,#3    bclr IFS1,#CNIF
        CPB PORTB        compare portb to wreg with borrow, why? wreg/w0 contains a random value
        BTSC SR,#1      btsc SR,#Z, why replicate a test ?
        BTG PORTB,#15
        RETFIE


    Numbers don't mean Jack. include "xc.h"



    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #7
    RodoPIC
    Junior Member
    • Total Posts : 102
    • Reward points : 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 11:43:30 (permalink)
    0
    BSET PORTB,#15 ;Set LED4 LOW
     
    I'm no expert at this. I keep reading "use LAT for outputs and PORT for inputs". Does this apply to assembly code as well?
     
     
    #8
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/06 11:54:35 (permalink)
    0
    Yes it does
    #9
    asmeng
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2017/08/18 07:50:35
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 12:04:23 (permalink)
    0
    MBedder: I made this change to my code and it's now working with the simulator but not on the actual development board. On the board the interrupt flag is getting set but it still won't enter the ISR.
    post edited by asmeng - 2017/09/07 12:05:32
    #10
    asmeng
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2017/08/18 07:50:35
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 12:12:18 (permalink)
    0
    RodoPIC
    BSET PORTB,#15 ;Set LED4 LOW
     
    I'm no expert at this. I keep reading "use LAT for outputs and PORT for inputs". Does this apply to assembly code as well?
     
     




    The LAT and PORT registers are both used for the I/O pins, they just have different functions. Normally you won't have to use the LAT register for anything. The TRIS register is used to configure the pins as inputs or outputs. The PORT register bits are either read from or written to if they're inputs or outputs respectively.
    #11
    MBedder
    Circuit breaker
    • Total Posts : 6279
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 12:21:15 (permalink)
    +1 (1)
    Writing/altering the individual bits of PORT causes the RMW problems, so use LAT instead. Writing/altering the entire PORT can be done without these problems.
     
    asmengMBedder: I made this change to my code and it's now working with the simulator but not on the actual development board. On the board the interrupt flag is getting set but it still won't enter the ISR.
    How did you discover that? Did you fix your ISR code as Gort2015 suggested?
    #12
    asmeng
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2017/08/18 07:50:35
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 13:58:02 (permalink)
    0
    MBedder
    Writing/altering the individual bits of PORT causes the RMW problems, so use LAT instead. Writing/altering the entire PORT can be done without these problems.
     
    asmengMBedder: I made this change to my code and it's now working with the simulator but not on the actual development board. On the board the interrupt flag is getting set but it still won't enter the ISR.
    How did you discover that? Did you fix your ISR code as Gort2015 suggested?




    The only thing I changed was taking that extra 'I' out of CNIInterrupt. Here's my current code,
     
    ;

    .include "p24fj64ga002.inc"
    .global __reset

    .text

    __reset:

    ;*******************************************************************************

    ;Clear WREG
    CLR WREG

    ;Configure input and output pins
    BSET TRISB,#5 ;Set RB5 as input
    BCLR TRISB,#15 ;Clear RB15 as output
    BSET PORTB,#15 ;Set LED4 LOW

    ;Enable input change notification for CN27
    BSET CNEN2,#11

    ;Configure interrupt control registers
    BSET INTCON1,#15 ;Set NSTDIS control bit to disable nested interrupts
    MOV #0x7000,W0
    IOR IPC4 ;Set input change notification priority to highest
    BCLR IFS1,#3 ;Clear input change notification flag
    BSET IEC1,#3 ;Set input change notification interrupt enable

    ;*******************************************************************************

    ;Clear WREG. WREG is used to compare PORTB values to see if the input
    ;has actually changed.
    CLR WREG

    ;Move PORTB value into W1 register since PORTB,#5 will initially be high
    MOV PORTB,WREG

    LOOP:
    GOTO LOOP

    .global __CNInterrupt
    __CNInterrupt:
    BCLR IFS1,#3
    CPB PORTB
    BTSC SR,#1
    BTG PORTB,#15
    RETFIE

    .end

     
    I used that debugger in both cases to watch the registers.
    #13
    qhb
    Superb Member
    • Total Posts : 6235
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 14:48:24 (permalink)
    +2 (2)
    asmeng
     
    ...
    The LAT and PORT registers are both used for the I/O pins, they just have different functions. Normally you won't have to use the LAT register for anything. 

    This is wrong, wrong, wrong.
    Please do not refute good advice.
    Once you have learnt about the read-modify-write nature of the BSF and BCF instructions, you will see why it is very GOOD advice to only write to the LAT registers, not the PORT registers.
     
    #14
    MBedder
    Circuit breaker
    • Total Posts : 6279
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/07 15:27:32 (permalink)
    +2 (2)
    Fix your buggy ISR code first as suggested above.
    #15
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1473
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/08 03:33:00 (permalink)
    +1 (1)
    On entering the isr, do not assume that wreg/w0 contains a value.
    W1 is not wreg that you have in your comment.
     
    You don't need to check if portb has changed when you are using CN.
     
    You are still not using the equate values from, "xc.inc".  How do you expect people to understand your code?
     
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #16
    asmeng
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2017/08/18 07:50:35
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/12 14:16:59 (permalink)
    0
    Gort2015
    On entering the isr, do not assume that wreg/w0 contains a value.
    W1 is not wreg that you have in your comment.
     
    You don't need to check if portb has changed when you are using CN.
     
    You are still not using the equate values from, "xc.inc".  How do you expect people to understand your code?
     
     
     




    I added the equate names, changed my read and writes to LATB, and simplified the ISR however I'm still having the same problem where the PC won't enter the ISR. I placed my updated code below.
     

    .include "p24fj64ga002.inc"

    .global __reset

    .text

    __reset:

    ;*******************************************************************************

    ;Clear WREG
    CLR WREG

    ;Configure input and output pins
    BSET TRISB,#RB5 ;Set RB5 as input
    BCLR TRISB,#RB15 ;Clear RB15 as output
    BSET LATB,#LATB15 ;Set LED4 LOW

    ;Enable input change notification for CN27
    BSET CNEN2,#CN27IE

    ;Configure interrupt control registers
    BSET INTCON1,#NSTDIS ;Set NSTDIS control bit to disable nested interrupts
    MOV #0x7000,W0
    IOR IPC4 ;Set input change notification priority to highest
    BCLR IFS1,#CNIF ;Clear input change notification flag
    BSET IEC1,#CNIE ;Set input change notification interrupt enable

    ;*******************************************************************************

    ;Clear WREG. WREG is used to compare PORTB values to see if the input
    ;has actually changed.
    CLR WREG

    LOOP:
    GOTO LOOP

    .global __CNInterrupt
    __CNInterrupt:
    BCLR IFS1,#CNIF
    BTG LATB,#LATB15
    RETFIE

    .end

    #17
    MBedder
    Circuit breaker
    • Total Posts : 6279
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/12 14:30:22 (permalink)
    +2 (2)
    RTFM:
     
    When a CN interrupt occurs, the user should read the PORT register associated with the CN pin(s). This will clear the mismatch condition and set up the CN logic to detect the next pin change (FRM, DS39711B-page 12-20)


    #18
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1473
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/12 16:56:06 (permalink)
    +1 (1)
    CN27 not sharing with anything else like analogue?
     
    You should include "xc.inc" instead of the chip device.
    post edited by Gort2015 - 2017/09/12 16:58:18

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #19
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1473
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/12 17:02:03 (permalink)
    +1 (1)
    PGED3/ASDA1/RP5/CN27/PMD7/RB5
     
    No analogue.  Alt. SDA in use?

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2017 APG vNext Commercial Version 4.5