i2cmem.sdi
来自「代码保护功能处于持续发展中。Microchip 承诺将不断改进产品的代码保护功能」· SDI 代码 · 共 204 行
SDI
204 行
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,;
,,,; 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
00002007,3FF1,, __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
,,,
00000000,01A0,, CLRF SenReg ; Zeroise data
00000001,01A1,, CLRF HiReg ; Zeroise high address
00000002,01A2,, CLRF LoReg ; Zeroise low address
00000003,2872,, GOTO begin ; jump to main program
,,,
,,,;--------------------------------------------------------
,,,; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,,; Wait for interrupt flag SSPIF for send/recive done ...
,,,
00000004,0000,wint,wint NOP ; BANKSEL has no address
00000005,1283 1303,, BANKSEL PIR1 ; Select bank
00000007,118C,, BCF PIR1,SSPIF ; reset interrupt flag
00000008,0000,win,win NOP
00000009,1D8C,, BTFSS PIR1,SSPIF ; wait for..
0000000A,2808,, GOTO win ; ..transmit done
0000000B,0008,, RETURN ; done
,,,
,,,
,,,; Send a byte ....................................
,,,
0000000C,0000,send,send NOP ; Select..
0000000D,1283 1303,, BANKSEL SSPBUF ; .. bank
0000000F,0093,, MOVWF SSPBUF ; Send address/data
00000010,2004,, CALL wint ; Wait until sent
00000011,0008,, RETURN ; done
,,,
,,,;--------------------------------------------------------
,,,; Routines to send start, control, address, data, stop ..
,,,;........................................................
,,,
00000012,0000,sencon,sencon NOP ; GENERATE START BIT
00000013,1283 1303,, BANKSEL PIR1
00000015,118C,, BCF PIR1,SSPIF ; Clear interrupt flag
00000016,1683 1303,, BANKSEL SSPCON2 ; select register page
00000018,1711,, BSF SSPCON2,ACKSTAT ; Set acknowledge flag
00000019,1411,, BSF SSPCON2,SEN ; Generate start bit
0000001A,2004,, CALL wint ; wait till done
0000001B,0824,, MOVF ConReg,W ; SEND CONTROL BYTE
0000001C,200C,, CALL send ; Memory ID & chip address
0000001D,0008,, RETURN ; done
,,,
,,,;........................................................
,,,
0000001E,0000,senadd,senadd NOP
0000001F,1283 1303,, BANKSEL SSPCON ; SEND ADDRESS BYTES
00000021,0821,, MOVF HiReg,W ; load address high byte
00000022,200C,, CALL send ; and send
00000023,0822,, MOVF LoReg,W ; load address low byte
00000024,200C,, CALL send ; and send
00000025,0008,, RETURN
,,,
,,,;........................................................
,,,
00000026,0820,sendat,sendat MOVF SenReg,W ; Load data
00000027,200C,, CALL send ; and send
00000028,0008,, RETURN ; done
,,,
,,,;........................................................
,,,
00000029,0000,senstop,senstop NOP
0000002A,1683 1303,, BANKSEL SSPCON2 ; GENERATE STOP BIT
0000002C,1511,, BSF SSPCON2,PEN ; Generate stop bit
0000002D,2004,, CALL wint ; wait till done
0000002E,0008,, RETURN ; done
,,,
,,,;........................................................
,,,
0000002F,0000,senack,senack NOP
00000030,1683 1303,, BANKSEL SSPCON2
00000032,1691,, BSF SSPCON2,ACKDT ; Set ack. bit high
00000033,1611,, BSF SSPCON2,ACKEN ; Initiate ack.sequence
00000034,2004,, CALL wint ; Wait for ack. done
00000035,0008,, RETURN ; done
,,,
,,,;........................................................
,,,
00000036,0000,wait,wait NOP
00000037,1283 1303,, BANKSEL TMR0 ; WAIT FOR WRITE DONE
00000039,309C,, MOVLW d'156' ; Set starting value
0000003A,0081,, MOVWF TMR0 ; and load into timer
0000003B,1283 1303,, BANKSEL INTCON ; 64 x 156us = 10ms
0000003D,110B,, BCF INTCON,T0IF ; Reset timer out flag
0000003E,1D0B,wem,wem BTFSS INTCON,T0IF ; Wait 10ms
0000003F,283E,, GOTO wem ; for timeout
00000040,1283 1303,, BANKSEL TMR0 ; default bank
00000042,0008,, RETURN ; Byte write done....
,,,
,,,;--------------------------------------------------------
,,,; Initialisation sequence ..............................
,,,
00000043,0000,init,init NOP
00000044,1683 1303,, BANKSEL SSPCON2 ;
00000046,3060,, MOVLW b'01100000' ; Set ACKSTAT,ACKDT bits
00000047,0091,, MOVWF SSPCON2 ; Reset SEN,ACK bits
00000048,3080,, MOVLW b'10000000' ;
00000049,0094,, MOVWF SSPSTAT ; Speed & signal levels
0000004A,3013,, MOVLW 0x13 ; Clock = 50kHz
0000004B,0093,, MOVWF SSPADD ; Load baud rate count-1
0000004C,1283 1303,, BANKSEL SSPCON ;
0000004E,3028,, MOVLW b'00101000' ;
0000004F,0094,, MOVWF SSPCON ; Set mode & enable
00000050,118C,, BCF PIR1,SSPIF ; clear interrupt flag
,,,
,,,; Initialise TIMER0 for write delay ...............
,,,
00000051,1683 1303,, BANKSEL OPTION_REG ;
00000053,30C5,, MOVLW B'11000101' ; TIMER0 setup code
00000054,0081,, MOVWF OPTION_REG ; Internal clock,1/64
00000055,1283 1303,, BANKSEL TMR0
00000057,0008,, RETURN
,,,
,,,;--------------------------------------------------------
,,,; Write a test byte to given address ................
,,,
00000058,30A0,writeb,writeb MOVLW 0xA0 ; Control byte for WRITE
00000059,00A4,, MOVWF ConReg ;
0000005A,2012,, CALL sencon ; Send control byte
0000005B,201E,, CALL senadd ; Send address bytes
0000005C,2026,, CALL sendat ; Send data byte
0000005D,2029,, CALL senstop ; Send stop bit
0000005E,2036,, CALL wait ; Wait 10ms for write
0000005F,0008,, RETURN
,,,
,,,;--------------------------------------------------------
,,,; Read the byte from given address ...................
,,,
00000060,30A0,readb,readb MOVLW 0xA0 ; Control byte to WRITE
00000061,00A4,, MOVWF ConReg ; address to memory
00000062,2012,, CALL sencon ; Send control byte
00000063,201E,, CALL senadd ; Send address bytes
00000064,2029,, CALL senstop ; Stop
,,,
00000065,30A1,, MOVLW 0xA1 ; Control byte to READ
00000066,00A4,, MOVWF ConReg ; data from memory
00000067,2012,, CALL sencon ; Send control byte
00000068,1683 1303,, BANKSEL SSPCON2
0000006A,1591,, BSF SSPCON2,RCEN ; Enable receive mode
0000006B,1C14,war,war BTFSS SSPSTAT,BF ; Check ...
0000006C,286B,, GOTO war ; for read done
0000006D,202F,, CALL senack ; send NOT acknowledge
0000006E,2029,, CALL senstop ; send stop bit
,,,
0000006F,0813,, MOVF SSPBUF,W ; Read receive buffer
00000070,00A3,, MOVWF RecReg ; and store it
00000071,0008,, RETURN
,,,
,,,
,,,; MAIN PROGRAM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
00000072,2043,begin,begin CALL init ; Initialise for I2C
00000073,2058,next,next CALL writeb ; write the test byte
00000074,2060,, CALL readb ; and read it back
00000075,0AA0,, INCF SenReg ; next data
00000076,0AA2,, INCF LoReg ; next location
00000077,1D03,, BTFSS STATUS,Z ; end of memory block?
00000078,2873,, GOTO next ; no, next location
00000079,0AA1,, INCF HiReg ; next block
,,,
0000007A,0821,, MOVF HiReg,W ; copy high address byte
0000007B,00A5,, MOVWF Temp ; store it
0000007C,3040,, MOVLW 0x40 ; Last block = 3F
0000007D,02A5,, SUBWF Temp ; Compare
0000007E,1D03,, BTFSS STATUS,Z ; Finish if block = 40xx
0000007F,2873,, GOTO next ; next memory block..
00000080,0063,, SLEEP ; .. unless done
,,,
,,, END ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?