void main(void) { unsigned char sec, min, hour, date, month, year; Sys_Init(); watch_dog(); DS1307_init(); //DS1307_settime(0x67,0x45,0x00); /* Set Time (hh:mm:ss) */ //DS1307_setdate(0x03,0x08,0x06); /* Set Date (dd/mm/yy) */ while (1) { watch_dog(); /* Get Date & Time */ sec = DS1307_get(SEC); min = DS1307_get(MIN); hour = DS1307_get(HOUR); date = DS1307_get(DATE); month = DS1307_get(MONTH); year = DS1307_get(YEAR); } } void DS1307_init(void) { I2C_start(); I2C_write(DS1307_ID); I2C_write(0x00); //I2C_write(0x7F); //I2C_write(0x80); I2C_write(0x00); I2C_stop(); datas[0]='I'; com_t_index=0; com_t_length=1; TX_RX=1; watch_dog(); TI=1; } unsigned char DS1307_get(unsigned char addr) { unsigned char ret; DS1307_init(); I2C_start(); /* Start i2c bus */ I2C_write(DS1307_ID); I2C_write(IN); I2C_write(IN); I2C_stop(); 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_noack(); //delay_10ms(10); I2C_stop(); /* Stop i2c bus */ return ret; datas[0]=hextoascii((ret>>4)&0x0F); datas[1]=hextoascii((ret)&0x0F); com_t_index=0; com_t_length=2; TX_RX=1; watch_dog(); TI=1; } void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss) { I2C_start(); I2C_write(DS1307_ID); /* connect to DS1307 */ I2C_write(0x00); /* Request RAM address at 00H */ I2C_write(ss); /* Write sec on RAM address 00H */ I2C_write(mm); /* Write min on RAM address 01H */ I2C_write(hh); /* Write hour on RAM address 02H */ //delay_10ms(10); I2C_stop(); /* Stop i2c bus */ } void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy) { I2C_start(); I2C_write(DS1307_ID); /* connect to DS1307 */ I2C_write(0x04); /* Request RAM address at 04H */ I2C_write(dd); /* Write date on RAM address 04H */ I2C_write(mm); /* Write month on RAM address 05H */ I2C_write(yy); /* Write year on RAM address 06H */ //delay_10ms(10); I2C_stop(); /* Stop i2c bus */ } void I2C_delay(void) { unsigned char i; for(i=0; i<I2C_DELAY; i++); } void I2C_clock(void) { I2C_delay(); SCL_1 = 1; /* Start clock */ I2C_delay(); SCL_1 = 0; /* Clear SCL */ } void I2C_start(void) { if(SCL_1) SCL_1 = 0; /* Clear SCL */ SDA_1 = 1; /* Set SDA */ SCL_1 = 1; /* Set SCL */ I2C_delay(); SDA_1 = 0; /* Clear SDA */ I2C_delay(); SCL_1 = 0; /* Clear SCL */ } void I2C_stop(void) { if(SCL_1) SCL_1 = 0; /* Clear SCL */ SDA_1 = 0; /* Clear SDA */ I2C_delay(); SCL_1 = 1; /* Set SCL */ I2C_delay(); SDA_1 = 1; /* Set SDA */ } bit I2C_write(unsigned char dat) { bit data_bit; unsigned char i; for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */ { data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */ SDA_1 = data_bit; /* Send data_bit to SDA */ I2C_clock(); /* Call for send data to i2c bus */ dat = dat<<1; } SDA_1 = 1; /* Set SDA */ I2C_delay(); SCL_1 = 1; /* Set SCL */ I2C_delay(); data_bit = SDA_1; /* Check acknowledge */ // I2C_ack(); SCL_1 = 0; /* Clear SCL */ I2C_delay(); return data_bit; /* If send_bit = 0 i2c is valid */ } unsigned char I2C_read(void) { bit rd_bit; unsigned char i, dat; dat = 0x00; for(i=0;i<8;i++) /* For loop read data 1 byte */ { I2C_delay(); SCL_1 = 1; /* Set SCL */ I2C_delay(); rd_bit = SDA_1; /* Keep for check acknowledge */ dat = dat<<1; dat = dat | rd_bit; /* Keep bit data in dat */ SCL_1 = 0; /* Clear SCL */ } return dat; } void I2C_ack() { SDA_1 = 0; // Clear SDA I2C_delay(); I2C_clock(); //Call for send data to i2c bus SDA_1 = 1; // Set SDA } void I2C_noack() { SDA_1 = 1; // Set SDA I2C_delay(); I2C_clock(); // Call for send data to i2c bus SCL_1 = 1; // Set SCL } unsigned short int hextoascii (unsigned char c) { unsigned char ch = c; unsigned char as1; unsigned char as2; // take the char and turn it to ASCII as1 = ch; as1 >>= 4; as1 &= 0x0F; if ( as1<10 ) as1 += 48; else as1 += 55; as2 = ch; as2 &= 0x0F; if ( as2<10 ) as2 += 48; else as2 += 55; return( ((unsigned short int)(as1)<<8) + as2 ); }
www.8052.com/.../120313
www.catb.org/.../smart-questions.html
in my program the osc in DS1307 is not oscillating.i have changed the crystal,rtc and changed capacitor fron 12pf to 15pf.....please can any one help me this is the code....
Is that likely to be a software issue?
i am pradeep,i am using 89v51rd2 to display the time and date to UART.i interfaced DS1307with it.i am getting the output in uart as 0xFF.the osc in DS1307 is not oscillating but i initialized.can any ony tell me what is the problem with the code..
i think so...in my program the DS1307 is initialized....but the osc is not working....
So - what have you done to debug?
Checked that everything is correctly connected?
Checked signals?
Correct speed?
Correct levels?
Tried to look at a snapshot to see if the communication at least starts as expected?
Tested what parts of your code that gets executed - what does your software thinks happens?
i have tested the hard ware connection it is correct.i am getting the clock in SCL_1 pin and some data signal in SDA_1 pin.but in the DS1307 pin 2 i am getting only 1.44volt there is no oscillation in that pin...
"i think so"
Why do you think so?
What does the DS1307 Datasheet tell you about software control of the oscillator?
"in my program the DS1307 is initialized"
But is it initialised correctly? Again, what does the DS1307 Datasheet tell you?
Have you looked at examples, application notes, etc, from the chip manufacturer (Maxim)...?
How did you test/measure that?
What capacitor?
i tested with cro i does not got the osc in pin2.i getting the return value as F after reading from the rct.
So have you checked the Datasheet?
Have you checked the Maxim website for further guidance, Appliication Notes, etc? (there is one Application Note specifically mentioned in the Datatsheet - have you read it?)
Have you followed all the hardware design guidelines? (that Application Note again).
What software options control the oscillator?
No. It really does not sound like you have made a checklist, and then walked through that checklist item-by-item.
It sounds like you have made some random tests and then instantly jumped to this forum.
Please list the tests you have done, and give a good description of the outcome of these tests.
By the way - what equipment do you have access to?
And - as Andy notes - have you double-checked your code against datasheet and application notes?
the CH bit must be 0 to set the osc.i have made that as shown in the code below.is there any correction in it. DS1307_ID=0xD0;
I2C_start(); I2C_write(DS1307_ID); I2C_write(0x00); I2C_write(0x00); I2C_stop();
You really, really need a strategy when debugging; again, see: www.8052.com/.../120313
"It sounds like you have made some random tests and then instantly jumped to this forum"
Indeed.
"Please list the tests you have done, and give a good description of the outcome of these tests"
And also describe your conclusions from those outcomes; ie, what do you think the test results are telling you? Andy why do you think that?
"have you double-checked your code"
I think you are jumping to a conclusion that it's the code.
Why do you think it's the code?
What have you done to rule-out the possibility that it's a hardware issue?
The code can't work if the underlying hardware isn't working - so you really need to rule out hardware issues first