;---------------------------------------------------------------------------- ;picbench.asm ;PICBench SCADA Example ;Copyright Andrew Ferry 2004 ;---------------------------------------------------------------------------- ;This illustrates the application of the PICBench Comms Protocol Driver Macros ;---------------------------------------------------------------------------- ;assembler setup errorlevel -302 ;suppress "not in bank 0" message ;---------------------------------------------------------------------------- ;includes ;CHANGE THIS TO SUIT YOUR DEVICE IFDEF __16F627 #include ;processor specific definitions ENDIF IFDEF __16F876 #include ;processor specific definitions ENDIF ;---------------------------------------------------------------------------- ;device config ;CHANGE THIS TO SUIT YOUR APPLICATION IFDEF __16F876 __CONFIG _DEBUG_OFF & _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _RC_OSC & _LVP_OFF ENDIF IFDEF __16F627 __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_CLKOUT & _LVP_OFF ENDIF ;---------------------------------------------------------------------------- ;storage for context during interrupt CBLOCK 0x20 WREG_TEMP ;storage for WREG during interrupt STATUS_TEMP ;storage for STATUS during interrupt PCLATH_TEMP ;storage for PCLATH during interrupt FSR_TEMP ;storage for FSR during interrupt ENDC ;---------------------------------------------------------------------------- ;SCADA protocol macro includes #include #include ;---------------------------------------------------------------------------- ;this code executes when a reset occurs. ORG 0x0000 ;place code at reset vector ResetCode: nop clrf PCLATH ;select program memory page 0 goto Main ;go to beginning of program ;---------------------------------------------------------------------------- ;this code executes when an interrupt occurs. ORG 0x0004 ;place code at interrupt vector ;save context bcf STATUS,RP0 ;bank0 bcf STATUS,RP1 movwf WREG_TEMP ;save WREG movf STATUS,W ;store STATUS in WREG clrf STATUS ;select file register bank0 movwf STATUS_TEMP ;save STATUS value movf PCLATH,W ;store PCLATH in WREG movwf PCLATH_TEMP ;save PCLATH value clrf PCLATH ;select program memory page0 movf FSR,W ;store FSR in WREG movwf FSR_TEMP ;save FSR value btfsc PIR1,RCIF ;test receive interrupt goto RcISR ;if set do ISR ;tx should be the last interrupt serviced ;as the flag is nearly always set btfsc PIR1,TXIF ;test transmit interrupt goto TxISR ;if set do ISR clrf PIR1 IFDEF __16F876 clrf PIR2 ENDIF goto EndISR ;---------------------------------------------------------------------------- ;Interrupt service routines RcISR: ;USART receive ready SCADA_RX ;RCIF is cleared automatically by hardware goto EndISR TxISR: ;USART transmit ready SCADA_TX ;TXIF is only ever clear when a transmit is pending ;-------------------------------------------------------------- EndISR: ;restore context movf FSR_TEMP,W ;get saved FSR value movwf FSR ;restore FSR movf PCLATH_TEMP,W ;get saved PCLATH value movwf PCLATH ;restore PCLATH movf STATUS_TEMP,W ;get saved STATUS value movwf STATUS ;restore STATUS swapf WREG_TEMP,F ;prepare WREG to be restored swapf WREG_TEMP,W ;restore WREG without affecting STATUS retfie ;return from interrupt ;---------------------------------------------------------------------------- Main: ;EPROM ADDRESS 0x00 must be set to the address ID you want the device to ;respond to ;initialise the SCADA protocol SCADA_INIT mainloop: clrwdt goto mainloop ;---------------------------------------------------------------------------- END