MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 1 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00001 ;********************************************************************** 00002 ; This file is a sample showing how to do interrupt processing on * 00003 ; the PIC16F874. * 00004 ; * 00005 ;********************************************************************** 00006 ; * 00007 ; Filename: testinterrupts.asm * 00008 ; Date: 17 February 2004 * 00009 ; File Version: 1 * 00010 ; * 00011 ; Author: CDR Charles B. Cameron * 00012 ; Company: United States Naval Academy * 00013 ; * 00014 ; * 00015 ;********************************************************************** 00016 ; * 00017 ; Files required: * 00018 ; * 00019 ; p16f874.inc * 00020 ; * 00021 ;********************************************************************** 00022 ; * 00023 ; Notes: * 00024 ; This program sets up Timer 2 to generate an interrupt every * 00025 ; 40 us. The ISR counts four such interrupts and toggles * 00026 ; Bit 0 of Port B. Since this bit toggles every 160 us, the * 00027 ; period is twice this, or 320 us, making a frequency of * 00028 ; 3.125 kHz for Bit 0. All these times are dependent on the * 00029 ; crystal oscillator running at 4 MHz. * 00030 ;********************************************************************** 00031 00032 00033 list p=16f874,b=4 ; list directive to define processor 00034 #include ; processor specific variable definitions 00001 LIST 00002 ; P16F874.INC Standard Header File, Version 1.00 Microchip Technology, Inc. 00374 LIST 00035 2007 3F32 00036 __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OF F 00037 00038 ; Turn code protection off, watch dog timer off, 00039 ; brown-out reset disabled, power-up timer enabled, HS oscillator mode selected, 00040 ; flash program memory write disabled, low-voltage in-circuit serial programming 00041 ; disabled, and data EE memory code protection off. 00042 00043 00044 ;***** VARIABLE DEFINITIONS 00000020 00045 w_temp equ 0x20 ; variable used for context saving 00000021 00046 status_temp equ 0x21 ; variable used for context saving 00000022 00047 Timer2Count equ 0x22 ; used to count Timer 2 interrupts 00048 00049 ; With the postscaler = 5, prescaler = 1, and PR2 = 8, we get MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 2 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00050 ; 5 x 1 x 8 = 40 instruction cycles, or 40 us, between interrupts 00000020 00051 T2CON_Init equ B'00100000' ; 1:5 Postscale, TM2OFF, Prescaler = 1 00000008 00052 PR2_Init equ D'8' ; TMR2 rolls over after 8 increments 00053 00000002 00054 PIE1_Init equ B'00000010' ; Mask pointing to the TMR2IE bit of PIE1 000000C0 00055 INTCON_Init equ B'11000000' ; Mask pointing to GIE and PEIE of INTCON 00000001 00056 LEDToggleBit equ B'00000001' ; Mask for toggling bit 0 of PORTB 00000000 00057 TRISB_Init equ B'00000000' ; Make all bits of PORT B output bits 00000004 00058 Timer2MagicNumber equ D'4' ; We'll count 4 Timer 2 interrupts. 00059 ; This represents 160 us between occasions 00060 ; when Port B Bit 0 is toggled, giving a 00061 ; period of 320 us in all, or a frequency 00062 ; of 3.125 kHz. 00063 00064 00065 00066 ;********************************************************************** 0000 00067 ORG 0x000 ; processor reset vector 0000 018A 00068 clrf PCLATH ; ensure page bits are cleared 0001 2810 00069 goto main ; go to beginning of program 00070 00071 0004 00072 ORG 0x004 ; interrupt vector location 0004 00A0 00073 movwf w_temp ; save off current W register contents 0005 0803 00074 movf STATUS,W ; move status register into W register 0006 1283 00075 bcf STATUS,RP0 ; ensure file register bank set to 0 0007 00A1 00076 movwf status_temp ; save off contents of STATUS register 00077 00078 00079 ; Check for TMR2 interrupt Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. 0008 188C 00080 btfsc PIE1,TMR2IF 0009 2021 00081 call Handle_Timer2 00082 00083 000A 1283 00084 bcf STATUS,RP0 ; ensure file register bank set to 0 000B 0821 00085 movf status_temp,W ; retrieve copy of STATUS register 000C 0083 00086 movwf STATUS ; restore pre-isr STATUS register contents 000D 0EA0 00087 swapf w_temp,F 000E 0E20 00088 swapf w_temp,W ; restore pre-isr W register contents 000F 0009 00089 retfie ; return from interrupt 00090 00091 00092 0010 00093 main 00094 00095 ; Initialize Timer 2 00096 ; Initialize TMR2 0010 0191 00097 clrf TMR2 00098 ; Initialize T2CON 0011 3020 00099 movlw T2CON_Init 0012 0092 00100 movwf T2CON 00101 ; Initialize PR2 MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 3 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0013 1683 00102 bsf STATUS,RP0 ; Bank 1 0014 3008 00103 movlw PR2_Init Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. 0015 0092 00104 movwf PR2 00105 ; Initialize PIE1 0016 3002 00106 movlw PIE1_Init Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. 0017 048C 00107 iorwf PIE1,F 0018 1283 00108 bcf STATUS,RP0 ; Bank 0- 00109 ; Initialize INTCON 0019 30C0 00110 movlw INTCON_Init 001A 048B 00111 iorwf INTCON,F 00112 ; Start Timer 2 001B 1512 00113 bsf T2CON,TMR2ON 00114 00115 ; Initialize the Port B direction bits 001C 1683 00116 bsf STATUS,RP0 ; Bank 1 001D 3000 00117 movlw TRISB_Init Message[302]: Register in operand not in bank 0. Ensure that bank bits are correct. 001E 0086 00118 movwf TRISB 001F 1283 00119 bcf STATUS,RP0 ; Bank 0 00120 0020 00121 loop 00122 ; This loop doesn't do anything. The main point of 00123 ; this program is to illustrate interrupt processing. 0020 2820 00124 goto loop 00125 0021 00126 Handle_Timer2 00127 ; Increment counter of timer 2 interrupts 0021 0AA2 00128 incf Timer2Count,F 00129 ; Have we reached the magic number yet? 0022 3004 00130 movlw Timer2MagicNumber 0023 0222 00131 subwf Timer2Count,W 0024 1D03 00132 btfss STATUS,Z 0025 2829 00133 goto EndOfTimer2 ; No 00134 ; Reinitialize Timer2Count ; Yes 0026 01A2 00135 clrf Timer2Count 00136 ; Toggle output bit 0027 3001 00137 movlw LEDToggleBit 0028 0686 00138 xorwf PORTB,F 00139 0029 00140 EndOfTimer2 00141 ; Clear Timer 2 interrupts so that another Timer 2 00142 ; interval can ensue 0029 108C 00143 bcf PIR1,TMR2IF 002A 0008 00144 return 00145 00146 END ; directive 'end of program' MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 4 SYMBOL TABLE LABEL VALUE ACKDT 00000005 ACKEN 00000004 ACKSTAT 00000006 ADCON0 0000001F ADCON1 0000009F ADCS0 00000006 ADCS1 00000007 ADDEN 00000003 ADFM 00000007 ADIE 00000006 ADIF 00000006 ADON 00000000 ADRESH 0000001E ADRESL 0000009E BCLIE 00000003 BCLIF 00000003 BF 00000000 BRGH 00000002 C 00000000 CCP1CON 00000017 CCP1IE 00000002 CCP1IF 00000002 CCP1M0 00000000 CCP1M1 00000001 CCP1M2 00000002 CCP1M3 00000003 CCP1X 00000005 CCP1Y 00000004 CCP2CON 0000001D CCP2IE 00000000 CCP2IF 00000000 CCP2M0 00000000 CCP2M1 00000001 CCP2M2 00000002 CCP2M3 00000003 CCP2X 00000005 CCP2Y 00000004 CCPR1H 00000016 CCPR1L 00000015 CCPR2H 0000001C CCPR2L 0000001B CHS0 00000003 CHS1 00000004 CHS2 00000005 CKE 00000006 CKP 00000004 CREN 00000004 CSRC 00000007 D 00000005 DATA_ADDRESS 00000005 DC 00000001 D_A 00000005 EEADR 0000010D MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 5 SYMBOL TABLE LABEL VALUE EEADRH 0000010F EECON1 0000018C EECON2 0000018D EEDATA 0000010C EEDATH 0000010E EEIE 00000004 EEIF 00000004 EEPGD 00000007 EndOfTimer2 00000029 F 00000001 FERR 00000002 FSR 00000004 GCEN 00000007 GIE 00000007 GO 00000002 GO_DONE 00000002 Handle_Timer2 00000021 I2C_DATA 00000005 I2C_READ 00000002 I2C_START 00000003 I2C_STOP 00000004 IBF 00000007 IBOV 00000005 INDF 00000000 INTCON 0000000B INTCON_Init 000000C0 INTE 00000004 INTEDG 00000006 INTF 00000001 IRP 00000007 LEDToggleBit 00000001 NOT_A 00000005 NOT_ADDRESS 00000005 NOT_BO 00000000 NOT_BOR 00000000 NOT_DONE 00000002 NOT_PD 00000003 NOT_POR 00000001 NOT_RBPU 00000007 NOT_RC8 00000006 NOT_T1SYNC 00000002 NOT_TO 00000004 NOT_TX8 00000006 NOT_W 00000002 NOT_WRITE 00000002 OBF 00000006 OERR 00000001 OPTION_REG 00000081 P 00000004 PCFG0 00000000 PCFG1 00000001 PCFG2 00000002 PCFG3 00000003 MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 6 SYMBOL TABLE LABEL VALUE PCL 00000002 PCLATH 0000000A PCON 0000008E PEIE 00000006 PEN 00000002 PIE1 0000008C PIE1_Init 00000002 PIE2 0000008D PIR1 0000000C PIR2 0000000D PORTA 00000005 PORTB 00000006 PORTC 00000007 PORTD 00000008 PORTE 00000009 PR2 00000092 PR2_Init 00000008 PS0 00000000 PS1 00000001 PS2 00000002 PSA 00000003 PSPIE 00000007 PSPIF 00000007 PSPMODE 00000004 R 00000002 RBIE 00000003 RBIF 00000000 RC8_9 00000006 RC9 00000006 RCD8 00000000 RCEN 00000003 RCIE 00000005 RCIF 00000005 RCREG 0000001A RCSTA 00000018 RD 00000000 READ_WRITE 00000002 RP0 00000005 RP1 00000006 RSEN 00000001 RX9 00000006 RX9D 00000000 R_W 00000002 S 00000003 SEN 00000000 SMP 00000007 SPBRG 00000099 SPEN 00000007 SREN 00000005 SSPADD 00000093 SSPBUF 00000013 SSPCON 00000014 SSPCON2 00000091 MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 7 SYMBOL TABLE LABEL VALUE SSPEN 00000005 SSPIE 00000003 SSPIF 00000003 SSPM0 00000000 SSPM1 00000001 SSPM2 00000002 SSPM3 00000003 SSPOV 00000006 SSPSTAT 00000094 STATUS 00000003 SYNC 00000004 T0CS 00000005 T0IE 00000005 T0IF 00000002 T0SE 00000004 T1CKPS0 00000004 T1CKPS1 00000005 T1CON 00000010 T1INSYNC 00000002 T1OSCEN 00000003 T1SYNC 00000002 T2CKPS0 00000000 T2CKPS1 00000001 T2CON 00000012 T2CON_Init 00000020 TMR0 00000001 TMR1CS 00000001 TMR1H 0000000F TMR1IE 00000000 TMR1IF 00000000 TMR1L 0000000E TMR1ON 00000000 TMR2 00000011 TMR2IE 00000001 TMR2IF 00000001 TMR2ON 00000002 TOUTPS0 00000003 TOUTPS1 00000004 TOUTPS2 00000005 TOUTPS3 00000006 TRISA 00000085 TRISB 00000086 TRISB_Init 00000000 TRISC 00000087 TRISD 00000088 TRISE 00000089 TRISE0 00000000 TRISE1 00000001 TRISE2 00000002 TRMT 00000001 TX8_9 00000006 TX9 00000006 TX9D 00000000 MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 8 SYMBOL TABLE LABEL VALUE TXD8 00000000 TXEN 00000005 TXIE 00000004 TXIF 00000004 TXREG 00000019 TXSTA 00000098 Timer2Count 00000022 Timer2MagicNumber 00000004 UA 00000001 W 00000000 WCOL 00000007 WR 00000001 WREN 00000002 WRERR 00000003 Z 00000002 _BODEN_OFF 00003FBF _BODEN_ON 00003FFF _CPD_OFF 00003FFF _CPD_ON 00003EFF _CP_ALL 00000FCF _CP_HALF 00001FDF _CP_OFF 00003FFF _CP_UPPER_256 00002FEF _DEBUG_OFF 00003FFF _DEBUG_ON 000037FF _HS_OSC 00003FFE _LP_OSC 00003FFC _LVP_OFF 00003F7F _LVP_ON 00003FFF _PWRTE_OFF 00003FFF _PWRTE_ON 00003FF7 _RC_OSC 00003FFF _WDT_OFF 00003FFB _WDT_ON 00003FFF _WRT_ENABLE_OFF 00003DFF _WRT_ENABLE_ON 00003FFF _XT_OSC 00003FFD __16F874 00000001 loop 00000020 main 00000010 status_temp 00000021 w_temp 00000020 MPASM 03.60 Released TESTINTERRUPTS.ASM 2-18-2004 13:16:11 PAGE 9 MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XX--XXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXX----- ---------------- 2000 : -------X-------- ---------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 41 Program Memory Words Free: 4055 Errors : 0 Warnings : 0 reported, 0 suppressed Messages : 4 reported, 0 suppressed