Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Interface Ds1307 with Pic18

Status
Not open for further replies.

Praveen Kumar P S

Member level 4
Member level 4
Joined
Aug 21, 2014
Messages
79
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Location
India
Visit site
Activity points
627
hello guys...
i was trying to interface ds1307 with pic18f4550...
what i want to know is that do the pic have programmable sda and scl pin???
how ds1307 works??
Thank you
 


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.
 
Last edited:
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.

Hi....
There is a little problem with my code here....
the lcd_gotoxy(2,2) not working....it keeps on printing on firstline of the lcd..
here is my code::

Code:
#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); 
              } 
} 
}

But my lcd driver works fine with other program....
when i used it with ds1307 program it works fine but the lcd_gotoxy(2,2) not woking...
plz anybody can figure out whats the problem is ??

Thank You..
 

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.....
 

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 still i have a problem pending....
The ds1307 is working perfectly fine...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...
PLz help me...
Thank YOu....
 

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...

times need to set once in the rtc if you are loading the same code which you posted earlier then it every times reload the timing in DS1307.So do load time once in ds1307 once it is set and updated correctly just put the comment in front of ds1307 time setting function.

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
 
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

I tried the above code, now the rtc not resets itself rather it start with exact time at which the rtc was switched off...
the rtc not working with the battery when the vcc is turned off....
any idea...

Thank You...
 

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.
 
Last edited:

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.


hI...
I did the same,...but no effect. when the vcc supply of controller is switched off, the rtc automatically also turn off.....i think its the problem with my rtcc...
i cant find whats wrong with rtc??

Thank You...
 

put your schematic and ds1307 connection and your battery also plugged in while supply Vcc on.So your Ds1307 switched to battery supply when VCC power off.
 

hello,


Add an extra input to choose or not, Init_DS1307()
After running the program, DS1307 count elapsed time...
when you power off
DS1307 must continue to count and maintain current time (if battery connected!)
but when you Power ON again, you must pass over the init_DS1307 else you start again from scratch.
Just test an input to choose, init or read time.

Code:
lcd_init();
if (input #x is ON)
{
ds1307_init();
delay_ms(100);
ds1307_set_date_time(1,1,1,1,11,0,59,50); 
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top