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.
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);
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
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
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
///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
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){
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.