Tuesday, May 18, 2010

Timer0 interrupt generation Assembly code for microchip pic16f877

list p=16f877
include <dev_fam.inc> ;this file is checked and corrected
include <math16.inc> ;
include <p16f877.inc>
;**********************************************************
;DEFINITIONS:CONSTANTS
FALSE equ 0
TRUE equ 1
PP0 equ b'00000000' ;PIC program page
PP1 equ b'00001000'
PP2 equ b'00010000'
PP3 equ b'00011000'
;------------------------------------------------------------
; RAM variables
    ORG 0X20
;-------------------------------------------------------------
PROGRAMPAGE set PP0
    org 0x00 ;RESET VECTOR
    goto start ;RUN main routine
    org 0x04 ;Interrupt VECTOR
;you can enter your interrupt check routines here
    goto ISR
;------------------------------------------------------------------
    org 0xA0
    start
    ;code for Initialization of Timer0 and other bits
       BANKSEL   INTCON ;BANK2
       MOVLW   0xA0
       MOVWF   INTCON ;Global interrupt and TMR0 interrupt enabled
    ;set timer 0
    banksel TMR0
    movlw b'00000000'   
    movwf TMR0
    ;get one timer 0 inturrupt after 256 triggers
    banksel OPTION_REG
    movlw b'10000110'   
    movwf OPTION_REG
    ;set port B as output
    banksel TRISB
    movlw b'11110000'
    movwf TRISB        
    banksel PORTB
    movlw b'00000000'
    movwf PORTB
    loop
    ;loops forever until inturrupts come
    goto loop
    ISR
    ;handling the interrupt
       comf  PORTB,1    ; Toggle Port B
      BANKSEL   INTCON ;BANK0
       BCF      INTCON,2 ;CLEAR INTERRUPT FLAG
    Retfie ;return and set global in turrupt enable
    end
--------------------------------------------------------------------------

2 comments:

  1. hi,
    i've got an assignment.
    if timer 1 is executing its ISR, a timer0 interrupt occurs and it should handle timer0 interrupt and return to the previous state after completing timer0 interrupt.
    can u pls help me doing this assignment.
    my id:luk4ami@gmail.com

    ReplyDelete
  2. What you have to do is set some flag after timer1 ISR executes then when timer0 ISR executes check that flag and do whatever you want and clear that flag bit.

    ReplyDelete