need help on rtc ds1307 with pic 16f877a

Status
Not open for further replies.
hi dear

i working with ccs c compiler

my problem is rtc does not write set time and date also does not read time and date

it shows only 00:00:00 both date and time how to check my 32.768khz working or not

i checked with CRO the clock is coming from PIC but no data out to sda pin

this is my code

#include<lcd.c>
#include "ds1307.c"
byte sec;
byte min;
byte hrs;
byte day;
byte month;
byte year;
byte dow;

void main()
{

lcd_init();
ds1307_init();
//port_b_pullups(TRUE); // Need to pull up the keypad row pins
lcd_putc("\Welcome\n");
ds1307_set_date_time(31,12,9,2,23,59,55);//day, month,year,dow,hr,min.sec
while(1)
{
delay_ms(1000);
ds1307_get_date(day,month,year,dow);
ds1307_get_time(hrs,min,sec);
lcd_gotoxy(1,1);
printf(lcd_putc,"Time : %02d:%02d:%02d",hrs,min,sec);
lcd_gotoxy(1,2);
printf(lcd_putc,"Date : %02d:%02d:%02d",dow,month,year);

}
}
 
Last edited:
Are you using I2C protocol or not, because I am not seeing I2C being used in code and DS1307 is I2C Compitable IC.
 
Last edited:
////////////////////////////////////////////////////////////////////////////////
/// DS1307.C ///
/// Driver for Real Time Clock ///
/// ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
/// used when PIC loses power and DS1307 run from 3V BAT ///
/// - Disable squarewave output ///
/// ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec) Set the date/time ///
/// ///
/// ds1307_get_date(day,mth,year,dow) Get the date ///
/// ///
/// ds1307_get_time(hr,min,sec) Get the time ///
/// ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA PIN_C4
#define RTC_SCL PIN_C3

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
BYTE seconds = 0;

i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0

i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
i2c_stop();
seconds &= 0x7F;

delay_us(3);

i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x80); // Disable squarewave output pin
i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;

i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03); // Start at REG 3 - Day of week
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f); // REG 3
day = bcd2bin(i2c_read() & 0x3f); // REG 4
mth = bcd2bin(i2c_read() & 0x1f); // REG 5
year = bcd2bin(i2c_read(0)); // REG 6
i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = bcd2bin(i2c_read(0) & 0x3f);
i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
BYTE temp;
BYTE retval;

temp = binary_value;
retval = 0;

while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}

return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
BYTE temp;

temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f));
}
 
Can you try to put these code in I2c
ds1307_set _time(0,0x80); //Reset second to 0 sec. and stop Oscillator
ds1307_set _time(1,0x01); //write min
ds1307_set _time(2,0x01); //write hour
ds1307_set _time(3,0x02); //write day of week
ds1307_set _time(4,0x05); // write date
ds1307_set _time(5,0x01); // write month
ds1307_set _time(6,0x0C); // write year
ds1307_set _time(7,0x10); //SQWE output at 1 Hz
ds1307_set _time(0,0x00); //Reset second to 0 sec. and start Oscillator


void ds1307_set _time(unsigned short address,unsigned short time_data)
{
I2C_Start(); // issue I2C start signal

I2C_Wr(0xD0); // send byte via I2C (device address + W)
I2C_Wr(address); // send byte (address of DS1307 location)
I2C_Wr(time_data); // send data (data to be written)
I2C_Stop(); // issue I2C stop signal
}

sec=ds1307_get_time(0); // read second
minute=ds1307_get_time(1); // read minute
hour=ds1307_get_time(2); // read hour
day=ds1307_get_time(3); // read day
date=ds1307_get_time(4); // read date
month=ds1307_get_time(5); // read month
year=ds1307_get_time6); // read year

unsigned short ds1307_set _time(unsigned short address)
{
I2C_Start();
I2C_Wr(0xd0);
I2C_Wr(address);
I2C_Repeated_Start();
I2C_Wr(0xd1);
data=I2C_Rd(0);
I2C_Stop();
return(data);
}
 
Sorry for the late reply...
please try out the below link you will get all the details...

@Moderator : Sorry for providing the link for the other blogspot....
[MODNOTE]
Please post details of the project to this Thread.[/MODNOTE]
 
Last edited by a moderator:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…