Paritoshgiri
Member level 5
Here i used 89v51rd2 to read and write in ds1307. However i need to modify it so that the user can send inital date and time and the ds1307 starts its clock from the user assigned state. Can anyone plz help me by sending similar code or give me any idea how to modify this code?
#include <P89V51Rx2.H>
#include<stdio.h>
#include "I2C.h"
//sbit SDA = P1^3;
//sbit SCL = P1^2;
char putchar(char c) {
SBUF = c;
while(TI == 0);
TI = 0;
return c;
}
void initserial(void) {
SCON=0X50;
TMOD=0X20;
TH1=0Xfa;
TL1=0X00;
TR1=1;
}
void delay(unsigned int i) {
unsigned int j;
for(j=0;j<i;j++);
}
void rtc_write(unsigned char addr, unsigned char val)
{
I2C_start();
I2C_write(0xD0); // Connect to RTC
I2C_write(addr); // Request RAM address (Low byte)
I2C_write(val); // Write sec on RAM specified address
I2C_stop(); // Stop i2c bus
}
unsigned char rtc_read(unsigned char addr)
{
unsigned char dat;
I2C_start(); // Start i2c bus
I2C_write(0xD0); // Connect to RTC
I2C_write(addr); // Request RAM address (Low byte)
I2C_start(); // Start i2c bus
I2C_write(0xD1); // Connect to RTC for Read
dat = I2C_read(); // Receive data
I2C_noack();
I2C_stop(); // Stop i2c bus
return dat;
}
void main() {
unsigned char Second, Minutes, Hours, Day, Date, Month, Year;
initserial();
printf("Initialization");
//rtc_write(0x07,0x00); //Control register
rtc_write(0x00,0x05); //Control register for second
rtc_write(0x01,0x07); //Control register for minute
rtc_write(0x02,0x06); //Control register for hour
rtc_write(0x03,0x02); //Control register for day
rtc_write(0x04,0x03); //Control register for day
rtc_write(0x05,0x02); //Control register for month
rtc_write(0x06,0x04); //Control register for year
delay(0xFFFF);
Second = rtc_read(0x00);
Minutes = rtc_read(0x01);
Hours = rtc_read(0x02);
Day = rtc_read(0x03);
Date = rtc_read(0x04);
Month = rtc_read(0x05);
Year = rtc_read(0x06);
I2C_stop();
delay(0xFFFF);
printf("Second = %x",Second);
printf("Minutes = %x",Minutes);
printf("Hours = %x",Hours);
printf("Day = %x", Day);
printf("Date = %x", Date);
printf("Month = %x", Month);
printf("Year = %x", Year);
while(1);
}