📄 i2cmem.asm
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; I2CMEM.ASM MPB 7-1-06
;
; Test program for 24AA128 I2C 16k byte serial
; memory with P16F877A (4MHz XT)
; Demonstrates single byte write and read
; with 10-bit address.
;
; Write data from 0x20
; High address 0x21
; Low address 0x22
; Read data back to 0x23
;
; Version: Final
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PROCESSOR 16F877A
__CONFIG 3FF1
INCLUDE "P16F877A.INC"
; Data, address & control registers ;;;;;;;;;;;;;;;;;;;;
SenReg EQU 0x20 ; Send data store
HiReg EQU 0x21 ; High address store
LoReg EQU 0x22 ; Low address store
RecReg EQU 0x23 ; Receive data store
ConReg EQU 0x24 ; Control byte store
Temp EQU 0x25 ; Scratchpad location
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ORG 0 ; Program start address
CLRF SenReg ; Zeroise data
CLRF HiReg ; Zeroise high address
CLRF LoReg ; Zeroise low address
GOTO begin ; jump to main program
;--------------------------------------------------------
; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Wait for interrupt flag SSPIF for send/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
; Send a byte ....................................
send NOP ; Select..
BANKSEL SSPBUF ; .. bank
MOVWF SSPBUF ; Send address/data
CALL wint ; Wait until sent
RETURN ; done
;--------------------------------------------------------
; Routines to send 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 ; SEND CONTROL BYTE
CALL send ; Memory ID & chip address
RETURN ; done
;........................................................
senadd NOP
BANKSEL SSPCON ; SEND ADDRESS BYTES
MOVF HiReg,W ; load address high byte
CALL send ; and send
MOVF LoReg,W ; load address low byte
CALL send ; and send
RETURN
;........................................................
sendat MOVF SenReg,W ; Load data
CALL send ; and send
RETURN ; done
;........................................................
senstop NOP
BANKSEL SSPCON2 ; GENERATE STOP BIT
BSF SSPCON2,PEN ; Generate stop bit
CALL wint ; wait till done
RETURN ; done
;........................................................
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 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....
;--------------------------------------------------------
; Initialisation sequence ..............................
init 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'00101000' ;
MOVWF SSPCON ; Set mode & enable
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
;--------------------------------------------------------
; Write a test byte to given address ................
writeb MOVLW 0xA0 ; Control byte for WRITE
MOVWF ConReg ;
CALL sencon ; Send control byte
CALL senadd ; Send address bytes
CALL sendat ; Send data byte
CALL senstop ; Send stop bit
CALL wait ; Wait 10ms for write
RETURN
;--------------------------------------------------------
; Read the byte from given address ...................
readb MOVLW 0xA0 ; Control byte to WRITE
MOVWF ConReg ; address to memory
CALL sencon ; Send control byte
CALL senadd ; Send address bytes
CALL senstop ; Stop
MOVLW 0xA1 ; Control byte to READ
MOVWF ConReg ; data from memory
CALL sencon ; Send control byte
BANKSEL SSPCON2
BSF SSPCON2,RCEN ; Enable receive mode
war BTFSS SSPSTAT,BF ; Check ...
GOTO war ; for read done
CALL senack ; send NOT acknowledge
CALL senstop ; send stop bit
MOVF SSPBUF,W ; Read receive buffer
MOVWF RecReg ; and store it
RETURN
; MAIN PROGRAM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
begin CALL init ; Initialise for I2C
next CALL writeb ; write the test byte
CALL readb ; and read it back
INCF SenReg ; next data
INCF LoReg ; next location
BTFSS STATUS,Z ; end of memory block?
GOTO next ; no, next location
INCF HiReg ; next block
MOVF HiReg,W ; copy high address byte
MOVWF Temp ; store it
MOVLW 0x40 ; Last block = 3F
SUBWF Temp ; Compare
BTFSS STATUS,Z ; Finish if block = 40xx
GOTO next ; next memory block..
SLEEP ; .. unless done
END ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -