| PIC单片机的I2C应用程序1 ; Functionality: ; ; This code implements the basic functions for an I2C slave device ; using the SSP module. All I2C functions are handled in an ISR. ; Bytes written to the slave are stored in a buffer. After a number ; of bytes have been written, the master device can then read the ; bytes back from the buffer. ; ; Variables and Constants used in the program: ; ; The start address for the receive buffer is stored in the variable ; 'RXBuffer'. The length of the buffer is denoted by the constant ; value 'RX_BUF_LEN'. The current buffer index is stored in the ; variable 'Index'. ; ;-------------------------------------------------------------------- ; ; The following files should be included in the MPLAB project: ; ; an734.asm-- Main source code file ; ; 16f872.lkr-- Linker s cript file ; (change this file for the device ; you are using) ;--------------------------------------------------------------------- #include <p16f872.inc> ; Change to device that you are using. ;--------------------------------------------------------------------- ;Constant Definitions ;--------------------------------------------------------------------- #define NODE_ADDR 0x02 ; I2C address of this node ; Change this value to address that ; you wish to use. ;--------------------------------------------------------------------- ; Buffer Length Definition ;--------------------------------------------------------------------- #define RX_BUF_LEN 32 ; Length of receive buffer ;--------------------------------------------------------------------- ; Variable declarations ;--------------------------------------------------------------------- udata WREGsave res 1 STATUSsave res 1 FSRsave res 1 PCLATHsave res 1 Index res 1 ; Index to receive buffer Temp res 1 ; RXBuffer res RX_BUF_LEN ; Holds rec'd bytes from master ; device.
;--------------------------------------------------------------------- ; Vectors ;--------------------------------------------------------------------- STARTUP code nop goto Startup ; nop ; 0x0002 nop ; 0x0003 goto ISR ; 0x0004 PROG code ;--------------------------------------------------------------------- ; Macros ;--------------------------------------------------------------------- memset macro Buf_addr,Value,Length movlw Length ; This macro loads a range of data memory movwf Temp ; with a specified value. The starting movlw Buf_addr ; address and number of bytes are also movwf FSR ; specified. SetNext movlw Value movwf INDF incf FSR,F decfsz Temp,F goto SetNext endm LFSR macro Address,Offset ; This macro loads the correct value movlw Address ; into the FSR given an initial data movwf FSR ; memory address and offset value. movf Offset,W addwf FSR,F endm
;--------------------------------------------------------------------- ; Main Code ;--------------------------------------------------------------------- Startup bcf STATUS,RP1 bsf STATUS,RP0 call Setup Main clrwdt ; Clear the WDT goto Main ; Loop forever. ;--------------------------------------------------------------------- ; Interrupt Code
本新闻共 3页,当前在第 1页 [ 1] [ 2] [ 3] |