Respected Sir /Madam
I am developing a project for which i have to interface the ds1307 rtc to it. The controller used is 8051 base.
I am not able to read back the registers and get the desired values written to it. Here is my source.Kindly give suggestions and help to change the corrections if any.
void Rtc(void) { I2c_Routine(); // Go to i2c routine }
void I2c_Routine(void) { CFG841 = CFG841 | 0X01; rtc_set_time(0x00); // Initialise the RTC (Clear sec register CH bit) // while(1); Rtc_Write(); while(1) { /* ack_delay(); ack_delay(); ack_delay(); */ rtc_get_time(); Digit_Placement(1,6,0,hour_M); Digit_Placement(1,6,6,hour_L); Display_Line(6,14,&isto[0]);
Digit_Placement(1,6,19,mins_M); Digit_Placement(1,6,25,mins_L); Display_Line(6,31,&isto[0]);
Digit_Placement(1,6,36,seconds_M); Digit_Placement(1,6,42,seconds_L); } }
void Send_Byte(unsigned char data1) { unsigned char i; for(i=0x80; i!=0; i=i>>1) { if((data1 & i) == 0) { SDA = 0; Delay_New(5); } else { SDA = 1; Delay_New(5); } SCL = 1; Delay_New(10); SCL = 0; } get_ack();
}void Send_Data(unsigned char SLAVEADD) { Send_Byte(SLAVEADD); // Go to Send_Byte routine }
void check_busy(void) { SDA = 1; SCL = 1; if((SCL == 1) && (SDA == 1)) BUSY = 0; else BUSY = 1; }
void Start_Bit(void) { if(BUSY == 0) { // SDA = 1; // SCL = 1; Delay_New(5); SDA = 0; Delay_New(10); // SDATA o/p low for start condition SCL = 0; } }
void Stop_Bit(void) { SDA = 0; Delay_New(5); SCL = 1; Delay_New(10); SDA = 1; }
void get_ack(void) { SDA = 1; Delay_New(5); SCL = 1; Delay_New(10); // while(SDA){} // catch ack from slave(ds 1307) // while(!SDA){} P2_6 = 0; SCL = 0; ack_delay(); P2_6 = 1; Delay_New(20); ack_delay(); // break; }
void give_nack(void) { SDA = 1; // Delay_New(5); SCL = 1; Delay_New(10); SCL = 0; }
void Receive_Byte(void) { unsigned char m = 0; // SCL = 0; SDA = 1; Delay_New(5); readdata = 0x00; for(m=0x80; m!=0; m=m>>1) { SCL = 1; Delay_New(5); if(SDA==1) { readdata |= (m & 0xff); } else if(SDA==0) { readdata |= (m & 0x00); } SCL = 0; } give_nack(); }
void Write_1302(unsigned char addr,unsigned char datawrite) { check_busy(); Start_Bit(); Send_Data(0xD0); // SLAVE ID for DS 1307 Send_Data(addr); // Corresponding register Address of RTC Send_Data(datawrite); // DATA VALUE for the Selected Register Stop_Bit(); }
unsigned char read_1302(unsigned char addr) // incase of correction change this func to return type { check_busy(); Start_Bit(); Send_Data(0xD0); Send_Data(addr); check_busy(); Start_Bit(); Send_Data(0xD1); Receive_Byte(); Stop_Bit(); return(readdata); }
void Rtc_Write(void) { Write_1302(0x07,0x10); //data to control register Write_1302(0x06,0x06); //data to year register Write_1302(0x05,0x01); //data to month register Write_1302(0x04,0x01); //data to date register Write_1302(0x03,0x01); //data to day register Write_1302(0x02,0x00); //data to hour register Write_1302(0x01,0x00); //data to minutes register Write_1302(0x00,0x00); //data to seconds register }
void rtc_set_time(unsigned char sec) { // Write_1302(0x00,(bin2bcd(sec))); Write_1302(0x00,sec); }
void rtc_get_time(void) { unsigned char temp_sec,temp_mins,temp_hour;
temp_sec = read_1302(0x00); temp_mins = read_1302(0x01); hours_d = read_1302(0x02); day_d = read_1302(0x03); date_d = read_1302(0x04); month_d = read_1302(0x05); year_d = read_1302(0x06);
//hours_d = bcd2bin(read_1302(0x02)); //mins_d = bcd2bin(read_1302(0x01)); //temp_sec = bcd2bin(read_1302(0x00)); //seconds_M = ((temp_sec & 0xF0)+ 0x30);
hour_M = (((temp_hour & 0xF0) >> 4) + '0'); hour_M = ((temp_hour & 0x0F) + '0');
mins_M = (((temp_mins & 0xF0) >> 4 ) + '0'); mins_M = ((temp_mins & 0x0F) + '0');
seconds_M = (((temp_sec & 0xF0) >> 4 )+ '0'); seconds_L = ((temp_sec & 0x0F) + '0');
}unsigned char bin2bcd(unsigned char binval) { unsigned char temp_val; if(binval>0x99) return(0); else { temp_val = binval / 10; temp_val 4) & 0x0F) * 10; temp_val += (bcdval & 0x0F); return (temp_val); }
Thanking all. Regards Navaneethan.