|
|
View previous topic :: View next topic |
Author |
Message |
kaem1189
Joined: 27 Dec 2011 Posts: 12
|
DS1307 Real time Clock Driver AM/PM Format |
Posted: Sun May 05, 2013 6:22 am |
|
|
Ds1307 with AM/PM function tested with software i2c on PIC16f88
Compiler: CCS v4.140
Driver
Code: | ///////////////////////////////////////////////////////////////////////////////////////
/// DS1307_12.h ///
/// Modified Driver for Real Time Clock DS1307 ///
/// 12 hour(AM PM) format ///
/// ///
/// 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,am_pm,min,sec) Set the date/time ///
/// ///
/// ds1307_get_date(day,mth,year,dow) Get the date ///
/// ///
/// ds1307_get_time(hr,am_pm,min,sec) Get the time ///
/// ///
/// ****** am_pm returns ///
/// 1 for PM and 0 for AM ///
/// ///
/// Developed by Christian Rebogio ///
/// suggestions and request please email me at ///
/// christian.rebogio@gmail.com ///
/// credits to http://www.ccsinfo.com/forum/viewtopic.php?t=23255 ///
///////////////////////////////////////////////////////////////////////////////////////
#define RTC_SDA PIN_B5
#define RTC_SCL PIN_B4
#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;
unsigned int8 hrs= 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));
}
|
Example use:
Code: |
#include <16f88.h>
#FUSES NOWDT,PUT,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,INTRC_IO
#use delay(internal=8000000)
#include "lcd16x2.h"
#include "ds1307_12.c"
unsigned int8 sec;
unsigned int8 min;
unsigned int8 hrs;
char time;
unsigned int1 am_pm;
unsigned int8 sec_cache=0;
void main(){
ds1307_init();
ds1307_set_date_time(1,1,1,1,11,0,59,50);
lcd_init();
while(true){
ds1307_get_time(hrs,am_pm,min,sec);
if(am_pm){time='P';}
else{ time='M';}
if(sec_cache!=sec){
sec_cache=sec;
printf(lcd_putc,"\f\%02d:\%02d:\%02d \%cM\n", hrs,min,sec,time);
}
}
} |
Still working with combined 24/12 hours format driver. |
|
|
muratmert4
Joined: 19 May 2011 Posts: 3
|
|
Posted: Fri May 10, 2013 8:00 am |
|
|
Thanks to a good project. |
|
|
kaem1189
Joined: 27 Dec 2011 Posts: 12
|
|
Posted: Wed May 15, 2013 4:32 pm |
|
|
welcome |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon May 20, 2013 12:53 pm |
|
|
Thanks for sharing your code.
It would have been nice when you made more clear that your code only differs from the original version in the part where your code is 12hrs with AM/PM support where the original is 24hrs.
Also, why did you delete all the comments from the original version? Now your code is more difficult to understand. |
|
|
kaem1189
Joined: 27 Dec 2011 Posts: 12
|
|
Posted: Fri May 24, 2013 8:23 pm |
|
|
ckielstra wrote: | Thanks for sharing your code.
It would have been nice when you made more clear that your code only differs from the original version in the part where your code is 12hrs with AM/PM support where the original is 24hrs.
Also, why did you delete all the comments from the original version? Now your code is more difficult to understand. |
Ok, i will edit my code and write some comments to make it clearer. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|