Praveen Kumar P S
Member level 4
- Joined
- Aug 21, 2014
- Messages
- 79
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 8
- Location
- India
- Activity points
- 627
I think this should solve your question
https://www.nbcafe.in/interfacing-ds1307-real-time-clock-with-pic16f877/
yes you can select other any gpio pin but then you need to write I2C logic seprately for that PINS. you cannot use PIC SPI inbuilt functionality for other normal GPIO pin.
#include <18f4550.h>
#fuses HS, CPUDIV1, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=16000000)
#include <flex_lcd420.c>
#define RTC_SDA PIN_C1
#define RTC_SCL PIN_C0
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL,FORCE_SW)
unsigned int8 bin2bcd(unsigned int8 binary_value);
unsigned int8 bcd2bin(unsigned int8 bcd_value);
void ds1307_init()
{
unsigned int8 sec = 0;
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_start();
i2c_write(0xD1);
sec = i2c_read(0);
i2c_stop();
bit_clear(sec,7);
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_write(sec);
i2c_start();
i2c_write(0xD0);
i2c_write(0x07);
i2c_write(0x80);
i2c_stop();
}
void ds1307_set_date_time(unsigned int8 day, unsigned int8 mth, unsigned int8 year, unsigned int8 dow, unsigned int8 hr,int1 am_pm, unsigned int8 min, unsigned int8 sec)
{
sec &= 0x7F;
hr &= 0x1F;
hr=bin2bcd(hr);
bit_set(hr,6);
if(am_pm){
bit_set(hr,5);
}
else
{
bit_clear(hr,5);
}
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_write(bin2bcd(sec));
i2c_write(bin2bcd(min));
i2c_write(hr);
i2c_write(bin2bcd(dow));
i2c_write(bin2bcd(day));
i2c_write(bin2bcd(mth));
i2c_write(bin2bcd(year));
i2c_write(0x80);
i2c_stop();
}
void ds1307_get_date(unsigned int8 &day, unsigned int8 &mth, unsigned int8 &year, unsigned int8 &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03);
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f);
day = bcd2bin(i2c_read() & 0x3f);
mth = bcd2bin(i2c_read() & 0x1f);
year = bcd2bin(i2c_read(0));
i2c_stop();
}
void ds1307_get_time(unsigned int8 &hr, int1 &am_pm, unsigned int8 &min, unsigned int8 &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = i2c_read(0);
i2c_stop();
am_pm = bit_test(hr,5);
hr = bcd2bin(hr & 0x1f);
}
unsigned int8 bin2bcd(unsigned int8 binary_value)
{
unsigned int8 temp;
unsigned int8 retval;
temp = binary_value;
retval = 0;
while(true)
{
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else
{
retval += temp;
break;
}
}
return(retval);
}
unsigned int8 bcd2bin(unsigned int8 bcd_value)
{
unsigned int8 temp;
temp = bcd_value;
temp >>= 1;
temp &= 0x78;
return(temp + (temp >> 2) + (bcd_value & 0x0f));
}
unsigned int8 day,sec;
unsigned int8 mth,min;
unsigned int8 year,dow,hrs;
char time;
unsigned int1 am_pm;
unsigned int8 sec_cache=0;
void main()
{
lcd_init();
ds1307_init();
delay_ms(100);
ds1307_set_date_time(1,1,1,1,11,0,59,50);
while(true)
{
output_toggle(PIN_E1);
delay_ms(100);
ds1307_get_time(hrs,am_pm,min,sec);
ds1307_get_date(day,mth,year,dow);
if(am_pm)
{
time='P';
}
else
{
time='M';
}
if(sec_cache!=sec)
{
sec_cache=sec;
lcd_gotoxy(2,2);
printf(lcd_putc,"\f\%02d:\%02d:\%02d \%cM\n", hrs,min,sec,time);
}
}
}
printf(lcd_putc,"\f\%02d:\%02d:\%02d \%cM\n", hrs,min,sec,time);
hello
Code:printf(lcd_putc,"\f\%02d:\%02d:\%02d \%cM\n", hrs,min,sec,time);
\n is next line commande. go to next line ! try to remove it..
BTW what is \f ?
Hi paul,
Finally u got it...i spent hours trying to figure out the problem....all was just small careless mistake by me.....:laugh:
thank you man.....
But when the power is turned off the ds1307 resets....although i have used a 3V battery....
when the power is turned off its not,the rtc fully turns offf...
void main()
{
lcd_init();
ds1307_init();
delay_ms(100);
//ds1307_set_date_time(1,1,1,1,11,0,59,50);
with out above line when you load next time load code after the time sets once
Code:void main() { lcd_init(); ds1307_init(); delay_ms(100); //ds1307_set_date_time(1,1,1,1,11,0,59,50); with out above line when you load next time load code after the time sets once
how you are checking that rtc working on battery? just set time in rtc to your current PC time or local time and date and then after some time switched off VCC to your controller and after some time let us say after 10 min just switched on VCC and read what is your time?if its same as your local time then your circuit working fine.
lcd_init();
if (input #x is ON)
{
ds1307_init();
delay_ms(100);
ds1307_set_date_time(1,1,1,1,11,0,59,50);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?