Code:
processor pic16f877a
include <p16f877a.inc>
__config _xt_osc & _wdt_off & _cp_off & _boden_off & _lvp_off
;***********************************************************************
#define read porte,0
#define write porte,1
#define cs porte,2
dataout equ 0x20 ;chua du lieu can doc boi master
w_save equ 0x21
pc_lath equ 0x22
status_save equ 0x23
fsr_save equ 0x24
;*********************chuong trinh ngat
org 0x0004
goto isr
;*********************
isr
;luu cac gia tri thanh ghi giong tac vu push
movwf w_save
swapf status,w
movwf status_save
movf pclath,w
movwf pc_lath
clrf pclath
movf fsr,w
movwf fsr_save
btfss pir1,pspif ;kiem tra co ngat cua psp
goto exit ;bang 0 thi thoat
bcf pir1,pspif ;bang 1 thi xoa co ngat nay
banksel trise ;quay ve thanh ghi trise
btfsc trise,obf ;kiem tra du lieu da duoc doc chua?
Goto exit ;neu chua doc thi thoat ngat
bcf status,rp0
movf dataout,w ;chuyen du lieu tra portd
movwf portd
exit ;phuc hoi gia tri ban dau cua cac thanh ghi
bcf status,rp0
movf fsr_save,w
movwf fsr
movf pc_lath,w
movwf pclath
swapf status_save,w
movwf status
swapf w_save,f ;swap 2 lan de khong thay doi cac co trong thanh ghi status
swapf w_save,w
retfie
;*************************************************************************************
;chuong trinh chinh
org 0x0000
clrf status
movlw 0x00
movwf pclath
goto main
;*************************************************************************************
main
org 0x050
banksel portd
clrf portd
banksel trise
movlw b'00010111'
movwf trise ;3 chan input,cho phep psp
movlw 0x06
movwf adcon1 ;cac chan vao la digital
;***khoi tao ngat psp
bsf pie1,pspie
banksel intcon
bsf intcon,gie ;cho phep ngat toan cuc
bsf intcon,peie ;cho phep ngat psp
;khoi tao gia tri can doc boi master
movlw b'01010101'
movwf dataout
movwf portd
;**************************************************************************************
goto $
end
đoạn code này dùng cho con slave trong chế độ psp