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

Page: < 12 Showing page 2 of 2
Author
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/13 17:29:07 (permalink)
+1 (1)
Gort2015
PGED3/ASDA1/RP5/CN27/PMD7/RB5

No analogue.  Alt. SDA in use?



I'm certain it isn't being used by anything else. All the code I wrote is here except for the config.c file.
#21
dan1138
Super Member
  • Total Posts : 2541
  • Reward points : 0
  • Status: offline
Re: Change Notification Interrupt on PIC24 in Assembly 2017/09/13 23:07:13 (permalink)
+1 (1)
This is a complete, tested, working example:
    .nolist
.title " Sample PIC24FJ Assembler Source Code"
.sbttl " Change Notification interrupt handling"
.psize 1000,132
.list
;
; File: main.s
; Target: PIC24FJ64GA002
; IDE: MPLABX v4.00
; Compiler: XC16 v1.32
;
; PIC24FJ64GA002
; +--------------:_:--------------+
; ICD_VPP -> : 1 MCLR VDD 28 : <- PWR
; <> : 2 RA0/AN0 VSS 27 : <- GND
; <> : 3 RA1/AN1 RP15/RB15 26 : <> LED4
; ICD_PGC <> : 4 RB0/PGC1 RP14/RB14 25 : <> LED5
; ICD_PGD <> : 5 RB1/PGD1 RP13/RB13 24 : <> LED6
; <> : 6 RB2/RP2 RP12/RB12 23 : <> LED7
; <> : 7 RB3/RP3 RP11/RB11 22 : <>
; GND <> : 8 VSS RP10/RB10 21 : <>
; <> : 9 RA2/OSCI VCAP 20 : <- 10uF cap
; <> : 10 RA3/OSCO DISVREG 19 : <- GND
; <> : 11 RB4/SOSCI RP9/RB9 18 : <>
; <> : 12 RA4/SOSCO RP8/RB8 17 : <>
; PWR <> : 13 VDD INT0/RB7 16 : <>
; SW1 <> : 14 RB5/RP5/CN27 RP6/RB6 15 : <>
; +-------------------------------+
; DIP-28
; Description:
; Using the change notification interrupt toggle
; the LED connected to PORTB bit 15 when the
; input on PORTB bit 5 changes.
;
; Notes:
; This code test using the DM300027, 16-Bit 28-Pin Starter Board
; http://ww1.microchip.com/downloads/en/DeviceDoc/51656B.pdf
;
.include "xc.inc"
.text

config __CONFIG1, (WDTPS_PS32768 & FWPSA_PR128 & WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & BKBUG_OFF & GWRP_OFF & GCP_OFF & JTAGEN_OFF)
config __CONFIG2, (IESO_OFF & FNOSC_FRC & FCKSM_CSDCMD & OSCIOFNC_ON & IOL1WAY_OFF & I2C1SEL_SEC & POSCMOD_NONE)

.global __reset
__reset:
mov #__SP_init,w15 ; Initialize the Stack Pointer
mov #__SPLIM_init,w0 ; Initialize the Stack Pointer Limit Register
mov w0, SPLIM
nop ; Add NOP to follow SPLIM initialization

call _wreg_init ; Set all working registers to zero

call _PIC_init ; Initialize this PIC

;
; Setup to drive LED on PORTB bit 15
;
bset LATB,#15
bclr TRISB,#15
;
; Setup for change notification on PORTB bit 4 input.
; This is the Input Change Notification for CN27
;
bclr IEC1,#CNIE ; Disable change notification interrupt
clr CNEN1 ; Disable all change notification bits
clr CNEN2
bset CNEN2,#CN27IE ; Enable change notification for PORTB bit 5
bclr IPC4,#CNIP0 ; Set Input Change Notification
bclr IPC4,#CNIP1 ; interrupt priority to 4
bset IPC4,#CNIP2 ;
bclr IFS1,#CNIF ; Clear change notification assert
bset IEC1,#CNIE ; Enable change notification interrupt

