; eBASIC PIC16 Code Generator ; Copyright(c) 2004, Compubotics LLC ; www.Compubotics.com ORG 0x0 ; File: interrupt.bas ; ' ; ' eBASIC interrupt example ; ' ; ' This program illustrates how to code a user interrupt ; ' handler. In this example, we are capturing the TMR0 ; ' overflow interrupt. TMR0 is set to overflow every 1 ms ; ' in the eBASIC startup code. We captuer the interrupt ; ' and increment our tick counter to implement a crude ; ' real-time clock. Meanwhile, we are incrementing a ; ' foreground counter and displaying it's results, just for fun. ; ' ; ' Note that both the foreground and background routines use the ; ' 16-bit math operations. The math library is reentrant. ; ' ; ' See the PIC 16F877 data sheet for more info ; ' ; ' Copyright(c) 2004, Compubotics LLC ; ' Use freely for non-commercial purposes. ; ' www.compubotics.com ; ; #include "rc40.inc" ; #interrupt intHandler ; ; rtcCounter var word ; forecounter var word ; ; ' define some handy bit variables that we need ; #bit T0IF,INTCON,2 ; #bit T0IE,INTCON,5 ; #bit GIE,INTCON,7 ; ; Main: nop clrf 0xa goto _startup ORG 0x4 movwf 0x7c movf 0x3,0 movwf 0x7d movf 0xa,0 movwf 0x7e bcf 0x3,5 bcf 0x3,6 movf 0x70,0 movwf 0x58 movf 0x71,0 movwf 0x59 movf 0x72,0 movwf 0x5a movf 0x73,0 movwf 0x5b movf 0x74,0 movwf 0x5c movf 0x75,0 movwf 0x5d movf 0x76,0 movwf 0x5e movf 0x77,0 movwf 0x5f call intHandler movf 0x58,0 movwf 0x70 movf 0x59,0 movwf 0x71 movf 0x5a,0 movwf 0x72 movf 0x5b,0 movwf 0x73 movf 0x5c,0 movwf 0x74 movf 0x5d,0 movwf 0x75 movf 0x5e,0 movwf 0x76 movf 0x5f,0 movwf 0x77 movf 0x7e,0 movwf 0xa movf 0x7d,0 movwf 0x3 movf 0x7c,0 retfie _STR001: retlw 0x3a retlw 0x0 _startup: movlw 0x83 bsf 0x3,5 movwf 0x81 bcf 0x3,5 bcf 0xb,5 bsf 0xb,6 movlw 0x31 movwf 0x10 movlw 0x60 movwf 0x78 call _uartSetup goto Main ; ; ' enable timer 0 interrupt ; T0IE = 1 Main: bcf 0x3,5 bcf 0x3,6 bsf 0xb,5 ; ; ' global interrupt enable ; GIE = 1 bsf 0xb,7 ; ; ' loop here forever ; while (1) ; ' increment the foreground counter ; forecounter = forecounter + 1 _j000: movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 movlw 0x1 movwf 0x72 movlw 0x0 movwf 0x73 call _add16a bcf 0x3,5 bcf 0x3,6 movf 0x70,0 movwf 0x22 movf 0x71,0 movwf 0x23 ; ; ' display the counters every 100th tick of the foreground counter ; if (forecounter // 100) = 0 then movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 movlw 0x64 movwf 0x72 movlw 0x0 movwf 0x73 call _div16x movlw 0x0 movwf 0x72 movlw 0x0 movwf 0x73 call _comp16 btfsc 0x3,2 goto _j002 clrf 0x70 clrf 0x71 goto _j003 _j002: movlw 0x1 movwf 0x70 movlw 0x0 movwf 0x71 _j003: clrf 0x72 clrf 0x73 call _comp16 btfsc 0x3,2 goto _j004 ; debug dec forecounter,":", DEC rtcCounter, cr bcf 0x3,5 bcf 0x3,6 movf 0x22,0 movwf 0x70 movf 0x23,0 movwf 0x71 call _dbgDec movlw 0x32 movwf 0x70 movlw 0x0 movwf 0x71 call _dbgStr bcf 0x3,5 bcf 0x3,6 movf 0x20,0 movwf 0x70 movf 0x21,0 movwf 0x71 call _dbgDec movlw 0xd call _uartTX movlw 0xa call _uartTX ; endif ; ; wend _j004: goto _j000 ; ; ; end _j001: sleep goto $-1 ; ; ' ------------------------- ; ' interrupt handler routine ; ' ------------------------- ; intHandler: ; ; ' handle TMR0 interrupt ; if (T0IF) then intHandler: bcf 0x3,5 bcf 0x3,6 btfss 0xb,2 goto _j005 movlw 0x1 movwf 0x70 movlw 0x0 movwf 0x71 goto _j006 _j005: clrf 0x70 clrf 0x71 _j006: clrf 0x72 clrf 0x73 call _comp16 btfsc 0x3,2 goto _j007 ; ' increment our RTC ; rtcCounter = rtcCounter + 1 bcf 0x3,5 bcf 0x3,6 movf 0x20,0 movwf 0x70 movf 0x21,0 movwf 0x71 movlw 0x1 movwf 0x72 movlw 0x0 movwf 0x73 call _add16a bcf 0x3,5 bcf 0x3,6 movf 0x70,0 movwf 0x20 movf 0x71,0 movwf 0x21 ; ' MUST RESET TMR0 INTERRUPT FLAG!!! ; T0IF = 0 bcf 0xb,2 ; endif ; ; return _j007: return _uartSetup: movlw 0x67 bsf 0x3,5 bcf 0x3,6 movwf 0x99 bsf 0x98,2 bsf 0x98,5 bcf 0x3,5 bsf 0x18,4 bsf 0x18,7 return _add16a: movf 0x72,0 addwf 0x70,1 movf 0x73,0 btfsc 0x3,0 incf 0x73,0 addwf 0x71,1 return _push: bcf 0xb,7 movwf 0x79 movf 0x78,0 movwf 0x4 movf 0x79,0 movwf 0x0 incf 0x78,1 bsf 0xb,7 return _pop: bcf 0xb,7 decf 0x78,1 movf 0x78,0 movwf 0x4 movf 0x0,0 bsf 0xb,7 return _shftl16: bcf 0x3,0 rlf 0x72,1 rlf 0x73,1 decfsz 0x74,1 goto _shftl16 return _shftr16: bcf 0x3,0 rrf 0x73,1 rrf 0x72,1 decfsz 0x74,1 goto _shftr16 return _sub16a: movf 0x72,0 subwf 0x70,1 movf 0x73,0 btfss 0x3,0 incf 0x73,0 subwf 0x71,1 return _div16x: clrf 0x76 clrf 0x77 movf 0x72,0 iorwf 0x73,0 btfsc 0x3,2 retlw 0xff movlw 0x1 movwf 0x74 movf 0x73,0 iorlw 0x0 btfss 0x3,2 goto _j008 movf 0x72,0 xorlw 0x1 btfss 0x3,2 goto _j008 movf 0x70,0 movwf 0x76 movf 0x71,0 movwf 0x77 clrf 0x70 clrf 0x71 retlw 0x0 _j008: movf 0x70,0 iorwf 0x71,0 btfss 0x3,2 goto _j009 retlw 0x0 _j009: btfsc 0x73,7 goto _j010 incf 0x74,1 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftl16 call _pop movwf 0x74 goto _j009 _j010: movf 0x72,0 call _push movf 0x73,0 call _push movf 0x76,0 movwf 0x72 movf 0x77,0 movwf 0x73 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftl16 call _pop movwf 0x74 movf 0x72,0 movwf 0x76 movf 0x73,0 movwf 0x77 call _pop movwf 0x73 call _pop movwf 0x72 call _sub16a btfsc 0x3,0 goto _j011 call _add16a goto _j012 _j011: bsf 0x76,0 _j012: decf 0x74,1 btfsc 0x3,2 retlw 0x0 movf 0x74,0 call _push movlw 0x1 movwf 0x74 call _shftr16 call _pop movwf 0x74 goto _j010 _comp16: movf 0x73,0 subwf 0x71,0 btfss 0x3,2 return movf 0x72,0 subwf 0x70,0 return _compza: movlw 0x0 iorwf 0x71,0 btfss 0x3,2 return iorwf 0x70,1 return _inc16: incfsz 0x70,0 decf 0x71,1 incf 0x71,1 movwf 0x70 iorwf 0x71,0 return _neg16: comf 0x70,1 comf 0x71,1 goto _INC16 _dbgSDec: btfss 0x71,7 goto _dbgDec movlw 0x2d call _uartTX call _neg16 _dbgDec: clrf 0x50 clrf 0x51 clrf 0x52 clrf 0x53 clrf 0x54 movlw 0x5 movwf 0x75 _j013: decf 0x75,1 movlw 0xa movwf 0x72 clrf 0x73 call _div16x movlw 0x50 addwf 0x75,0 movwf 0x4 movf 0x70,0 addlw 0x30 movwf 0x0 movf 0x76,0 movwf 0x70 movf 0x77,0 movwf 0x71 call _compza btfsc 0x3,2 goto _j014 movf 0x75,0 iorlw 0x0 btfss 0x3,2 goto _j013 _j014: movlw 0x0 movwf 0x74 _j015: movlw 0x50 addwf 0x74,0 movwf 0x4 movf 0x0,0 btfss 0x3,2 call _uartTX incf 0x74,1 movf 0x74,0 xorlw 0x5 btfss 0x3,2 goto _j015 return _uartTX: bcf 0x3,5 bcf 0x3,6 btfss 0xc,4 goto $-1 movwf 0x19 return _dbgStr: call _j016 iorlw 0x0 btfsc 0x3,2 return call _uartTX call _inc16 goto _dbgStr _j016: movf 0x71,0 movwf 0xa movf 0x70,0 movwf 0x2 END ; end of code generation