Revision as of 00:02, 14 August 2014 by Ajay Bhargav (talk | contribs) (Created page with "If you want to go through the basics of I2C, Then I would suggest reading I2C TWI Tutorial to get basic understanding of I2C on protocol level. This tutorial will help you...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you want to go through the basics of I2C, Then I would suggest reading I2C TWI Tutorial to get basic understanding of I2C on protocol level. This tutorial will help you understand on how to implement I2C on an 8051 microcontroller.

As you know 8051 e.g AT89C51 does not have on chip I2C controller so many times in our practical projects their is always a need of an I2C device like RTC or EEPROM for timekeeping or non-volatile storage respectively. To achieve we can emulate I2C on GPIOs of 8051 microcontroller to communicate between devices connected on I2C bus.

The basic concept of bit-bang i2c is same either you implement it in C or Assembly language. Following section provides a sample code which shows how to do this.

Assembly Language

For this implementation we are using P0.0 for SDA and P0.1 for SCL. Here we go...

;***************************************
;Ports Used for I2C Communication
;***************************************
sda equ P0.0
scl equ P0.1

;***************************************
;Initializing I2C Bus Communication
;***************************************
i2cinit:
	setb sda
	setb scl
	ret

;****************************************
;ReStart Condition for I2C Communication
;****************************************
rstart:
	clr scl
	setb sda
	setb scl
	clr sda
	ret

;****************************************
;Start Condition for I2C Communication
;****************************************
startc:
	setb scl
	clr sda
	clr scl
	ret

;*****************************************
;Stop Condition For I2C Bus
;*****************************************
stop:
	clr scl
	clr sda
	setb scl
	setb sda
	ret

;*****************************************
;Sending Data to slave on I2C bus
;*****************************************
send:
	mov r7,#08
back:
	clr scl
	rlc a
	mov sda,c
	setb scl
	djnz r7,back
	clr scl
	setb sda
	setb scl
	mov c, sda
	clr scl
	ret

;*****************************************
;ACK and NAK for I2C Bus
;*****************************************
ack:
	clr sda
	setb scl
	clr scl
	setb sda
	ret

nak:
	setb sda
	setb scl
	clr scl
	setb scl
	ret

;*****************************************
;Receiving Data from slave on I2C bus
;*****************************************
recv:
	mov r7,#08
back2:
	clr scl
	setb scl
	mov c,sda
	rlc a
	djnz r7,back2
	clr scl
	setb sda
	ret

To use the above code in your project you can copy the source in your assembly file and call the subroutines. Here is a sample code which shows how to call these subroutines:

;*****************************************
; Write to slave device with
; slave address e.g. say 0x20
;*****************************************
	; Init i2c ports first
	lcall i2cinit
	; Send start condition
	lcall startc
	; Send slave address
	mov a,#20H
	acall send
	; after send call Carry flag has ACK bit
	; If you want to check if send was a
	; success or failure
	; Send data
	mov a,#07H
	acall send
	; Send another data
	mov a,#10
	acall send
	; Send stop condition
	acall stop

;*****************************************
; Read from slave device with
; slave address e.g. say 0x20
;*****************************************
	; Init i2c ports first
	lcall i2cinit
	; Send start condition
	lcall startc
	; Send slave address with Read bit set
	; So address is 0x20 | 1 = 0x21
	mov a,#21H
	acall send
	; Read one byte
	acall recv
	; Send ack
	acall ack
	; Read last byte
	acall recv
	; Send nak for last byte to indicate
	; End of transmission
	acall nak
	; Send stop condition
	acall stop
Powered by MediaWiki
ArrayContent is available under Creative Commons Attribution Non-Commercial Share Alike unless otherwise noted.