;
; Application loop, just waits for an interrupt.
;
ApplicationLoop:
bra ApplicationLoop
;
; Initialize W registers to zero
;
_wreg_init:
clr w0
mov w0,w14
repeat #12
mov w0,[++w14]
clr w14
return
;
; Initialize this PIC
;
_PIC_init:
mov #0,w0
mov.w w0,CLKDIV ; Set for default clock operations

bset INTCON1,#NSTDIS ; Disable interrupt nesting

mov #0xFFFF,w0
mov.w w0,AD1PCFG ; Set for digital I/O

mov #OSCCON,w1 ; Unlock peripheral pin selection
mov #0x46,w2
mov #0x57,w3
mov.b w2,[w1]
mov.b w3,[w1]
bclr.b OSCCON,#6

mov #0x1f1f,w0 ; Unmap all inputs
mov w0,RPINR0
mov w0,RPINR1
mov w0,RPINR3
mov w0,RPINR4
mov w0,RPINR7
mov w0,RPINR8
mov w0,RPINR9
mov w0,RPINR11
mov w0,RPINR18
mov w0,RPINR19
mov w0,RPINR20
mov w0,RPINR21
mov w0,RPINR22
mov w0,RPINR23

mov #0x0000,w0 ; Unmap all outputs
mov w0,RPOR0
mov w0,RPOR1
mov w0,RPOR2
mov w0,RPOR3
mov w0,RPOR4
mov w0,RPOR5
mov w0,RPOR6
mov w0,RPOR7

mov #OSCCON,w1 ; Lock peripheral pin selection
mov #0x46,w2
mov #0x57,w3
mov.b w2,[w1]
mov.b w3,[w1]
bset.b OSCCON,#6

return
;
; Input Change Notification handler
;
.global __CNInterrupt
__CNInterrupt:
btg LATB,#15 ; *** DEBUG *** Toggle LED to show we got to the ISR

cp0 PORTB ; Clear change notification mismatch
bclr IFS1,#CNIF ; Clear change notification assert
retfie

;
; Interrupt vectors without handlers are trapped here
;
.global __DefaultInterrupt
__DefaultInterrupt:
nop
bra $-2
retfie
;
; Setup the interrupt vectors
; for system traps.
;
.global __OscillatorFail
.global __AddressError
.global __StackError
.global __MathError

.global __AltOscillatorFail
.global __AltAddressError
.global __AltStackError
.global __AltMathError


.text

; Default Exception Vector handlers if ALTIVT(INTCON2<15>) = 0

; Oscillator Fail Trap
__OscillatorFail:
bclr INTCON1, #OSCFAIL
nop
bra $-2
retfie

; Address Error Trap
__AddressError:
bclr INTCON1, #ADDRERR
nop
bra $-2
retfie

; Stack Error Trap
__StackError:
bclr INTCON1, #STKERR
nop
bra $-2
retfie

; Math Error Trap
__MathError:
bclr INTCON1, #MATHERR
nop
bra $-2
retfie


; Alternate Exception Vector handlers if ALTIVT(INTCON2<15>) = 1

; Alternate Oscillator Fail Trap
__AltOscillatorFail:
bclr INTCON1, #OSCFAIL
nop
bra $-2
retfie

; Alternate Address Error Trap
__AltAddressError:
bclr INTCON1, #ADDRERR
nop
bra $-2
retfie

; Alternate Stack Error Trap
__AltStackError:
bclr INTCON1, #STKERR
nop
bra $-2
retfie

; Alternate Math Error Trap
__AltMathError:
bclr INTCON1, #MATHERR
nop
bra $-2
retfie

.end

Please understand this this code is a HACK.

It should be used only as an example how how the change notification interrupt can be setup.

THIS CODE IS NOT A GOOD METHOD TO DETECT CHANGES OF A SWITCH INPUT!
post edited by dan1138 - 2017/09/13 23:18:50
#22
Page: < 12 Showing page 2 of 2
Jump to:
© 2017 APG vNext Commercial Version 4.5