pic16f877a i2c asm code for 24c256 e2prom

Status
Not open for further replies.

tejas700

Newbie level 1
Joined
Mar 23, 2009
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,285
i2c .asm


if any one have sources for that then send me link
 

24c256

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

; NOP
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)

BCF IIC_SCL ; Start clock train ;PAGE3 (600-6FF)

RETLW .0 ;PAGE3 (600-6FF)

;
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; STOP bus communication routine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Input : None
; Output : Bus communication, STOP condition
; Time Used: MMAX 18 Cycle.
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
;Generate STOP bit (4 goes from low to high during 3 high state) ;PAGE3 (600-6FF)
;and check bus conditions. ;PAGE3 (600-6FF)
;
BSTOP

BCF IIC_SDA ; Make data line=0 ;PAGE3 (600-6FF)

MOVLW B'00000001' ; Put 1,2 line in output state ;PAGE3 (600-6FF)
TRIS PORTB ;PAGE3 (600-6FF)

BCF IIC_SDA ; Make data lint = 0 again
NOP ;PAGE3 (600-6FF)
; NOP ;PAGE3 (600-6FF)
NOP

BSF IIC_SCL ; Set clock = 1
NOP ;PAGE3 (600-6FF)
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)

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)

; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)

BSF IIC_SDA ;PAGE3 (600-6FF)
BSF IIC_SCL ; Clock high
NOP ;PAGE3 (600-6FF)
; NOP ;PAGE3 (600-6FF)
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
BTFSS IIC_SDA ;PAGE3 (600-6FF)
BCF EEPROM,DI ;PAGE3 (600-6FF)
BCF IIC_SCL ;PAGE3 (600-6FF)

RETLW .0 ;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

BIT0

BCF IIC_SDA ; Output bit 0 ;PAGE3 (600-6FF)

CLKOUT
BSF IIC_SCL ;PAGE3 (600-6FF)
; NOP
; NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
NOP ;PAGE3 (600-6FF)
BCF IIC_SCL ;PAGE3 (600-6FF)


RETLW .0 ;PAGE3 (600-6FF)


;
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; SEND DATA subroutine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Input : TXBUF
; Output : Data X'mitted to EEPROM device
; Time Used: MMAX 201 Cycle. ;PAGE3 (600-6FF)
;========================================================================= ;PAGE3 (600-6FF)
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
TX
MOVLW .8 ; 8 bits of data ;PAGE3 (600-6FF)
MOVWF COUNT ;PAGE3 (600-6FF)
;
TXLP

BCF EEPROM,DO ; in case of the data = 0 ;PAGE3 (600-6FF)
BTFSC TXBUF,7 ; yes? ;PAGE3 (600-6FF)
BSF EEPROM,DO ; no,so data = 1 ;PAGE3 (600-6FF)


CALL BITOUT ; send data ;PAGE3 (600-6FF)
RLF TXBUF,1 ; shift data ;PAGE3 (600-6FF)
DECFSZ COUNT,1 ; 8 bit data send over? ;PAGE3 (600-6FF)

GOTO TXLP ; No.


CALL BITIN ; yes, read confirm bit ;PAGE3 (600-6FF)

BTFSC EEPROM,DI ; check confirm bit ;PAGE3 (600-6FF)
BCF IIC_ACK ; send data failed ;PAGE3 (600-6FF)

RETLW .0 ;PAGE3 (600-6FF)


;
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; RECEIVE DATA subroutine
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
; Input : None
; Output : RXBUF = Receive 8-bit data
; Time Used: MMAX 222 Cycle.
;----------------------------------------------------------------------------- ;PAGE3 (600-6FF)
;
RX
MOVLW .8 ; 8 bits of data ;PAGE3 (600-6FF)
MOVWF COUNT ;PAGE3 (600-6FF)
CLRF RXBUF
BCF STATUS,C ;PAGE3 (600-6FF)
;
RXLP
RLF RXBUF,1 ; Shift data to buffer ;PAGE3 (600-6FF)
CALL BITIN ;PAGE3 (600-6FF)
BTFSC EEPROM,DI ;PAGE3 (600-6FF)
BSF RXBUF,0 ; Input bit =1 ;PAGE3 (600-6FF)
DECFSZ COUNT,1 ; 8 bits? ;PAGE3 (600-6FF)
GOTO RXLP ;PAGE3 (600-6FF)
BSF EEPROM,DO ; Set acknowledge bit = 1 ;PAGE3 (600-6FF)

CALL BITOUT ; to STOP further input ;PAGE3 (600-6FF)

RETLW .0 ;PAGE3 (600-6FF)
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…