Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
PIC16F877A has a hardware IIC interface,you shoule set relative register in order,or you use bleow codework in 4MHZ)
;
;************************************************************************* ;PAGE3 (600-6FF)
;
;PAGE3 (600-6FF)
; ;PAGE3 (600-6FF)
;***************************************************************************** ;PAGE3 (600-6FF)
;** Two wire/I2C Bus READ/WRITE Routines of Microchip's ;PAGE3 (600-6FF)
;** 24LCXX / 85Cxx serial CMOS EEPROM interfacing to a ;PAGE3 (600-6FF)
;** PIC16C57 8-bit CMOS single chip microcomputer ;PAGE3 (600-6FF)
;** Revsied Version 2.0
;** ;PAGE3 (600-6FF)
;** Part use = PIC16F57
;** Note: 1) All timings are based on a reference crystal frequency of 4MHz ;PAGE3 (600-6FF)
;** which is equivalent to an instruction cycle time of 1 usec. ;PAGE3 (600-6FF)
;** 2) Address and literal values are read in octal unless otherwise ;PAGE3 (600-6FF)
;** specified.
;***************************************************************************** ;PAGE3 (600-6FF)
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Two wire/I2C - CPU communication error status table and subroutine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; input : W-reg = error code ;PAGE3 (600-6FF)
; output : ERCODE = error code ;PAGE3 (600-6FF)
; FLAG(ERROR) = 1 ;PAGE3 (600-6FF)
;
; code error status mode ;PAGE3 (600-6FF)
; ------- ------------------------------------------------------ ;PAGE3 (600-6FF)
; 1 : 3 locked low by device (bus is still busy) ;PAGE3 (600-6FF)
; 2 : 4 locked low by device (bus is still busy) ;PAGE3 (600-6FF)
; 3 : No acknowledge from device (no handshake) ;PAGE3 (600-6FF)
; 4 : 4 bus not released for master to generate STOP bit ;PAGE3 (600-6FF)
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
;Subroutine to identify the status of the serial clock (3) and serial data ;PAGE3 (600-6FF)
;(4) condition according to the error status table. Codes generated are ;PAGE3 (600-6FF)
;useful for bus/device diagnosis.
;
;
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; START bus communication routine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; input : none
; output : initialize bus communication
; Time Used: MMAX 23 Cycle.
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
;Generate START bit (3 is high while 4 goes from high to low transition) ;PAGE3 (600-6FF)
;and check status of the serial clock.
BSTART
MOVLW B'00000001' ; Put 1,2 line in output state ;PAGE3 (600-6FF)
TRIS PORTB ;PAGE3 (600-6FF)
;***************************************************************************
BSF IIC_SDA ;Make data line is high level
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
BCF IIC_SCL ;Make clock line is low level
;***************************************************************************
NOP
BSF IIC_SCL ; Make clock line is 1
; NOP
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
BCF IIC_SDA ; make data lin got 0 from 1 when clock=1
BSF IIC_SDA ; Make data line 1 from 0 when clock line = 1
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
BCF IIC_SCL ; Make clock line low
NOP
RETLW .0 ;PAGE3 (600-6FF)
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Serial data send from PIC to serial EEPROM, bit-by-bit subroutine ;PAGE3 (600-6FF)
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Input : None
; Output : To (DI) of serial EEPROM device ;PAGE3 (600-6FF)
; Time Used: MAX 17 Cycle.
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
BITIN
BSF EEPROM,DI ; In case of input data = 1
MOVLW B'10000001' ; Force SDA line as input ;PAGE3 (600-6FF)
TRIS PORTB ;PAGE3 (600-6FF)
;
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Serial data receive from serial EEPROM to PIC, bit-by-bit subroutine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Input : EEPROM file
; Output : From (DO) of serial EEPROM device to PIC
; Time Used: MAX 13 Cycle.
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
BITOUT
MOVLW B'00000001' ; Put 1,2 line in output state ;PAGE3 (600-6FF)
TRIS PORTB ;PAGE3 (600-6FF)
BTFSS EEPROM,DO ; Check the status of sending data ;PAGE3 (600-6FF)
GOTO BIT0 ; =0 ;PAGE3 (600-6FF)
BSF IIC_SDA ; =1 ;PAGE3 (600-6FF)
GOTO CLKOUT
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.