#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;
}
Hi,
This has been discussed often enough in this forum.
Or do you encounter a special problem?
Klaus
Yes first I wanted to confirm that my routine is correct. I am facing problem to write routine for DS1307
SDA = SDA_High;
SCL = SCL_Low;
SDA = SDA_Low;
SCL = SCL_Low;
Does it make any sense ?Try as a first attempt, insert a small delay between each one:
// I2C_ReStart sends start bit sequence
void I2CRestart(void)
{
SDA = SDA_High;
delay (1000);
SCL = SCL_High;
delay (1000);
SDA = SDA_Low;
delay (1000);
SCL = SCL_Low;
delay (1000);
}
if you want to reinvent the wheel ... then I´d say it is your job to read I2C specifications.Does it make any sense ?
I don't want to repeat someone code. I want to implement my own with proper understanding. I just want someone who can tell me weather my code is right or wrong. I said previously I want to interface ds1307. If my I2C routine is OK than I will write code for DS1307. I am just asking confirmation for I2C routine. I will post all code for i2c routineHi,
if you want to reinvent the wheel ... then I´d say it is your job to read I2C specifications.
Klaus
#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 Delay Function //
void delay(unsigned int d)
{
unsigned int i;
for(i=0;i<d;i++);
}
//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;
delay (1000);
SCL = SCL_High;
delay (1000);
SDA = SDA_Low;
delay (1000);
SCL = SCL_Low;
delay (1000);
}
//I2C_Stop sends stop bit sequence//
void I2CStop()
{
SCL = SCL_Low;
delay (1000);
SDA = SDA_Low;
delay (1000);
SCL = SCL_High;
delay (1000);
SDA = SDA_High;
delay (1000);
}
//I2C_Send_ACK sends ACK bit sequence
void I2CAck()
{
SDA = SDA_Low;
delay (1000);
SCL = SCL_High;
delay (1000);
SCL = SCL_Low;
delay (1000);
SDA = SDA_High;
delay (1000);
}
//I2C_Send_NACK sends NACK bit sequence //
void I2CNak()
{
SDA = SDA_High;
delay (1000);
SCL = SDA_High;
delay (1000);
SCL = SCL_Low;
delay (1000);
SDA = SDA_High;
delay (1000);
}
unsigned char I2CSend(unsigned char Data)
{
unsigned char i, ack_bit;
for (i = 0; i < 8; i++)
{
if ((Data & 0x80) == 0)
SDA = SDA_Low;
delay (1000);
else
SDA = SDA_High;
delay (1000);
SCL = SCL_High;
delay (1000);
SCL = SCL_Low;
delay (1000);
Data<<=1;
}
SDA = SDA_High;
SCL = SCL_High;
delay (1000);
ack_bit = SDA;
SCL = SCL_Low;
delay (1000);
return ack_bit;
}
unsigned char I2CRead()
{
unsigned char i, Data=0;
for (i = 0; i < 8; i++) {
SCL = SCL_High;
delay (1000);
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
SCL = SCL_Low;
delay (1000);
}
return Data;
}
The huge delays make no sense. You want to introduce maximal µs delays to conform with 100 kHz I2C requirements.
99% of all treads and topics on this forum are similar
I provided complete code. Even that did not helped. But there I specially put delay function out from the code to make it optional and not hw related.
I don't understand this. This is why there are existing programs and libraries.I said previously I don't want to copy paste someones code. Without proper understanding How can someone write code.
Hi,
I don't understand this. This is why there are existing programs and libraries.
If you want to drive an car...you won't build your own car, especially when you don't have the knowledge to build a car.
You use it. It works. And you don't need to know about how the engine works and all that physics that is behind this.
If you want to creat a PCB layout on a computer. You won't design your own computer hardware, you won't design the OS and you won't write your own PCB layout software. You use existing, proven parts. Then read the manual on how to use it.
It is the wrong way to write software.
But you are free to go your own way.
If you want to do all this on your own, then I see no way around to go to school, learn how to read datasheets and specifications, and find ways to verify your work on your own. A forum can't replace all this.
Klaus
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?