vead
Full Member level 5
Hello
I want to interface ds1307 with 8051. I am looking help to build routine for I2C Protocols.
Am I missing something in this routine ?
I want to interface ds1307 with 8051. I am looking help to build routine for I2C Protocols.
Code:
#include<reg51.h>
sbit SDA = P0^0;
sbit SCL = P0^1;
#define SDA_Low (0)
#define SDA_High (1)
#define SCL_Low (0)
#define SCL_High (1)
//Set initial values of SCK and SDA pins //
void I2C_Inlization(void)
{
SDA = SDA_High;
SCL = SCL_High;
}
//I2C_Start sends start bit sequence//
void I2C_Start(void)
{
SDA = SDA_Low;
SCL = SCL_Low;
}
// I2C_ReStart sends start bit sequence
void I2CRestart(void)
{
SDA = SDA_High;
SCL = SCL_Low;
SDA = SDA_Low;
SCL = SCL_Low;
}
//I2C_Stop sends stop bit sequence//
void I2CStop()
{
SCL = SCL_Low;
SDA = SDA_Low;
SCL = SCL_High;
SDA = SDA_High;
}
//I2C_Send_ACK sends ACK bit sequence
void I2CAck()
{
SDA = SDA_Low;
SCL = SCL_High;
SCL = SCL_Low;
SDA = SDA_High;
}
//I2C_Send_NACK sends NACK bit sequence //
void I2CNak()
{
SDA = SDA_High;
SCL = SDA_High;
SCL = SCL_Low;
SDA = SDA_High;
}
unsigned char I2CSend(unsigned char Data)
{
unsigned char i, ack_bit;
for (i = 0; i < 8; i++)
{
if ((Data & 0x80) == 0)
SDA = SDA_Low;
else
SDA = SDA_High;
SCL = SCL_High;
SCL = SCL_Low;
Data<<=1;
}
SDA = SDA_High;
SCL = SCL_High;
ack_bit = SDA;
SCL = SCL_Low;
return ack_bit;
}
unsigned char I2CRead()
{
unsigned char i, Data=0;
for (i = 0; i < 8; i++) {
SCL = SCL_High;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
SCL = SCL_Low;
}
return Data;
}