Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HOw to conect I2C RTC DS1307 to 89S

Status
Not open for further replies.
Perhaps this can help you
 

This is simplest, naive working code here,though it is working and efficient it may help you.....

#define I2C_DELAY 0x0F
// DELAY at 11.0592MHz crystal.Calling the routine takes about 22us, and then //each count takes another 17us.
void I2C_delay(void);
void I2C_clock(void);
void I2C_start(void);
void I2C_stop(void);
bit I2C_write(unsigned char dat);
unsigned char I2C_read(void);


void I2C_delay(void){ //gives micro seconds delay
register unsigned int i;
for(i=0;i<I2C_DELAY;i++);
}



void I2C_clock(void){
I2C_delay();

SCL=1; //Start Clock

I2C_delay();

SCL=0; //Clear SCL
}

void I2C_start(void){
if(SCL){
SCL=0; //clear SCL intentionally cleared because if SCL=1 and SDA=0 it can be interpreted wrongly
I2C_delay();
}

SDA=1; //Set SDA
I2C_delay();
SCL=1; //Set SCL

I2C_delay();

SDA=0; //clear SDA

I2C_delay();

SCL=0; //clear SCL
I2C_delay();

}

void I2C_stop(void){
if(SCL){
SCL=0; //clear SCL
I2C_delay();
}

SDA=0; //clear SDA
I2C_delay();

SCL=1; //Set SCL

I2C_delay();

SDA=1; //Set SDA
I2C_delay();
}

bit I2C_write(unsigned char dat){
bit data_bit=1;
unsigned char i;

for(i=0;i<8;i++) //A "for" loop being executed 8times to send 1-byte (8-bits) of data.
{
data_bit = dat & 0x80; //Filter MSB bit Keep to data_bit
SDA=data_bit; //Send data_bit to SDA

I2C_clock(); //Call for send data to i2c bus

dat =dat<<1;
}

SDA=1;

I2C_delay();

SCL =1;

I2C_delay();

data_bit=SDA; //Check acknowledge
SCL=0; //Clear SCL

I2C_delay();

return data_bit; //If send_bit=0 i2c is valid
}


unsigned char I2C_read(void){
register bit rd_bit;
register unsigned char i,dat;

dat=0x00;
for(i=0;i<8;i++){
I2C_delay();

SCL=1;
I2C_delay();
rd_bit =SDA;//Keep for check acknowledge

dat=dat<<1;
dat =dat|rd_bit; //Keep bit data in dat
SCL=0;
}
return dat;
}


//These are low level driver functions and below are functions to accesses them


//////////////////////////////////////////
// DS1307 BLOCK functions/////////////////
//Writes and Retrieves time from DS1307////
/////////////////////////////////////////
#define DS1307_ID 0xD0

//Register Names and Addresses:

#define MIN 0X01
#define HOUR 0X02
#define CONTROLREG 0X07


unsigned char DS1307_get(unsigned char);
void DS1307_SetReg(void); //Sets DS1307 register
void DS1307_gettime(unsigned char *time_string);


//Functions to read and write time from Real Time Clock IC DS1307
//using I2C interface

unsigned char DS1307_get(unsigned char addr){
unsigned char ret;

I2C_start(); //Start I2c bus transaction

I2C_write(DS1307_ID); //Connect to DS1307
I2C_write(addr); //Request RAM address on DS1307

I2C_start(); //Start I2C bus

I2C_write(DS1307_ID+1); //Connect to DS1307 for Read
ret= I2C_read(); //Receive data

I2C_stop(); //Stop I2C bus

return ret;
}


void DS1307_SetReg(void){

I2C_start(); //Start I2C bus transaction

I2C_write(DS1307_ID); //connect to DS1307
I2C_write(0x00); //Request RAM address at 00H

I2C_write(seconds); //Write sec on RAM address 00H
I2C_write(minutes); //Write min on RAM address 01H
I2C_write(hour); //Write hour on RAM address 02H
I2C_write(day); //Write day on RAM address 03H
I2C_write(date); //Write date on RAM address 04H
I2C_write(month); //Write month on RAM address 05H
I2C_write(year); //Write year on RAM address 06H
I2C_write(controlReg); //Write controlReg on RAM address 07H

I2C_stop(); //Stop I2C bus

}

void DS1307_gettime(unsigned char *time_string){

char hour_raw=7;
char min_raw=7;

min_raw=DS1307_get(MIN);
hour_raw=DS1307_get(HOUR);

}

}


///I hope u don't want more for getting out what u want from DS1307.:lol:

//And do remember the following


"The reason I had to use the resistors, was to create a voltage divider to create a voltage on the battery backup pin.
As I found out and fortunately you will not endure because you are reading this right now,
is that the battery pin must have 2.5-3vdc to operate properly or the chip will stop responding to IC2 requests.
Ok, we all know the rule, "When all else fails, READ THE DIRECTIONS". As wisdom will have it,
I eventually read this seemingly insignificant information in the Data Sheet.
Well if I get the time I will outline all the details on the parts and schematic,
but the bottom line is that I was still to cheap to buy a 3v battery,
so I just used the voltage divider to drop the 5vdc supply.
The absolute most funny part is that the chip does run just fine without the 3vdc...... sometimes.
Meaning you think you have it all worked out and then......... sometimes.
Hence four hours. But your not going to make that mistake are you?
Because you are here. Soooooooo, after all is said and done the chip is very stable and works as expected and is reliable."



IT TOOK ME DAMN 21 DAYS TO FIND THIS OUT DAY AND NIGHTS WORKING:cry::cry:
Don't fall into trap again.
 

great work palreddyanurag ,
i m struggling from one week but no code is working ..B/W i found your code and will come back to u with result..anyway from your code , pls calrify:
void DS1307_gettime(unsigned char *time_string){

char hour_raw=7;
char min_raw=7;

min_raw=DS1307_get(MIN);
hour_raw=DS1307_get(HOUR);

}

}///
what is that *timestring.....it is unreferenced variable..
 

time_LED[4]-----for holding nos to be displayed & for retrieving hours and minutes

the time_string is essentially a pointer to time_LED so that when time is retrieved and needs to be displayed on 7-segment LEDs following manipulation can be done

min_raw=DS1307_get(MIN);
time_string[3]=(min_raw & 0x0F);
time_string[2]=(min_raw & 0x70)>>4 ;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top