i have done the interface between ds1307 and 89v51rd2.the output to uart after reading time and date from the rtc ic not correct i am getting like Time=36:32:0D,Date=1F/1C/3F.can any one help me.
i have did the BCD2HEX and HEX2ascii convertion.
http://www.keil.com/forum/19526/
i am getting the output but it is not correct..can u help me
Do you really think that's going to be in any way possible based on the information you've provided?
the ds1307 is working fine.i am getting the 1 HZ square wave in pin 7 of ds1307.i am getting the data in serial but it is not proper (time=36:32:00 and date=1F/1C/3F).what is the problem with the code...i loaded the code as
void main(void) { unsigned int sec, min, hour, date, month, year; //uchar sec=0x00, min=0x00, hour=0x00, date=0x00, month=0x00, year=0x00; Sys_Init(); DS1307_init(); DS1307_settime(0x20,0x20,0x20); /* Set Time (hh:mm:ss) */ DS1307_setdate(0x01,0x01,0x01); /* Set Date (dd/mm/yy) */ while (1) { //watch_dog(); GLED=0; delay_10ms(10); GLED=1; /* Get Date & Time */ sec = (DS1307_get(SEC)); delay_10ms(10); min = (DS1307_get(MIN)); delay_10ms(10); hour = (DS1307_get(HOUR)); delay_10ms(10); date = (DS1307_get(DATE)); delay_10ms(10); month = (DS1307_get(MONTH)); delay_10ms(10); year = (DS1307_get(YEAR)); delay_10ms(10); datas[0] = hextoascii(((BCD2HEX(hour))>>4)&0x0F); datas[1] = hextoascii((BCD2HEX(hour))&0x0f); datas[2]=':'; datas[3]=hextoascii(((BCD2HEX(min))>>4)&0x0F); datas[4]=hextoascii((BCD2HEX(min))&0x0f); datas[5]=':'; datas[6]=hextoascii(((BCD2HEX(sec))>>4)&0x0F); datas[7]=hextoascii((BCD2HEX(sec))&0x0f); datas[8]=' '; datas[9]=hextoascii(((BCD2HEX(date))>>4)&0x0F); datas[10]=hextoascii((BCD2HEX(date))&0x0f); datas[11]='/'; datas[12]=hextoascii(((BCD2HEX(month))>>4)&0x0F); datas[13]=hextoascii((BCD2HEX(month))&0x0f); datas[14]='/'; datas[15]=hextoascii(((BCD2HEX(year))>>4)&0x0F); datas[16]=hextoascii((BCD2HEX(year))&0x0f); datas[17]='\r'; com_t_index=0; com_t_length=18; TX_RX=1; watch_dog(); TI=1; delay_50ms(50); } } void DS1307_init(void) { I2C_start(); I2C_write(DS1307_ID); I2C_write(0x00); I2C_write(0x00); I2C_stop(); I2C_start(); I2C_write(DS1307_ID); I2C_write(0x07); I2C_write(0x10); I2C_stop(); } unsigned int DS1307_get(unsigned char addr) { unsigned int ret=0; 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(); I2C_stop(); /* Stop i2c bus */ return ((((ret>>4)&0x0F)*10)+(ret&0x0F));; } 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_20ms(20); 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_20ms(20); 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(); /* datas[0]=hextoascii((data_bit)); com_t_index=0; com_t_length=1; TX_RX=1; watch_dog(); TI=1;*/ 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; 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; }
i'm not familiarize with C just ASM (A51) but i see in your source that you wrote for example min = (DS1307_get(MIN)); and in the subroutine DS1307_get(MIN) min means the adress on the DS1307 but in the main prog you made MIN=0 it should be 1 sec=0 ; min = 1 ; hour = 2 ; day = 4 ; month = 5 ; year = 6
try to be sure that the voltage in the "pin 3" < 1.25 * Vcc
unsigned int DS1307_get(unsigned char addr) { unsigned int ret=0; /* shouldn't you issue a start bit before the write ? */ 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(); I2C_stop(); /* Stop i2c bus */ return ((((ret>>4)&0x0F)*10)+(ret&0x0F));; }
The most important tool for debugging is the one between your ears - you need to learn to think about the problem that you are seeing, think about what could possibly cause those problems, think about what tests & observations to make to identify the problems, etc, etc,...
Again, here are some tips:
www.8052.com/.../120313
www.eetimes.com/.../Developing-a-good-bedside-manner
Debugging is an essential part of the development process.
Developing software is not just a matter of writing code - you need to be able to debug it.
Similarly for hardware development.
View all questions in Keil forum