您当前的位置:五五电子网电子知识单片机-工控设备PIC单片机PIC的I2C读写程序 正文
PIC的I2C读写程序

PIC的I2C读写程序

点击数:7488 次   录入时间:03-04 11:36:41   整理:http://www.55dianzi.com   PIC单片机
;-----------------------------------------------------------------------------
;  input  :     W-reg   = error code
;  output :     ERCODE  = error code
;           FLAG(ERR1) = 1
;
;         code          error status mode
;       -------         ------------------------------------------------------
;           1   :       SCL locked low by device (bus is still busy)
;           2   :       SDA locked low by device (bus is still busy)
;           3   :       No acknowLEDge from device (no handshake)
;           4   :       SDA bus not released for master to generate STOP bit
;-----------------------------------------------------------------------------
;
;Subroutine to identify the status of the serial clock (SCL) and serial data
;(SDA) condition aCCording to the error status table. Codes generated are
;useful for bus/device diagnosis.
;
ERR
BTFSS   FLAG,ERR1       ; Remain as first error encountered
MOVWF   ERCODE          ; Save error code
BSF     FLAG,ERR1       ; Set error flag
RETLW   0
;
;-----------------------------------------------------------------------------
;       START bus communication routine
;-----------------------------------------------------------------------------
;       input   : none
;       output  : initialize bus communication
;-----------------------------------------------------------------------------
;
;Generate START bit (SCL is high while SDA goes from high to low transition)
;and check status of the serial clock.
BSTART
MOVLW   B'00111111'     ; Put SCL, SDA line in output state
TRIS    RB
;***************************************************************************
bsf     RB,SDA          ;make sure sda is high
;***************************************************************************
BSF     RB,SCL          ; Set clock high
MOVLW   1               ; Ready error status code 1
BTFSS   RB,SCL          ; Locked?
CALL    ERR             ; SCL locked low by device
BCF     RB,SDA          ; SDA goes low during SCL high
NOP                     ; Timing adjustment
NOP
NOP
BCF     RB,SCL          ; Start clock train
RETLW   0
;
;END SUB
 PAGE
;
;-----------------------------------------------------------------------------
;       STOP bus communication routine
;-----------------------------------------------------------------------------
;       Input   :       None
;       Output  :       Bus communication, STOP condition
;-----------------------------------------------------------------------------
;
;Generate STOP bit (SDA goes from low to high during SCL high state)
;and check bus conditions.
;
BSTOP
;****************************************************************************
MOVLW   B'00111111'     ; Put SCL, SDA line in output state
TRIS    RB
;****************************************************************************
BCF     RB,SDA          ; Return SDA to low
BSF     RB,SCL          ; Set SCL high
nop
nop
nop
MOVLW   1               ; Ready error code 1
BTFSS   RB,SCL          ; High?
CALL    ERR             ; No, SCL locked low by device
BSF     RB,SDA          ; SDA goes from low to high during SCL high
MOVLW   4               ; Ready error code 4
BTFSS   RB,SDA          ; High?
CALL    ERR             ; No, SDA bus not release for STOP
RETLW   0
;
;END SUB
;
;-----------------------------------------------------------------------------
;       Serial data send from PIC to serial EEPROM, bit-by-bit subroutine
;-----------------------------------------------------------------------------
;       Input   :       None
;       Output  :       To (DI) of serial EEPROM device

上一页  [1] [2] [3] [4] [5]  下一页


本文关键字:程序  PIC单片机单片机-工控设备 - PIC单片机