Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/08/00 11:26
Read: times


 
#1720 - RE: I2C Control
in case you do,t have a hardware IIC-block


I2Cvars SEGMENT data
RSEG I2Cvars

BITCNT: DS 1 ; bit counter for I2C routines
BYTECOUNT: DS 1 ; byte counter for I2C routines
SLAVEADD: DS 1 ; slave address for I2C routines

I2Cvars2 SEGMENT idata
RSEG I2Cvars2

I2C_sendbuffer: DS 18
I2C_receivebuffer: DS 17


I2Cbits SEGMENT BIT
RSEG I2Cbits

NOACK: DBIT 1 ; I2C no acknowledge flag
BUSY: DBIT 1 ; I2C busy flag
ERROR: DBIT 1 ; I2C error flag


RSEG IIC



iic_routines:
;----------------------------------------------------------------------
; DELAY: Create a delay for the main signals ( SCLOCK , SDATA )
;----------------------------------------------------------------------

wait4uS:NOP
RET

;----------------------------------------------------------------------
; SENDSTOP: Send the bit stop of the transmission
;----------------------------------------------------------------------

SENDSTOP:

SETB MDE ; to enable SDATA pin as an output
CLR MDO ; get SDATA ready for stop
SETB MCO ; set clock for stop
CALL wait4uS
SETB MDO ; this is the stop bit
CLR BUSY ; bus should be released
%ADuC_SHIT
RET

;----------------------------------------------------------------------
; SENDBYTE: Send a 8-bits word to the slave
;----------------------------------------------------------------------

SENDBYTE:

MOV BITCNT,#8 ; 8 bits in a byte

SETB MDE ; to enable SDATA pin as an output
CLR MDO
CLR MCO
LOOP: RLC A ; send one bit
MOV MDO,C ; put data bit on pin
SETB MCO ; send clock
CLR MCO ; clock is off
DJNZ BITCNT,LOOP

CLR MDE ; release data line for acknowledge
SETB MCO ; send clock for acknowledge
JNB MDI,NEXT ; this is a check
SETB NOACK ; no acknowledge
NEXT: CLR MCO ; clock for acknowledge
RET


;----------------------------------------------------------------------
; BITSTART: Send the bit start of the transmission and the slave
; address to the slave
;----------------------------------------------------------------------

BITSTART:

SETB BUSY ; I2C is in progress
SETB MDE ; to enable SDATA pin as an output
CLR NOACK
CLR ERROR
JNB MCO,FAULT
JNB MDO,FAULT
CLR MDO ; this is
CALL wait4uS ; the
CLR MCO ; start bit
FAULT: MOV A,SLAVEADD ; Get slave address
CALL SENDBYTE ; call routine to send slave addr. byte
RET

;----------------------------------------------------------------------
; SENDATA: Send all the sequence to the slave ( slave address + data )
;----------------------------------------------------------------------

SENDATA:

CALL BITSTART
JB MDI,NEXT1
SLOOP: MOV A,@R0
CALL SENDBYTE
INC R0
JB NOACK,NEXT1
DJNZ BYTECOUNT,SLOOP

NEXT1: CALL SENDSTOP
MOV C,NOACK
ORL C,BUSY
ORL C,ERROR
JNC RETOUR
CLR I2CRS
RETOUR: %ADUC_SHIT
RET

;----------------------------------------------------------------------
; RCVBYTE: receives one byte of data from an I2C slave device.
;----------------------------------------------------------------------

RCVBYTE:

MOV BITCNT,#8 ;Set bit count.

CLR MDE ;Data pin of the master is now an input
CLR MCO
LOOP2: SETB MCO
CLR MCO
MOV C,MDI ;Get data bit from pin.
RLC A ;Rotate bit into result byte.
DJNZ BITCNT,LOOP2 ;Repeat until all bits received.
;result byte is in the accumulator
PUSH ACC ;Save result byte in the stack
SETB MDE ;Data pin of the master must be an..
;..output for the acknowledge
MOV A,BYTECOUNT
CJNE A,#1,SACK ;Check for last byte of frame.
SETB MDO ;Send no acknowledge on last byte.
JMP NACK

SACK: CLR MDO ;Send acknowledge bit.

NACK: SETB MCO ;Send acknowledge clock.
POP ACC ;Restore accumulator
CALL wait4uS
CLR MCO
SETB MDO ;Clear acknowledge bit.
CALL wait4uS
CLR MDE
RET

;----------------------------------------------------------------------
; RCVDATA: receives one or more bytes of data from an I2C slave device.
;----------------------------------------------------------------------

RCVDATA: INC SLAVEADD ;Set for READ of slave.
CALL BITSTART ;Acquire bus and send slave address.
JB NoAck,RDEX ;Check for slave not responding.

RDLoop: CALL RCVBYTE ;Receive next data byte.
MOV @R0,A ;Save data byte in buffer.
INC R0 ;Advance buffer pointer.
DJNZ BYTECOUNT,RDLoop ;Repeat untill all bytes received.

RDEX: CALL SENDSTOP ;Done, send an I2C stop.
RET




simply put in BYTECOUNT the nr of bytes to send/receive.
in SLAVEADD the I2Caddress
in R0 the rambuffer address
and call SENdata or RCVdata

(uC is master)

List of 12 messages in thread
TopicAuthorDate
I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      
RE: I2C Control            01/01/70 00:00      

Back to Subject List