Microcontroller › 8051 › Interfacing AT24c64 with 89c52 › jeni try
jeni try this
;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
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
? Assembly Implementation:
#define SCL P0_1
void I2CInit(){
SDA = 1;
SCL = 1;
}
void I2CStart(){
SCL = 1;
SDA = 0;
SCL = 0;
}
void I2CRestart(){
SCL = 0;
SDA = 1;
SCL = 1;
SDA = 0;
}
void I2CStop(){
SCL = 0;
SDA = 0;
SCL = 1;
SDA = 1;
}
void I2CAck(){
SDA = 0;
SCL = 1;
SCL = 0;
SDA = 1;
}
void I2CNak(){
SDA = 1;
SCL = 1;
SCL = 0;
}
void I2CSend(unsigned char Data){
unsigned char i;
for(i=0;i<8;i++){
SCL = 0;
if((Data&0x80)==0)
SDA = 0;
else
SDA = 1;
SCL = 1;
Data<<=1;
}
SCL = 0;
SDA = 1;
}
unsigned char I2CRead(){
unsigned char i, Data=0;
for(i=0;i<8;i++){
SCL = 0;
SCL = 1;
if(SDA)
Data |=1;
Data<<=1;
}
SCL = 0;
SDA = 1;
return Data;
}