📄 sermem.inc
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SERMEM.INC MPB 14-1-06
;
; 24XX128 Serial Memory Driver
; Include file for demo system
;
; Version: under development
;
; TO WRITE A BYTE
; Load data into SenReg
; Load address into Hireg,Loreg
; Call writmem
;
; TO READ A BYTE
; Load address into Hireg,Loreg
; Call readmem
; Read data from RecReg
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Data, address & control registers ;;;;;;;;;;;;;;;;;;;;;
SenReg EQU 0x60 ; writem data store
HiReg EQU 0x61 ; High address store
LoReg EQU 0x62 ; Low address store
RecReg EQU 0x63 ; Receive data store
ConReg EQU 0x64 ; Control byte store
; Initialisation sequence ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
inimem NOP ;
BANKSEL SSPCON2 ;
MOVLW b'01100000' ; Set ACKSTAT,ACKDT bits
MOVWF SSPCON2 ; Reset SEN,ACK bits
MOVLW b'10000000' ;
MOVWF SSPSTAT ; Speed & signal levels
MOVLW 0x13 ; Clock = 50kHz
MOVWF SSPADD ; Load baud rate count-1
BANKSEL SSPCON ;
MOVLW b'00001000' ;
MOVWF SSPCON ; Set mode & disable
BCF PIR1,SSPIF ; clear interrupt flag
; Initialise TIMER0 for write delay ---------------------
BANKSEL OPTION_REG ;
MOVLW B'11000101' ; TIMER0 setup code
MOVWF OPTION_REG ; Internal clock,1/64
BANKSEL TMR0
RETURN
; MAIN ROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Write a test byte to given address ;;;;;;;;;;;;;;;;;;;;
writmem
NOP ; Setup
BANKSEL TRISC ; port
MOVLW b'11111111' ; for
MOVWF TRISC ; SSP
BANKSEL PORTC ; operation
BSF SSPCON,SSPEN ; Enable serial port
MOVLW 0xA0 ; Control byte for WRITE
MOVWF ConReg ; Load it
CALL sencon ; write control byte
CALL senadd ; write address bytes
CALL sendat ; write data byte
CALL senstop ; write stop bit
CALL waitm ; Wait 10ms for write
BCF SSPCON,SSPEN ; Disable serial port
RETURN
; Read the byte from given address ;;;;;;;;;;;;;;;;;;;;;;
readmem
NOP ; Setup
BANKSEL TRISC ; port
MOVLW b'11111111' ; for
MOVWF TRISC ; SSP
BANKSEL PORTC ; operation
BSF SSPCON,SSPEN ; Enable serial port
MOVLW 0xA0 ; Control byte to WRITE
MOVWF ConReg ; address to memory
CALL sencon ; write control byte
CALL senadd ; write address bytes
CALL senstop ; Stop
MOVLW 0xA1 ; Control byte to READ
MOVWF ConReg ; data from memory
CALL sencon ; write control byte
BANKSEL SSPCON2
BSF SSPCON2,RCEN ; Enable receive mode
war BTFSS SSPSTAT,BF ; Check ...
GOTO war ; for read done
CALL senack ; write NOT acknowledge
CALL senstop ; write stop bit
MOVF SSPBUF,W ; Read receive buffer
MOVWF RecReg ; and store it
BCF SSPCON,SSPEN ; Disable serial port
RETURN
; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Load buffer for write to memory -----------------------
writem NOP ; Select..
BANKSEL SSPBUF ; .. bank
MOVWF SSPBUF ; write address/data
CALL wint ; Wait until sent
RETURN ; done
; Routines to write, start, control, address, data, stop
sencon NOP ; GENERATE START BIT
BANKSEL PIR1
BCF PIR1,SSPIF ; Clear interrupt flag
BANKSEL SSPCON2 ; select register page
BSF SSPCON2,ACKSTAT ; Set acknowledge flag
BSF SSPCON2,SEN ; Generate start bit
CALL wint ; wait till done
MOVF ConReg,W ; write CONTROL BYTE
CALL writem ; Memory ID & address
RETURN ; done
; Write address to memory -------------------------------
senadd NOP
BANKSEL SSPCON ; writem ADDRESS BYTES
MOVF HiReg,W ; load address high byte
CALL writem ; and write
MOVF LoReg,W ; load address low byte
CALL writem ; and write
RETURN
; Write data to memeory ---------------------------------
sendat MOVF SenReg,W ; Load data
CALL writem ; and write
RETURN ; done
; Genearte stop bit -------------------------------------
senstop NOP
BANKSEL SSPCON2 ; GENERATE STOP BIT
BSF SSPCON2,PEN ; Generate stop bit
CALL wint ; wait till done
RETURN ; done
; Generate acknowledge ----------------------------------
senack NOP
BANKSEL SSPCON2
BSF SSPCON2,ACKDT ; Set ack. bit high
BSF SSPCON2,ACKEN ; Initiate ack.sequence
CALL wint ; Wait for ack. done
RETURN ; done
; Wait for write to finish ------------------------------
waitm NOP
BANKSEL TMR0 ; WAIT FOR WRITE DONE
MOVLW d'156' ; Set starting value
MOVWF TMR0 ; and load into timer
BANKSEL INTCON ; 64 x 156us = 10ms
BCF INTCON,T0IF ; Reset timer out flag
wem BTFSS INTCON,T0IF ; Wait 10ms
GOTO wem ; for timeout
BANKSEL TMR0 ; default bank
RETURN ; Byte write done....
; Wait for interrupt flag SSPIF for writem/recive done --
wint NOP ; BANKSEL has no address
BANKSEL PIR1 ; Select bank
BCF PIR1,SSPIF ; reset interrupt flag
win NOP
BTFSS PIR1,SSPIF ; wait for..
GOTO win ; ..transmit done
RETURN ; done
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -