Home > pic18f, Uncategorized > pic18f assembly example 3 – an interrupt example

pic18f assembly example 3 – an interrupt example

;-----------------------------------------------------------------------------------------------------------------------------
; This assembly code uses a timer interrupt to set LATD2 high for 50ms then low for 50ms, repeatedly.
;
; by David Dorran (https://dadorran.wordpress.com) May 2014
;---------------------------------------------------------------------


; TRY TO DO THE FOLLOWING IN ORDER TO DEVELOP YOUR PROGRAMMING SKILLS:
;    1. Use the counter register set up in the code to change the on-off rate from 10Hz to 1Hz

;configure the  assembler directive 'list' so as to set processor to 18f4620 and set the radix used for data expressions to decimal (can be HEX|DEC|OCT)

    list p=18f4620, r=DEC
    #include <p18f4620.inc>
; configure the micro so that the watchdog timer is off, low-voltage programming is off, master clear is off and the clock works off the internal oscillator
    config WDT=OFF, LVP=OFF, MCLRE=OFF, OSC=INTIO67
;The org directive tells the compiler where to position the code in memory
    org 0x0000 ;The following code will be programmed in reset address location i.e. This is where the micro jumps to on reset
    goto Main ;Jump to Main immediately after a reset
 
 ;NOTE: When an interrupt is triggered the micro jumps to 0x0008 for high priority and 0x0018 for low priority. 
    org 0x0008
    goto hi_isr
    org 0x0018
    goto low_isr
 
;--------------------------------------------------------------------------
; Main Program -------------------------------------------------------------
;--------------------------------------------------------------------------
    org 0x0100 
Main
counter  equ  0x10 ; counter refers to a register at address 0x10
    clrf counter ; clear the counter
    
    ; configure LATD0-LATD3 as outputs and RD4-RD7 as inputs
    banksel TRISD
    movlw 0xf0
    movwf TRISD
    
    clrf LATD ; turn off all LATD output pins
    bsf LATD,3 ; turn on LATD3
    
    ; Set clock frequency (section 2 of the PIC18F4620 Data Sheet)
    ; Set Fosc = 8MHz, which gives Tcy = 0.5us
    movlw B'01110000'
    movwf OSCCON 

    bcf INTCON, GIE ;Disable global interrupts
    bcf T0CON, TMR0ON; turn off timer 0       
    bsf INTCON, TMR0IE; Enable TIMER0 interupt
     
    ;set up 1:2 prescaler so that TMR0 SFR is incremented every 2 Tcy  i.e. 1us
    bcf T0CON,0
    bcf T0CON,1
    bcf T0CON,2
    
    bcf T0CON, T0CS; use internal instruction cycle clock
    bcf T0CON, T08BIT; use 16 bit mode
    bcf T0CON, PSA; turn on prescaler
    
    ; setup so that timer0 overflow is triggered quickly
    ; after the first trigger then set it up so that it is triggered at the rate you want
    movlw 0xFF
    movwf TMR0L
    movlw 0xFF
    movwf TMR0H
    
    bsf INTCON2, TMR0IP; ; Set the timer0 interrupt up as high priority
    bsf INTCON, GIE ;Enable global interrupts  
    bsf T0CON, TMR0ON; turn on timer 0    
    
loop   
    nop; ;loop waiting for interrupts to be trigged
    goto loop   

    ;-----High Priority Interrupt Service Routine -------------------------------------------------------------
    org 0x0200 
hi_isr
    btfss INTCON, TMR0IF
    goto end_int
    bcf INTCON, TMR0IF; reset interrupt
    ;An interrupt has been set up to be generated whenever TMR0 goes from FFFFh to 0000h i.e. on overflow.
    ;Would like timer overflow flag triggered every 500ms so will set timer to 0xffff - 1000 = 0x£CAF
    movlw 0xAF
    movwf TMR0L
    movlw 0x3C
    movwf TMR0H
    
    incf counter, f ; increment the counter
    
    ;invert LATD2
    movlw 0x4
    xorwf LATD,F

end_int
    retfie ;return and reset interrupts
 

    ;-----Low Priority Interrupt Service Routine  -------------------------------------------------------------   
    org 0x0300 
low_isr
    nop
    retfie ;return and reset interrupts

    end ; End of ASM code
Categories: pic18f, Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: