View previous topic :: View next topic |
Author |
Message |
cnsnt
Joined: 29 Nov 2007 Posts: 6
|
ds1307 "day of week" reading problem |
Posted: Thu Nov 29, 2007 1:53 am |
|
|
hi all.I have a problem for ds1307.I'm using following ds1307 driver but ı can not read "day of week"information.LCD always writing "00".. I can read other informations.could somebody help me? THANKS
DRIVER
// ds1307.c -- Functions for the Dallas Semiconductor DS1307
// real time clock and NVRAM chip.
//
// The DS1307 uses BCD as its internal format, but we use it
// in binary format, outside of this module. So we have code
// to convert to/from BCD to binary in the functions below.
//----------------------------------------------------------------------
// Set the date and time.
/*
The registers inside the ds1307 are in this format. The values are in BCD.
DS1307_SECONDS_REG 0
DS1307_MINUTES_REG 1
DS1307_HOURS_REG 2
DS1307_DAY_OF_WEEK_REG 3 // We don't use this register. Set it to 0.
DS1307_DATE_REG 4
DS1307_MONTH_REG 5
DS1307_YEAR_REG 6
*/
void ds1307_set_date_time(void)
{
char i;
// Convert the binary ds1307 data, which is passed in a global array,
// into bcd data. Store it in the same array.
/*for(i = 0; i <7>= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}
return(retval);
}
//--------------------------------------------------------------------------
// This function converts an 8 bit BCD value to an 8 bit binary value.
// The input range must be from 00 to 99.
char bcd2bin(char bcd_value)
{
char temp;
temp = bcd_value;
temp << 1; // Shifting upper digit right by 1 is same as mult. by 8
temp &= 0x78; // Isolate the bits for the upper digit
// Now return: (Tens *8) + (Tens *2) + Ones
return(temp + (temp << 2) + (bcd_value & 0x0f));
}
SOURCE CODE BLOCK
int dow;
while(1)
{
dow=(gca_ds1307_regs[3]);
ds1307_read_date_time();
lcd_init();
printf(lcd_putc,"\n%2X",dow);
} |
|
|
cnsnt
Joined: 29 Nov 2007 Posts: 6
|
|
Posted: Thu Nov 29, 2007 2:41 am |
|
|
also I wrote different values to for date in ds1307 but day of week didn't changed. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Thu Nov 29, 2007 3:17 am |
|
|
This is what I used
Code: |
/******* Weekday computation. Adapted ONLY for years 2000 to 2099 ********/
signed long dayOfWeek;
int cent = 20; // YYYY / 100
//int ISODayOfWeek(int YY, int MM, int DD)
// Zeller's Congruence algorythm
if (mth <3> 0)
year--;
else {
year = 99;
cent--;
}
}
dayOfWeek = day;
dayOfWeek += (((signed long)mth + 1) * 26) / 10;
dayOfWeek += year;
dayOfWeek += year / 4;
dayOfWeek += cent / 4;
dayOfWeek -= cent * 2;
while (dayOfWeek < 0)
dayOfWeek += 7;
dayOfWeek %= 7;
dow=ZellerDOW[(int)dayOfWeek]; // Now we have ISO day of week
rtc_set_datetime(day,mth,year,dow,hour,min);
|
Last edited by Eugeneo on Thu Nov 29, 2007 3:45 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 29, 2007 3:34 am |
|
|
To cnsnt:
Quote: | for(i = 0; i <7>= 10)
{ |
To Eugeneo:
Quote: | if (mth <3> 0)
year--; |
Notice how the code is messed up ? You must disable HTML when
you post code in this forum. There is a tickbox to do this. You can
see it just below the posting window. It looks like this:
Quote: | x Disable HTML in this post |
Make sure you do this before you press the Submit button. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Thu Nov 29, 2007 3:44 am |
|
|
Got it. Thanks |
|
|
cnsnt
Joined: 29 Nov 2007 Posts: 6
|
|
Posted: Thu Nov 29, 2007 4:00 am |
|
|
PCM programmer I didn't understand what you wrote.can you explane it? |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Nov 29, 2007 7:09 am |
|
|
Many would expect that encapsulating code with the code end code buttons is all that is needed to be done to post code. Surprise!! unless you also disable HTML the code won't post correctly. It's quirky but that's just the way it works |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Nov 29, 2007 7:41 am |
|
|
Eugeneo,
Your code fragment is still messed up and was originally posted in the code library by Aurelian Nichita.
When re-using code it is nice to give the original poster credit for his work. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Nov 29, 2007 1:00 pm |
|
|
Basically this is modulo 7 arithmetic with a bit of quirkiness for Feb29 during leap years. val=yr+yr/4 increments one every year and 2 if the year was a leap year. A regular year has 365 days or 365 modulo7 =1 hence we add one each year and one more if the year (leap) has 366 days.
That puts the routine a head of itself during jan and feb of an actual leap year hence the adjusting down of one. From the point of view of modulo 7 both 7 and 0 are the same so if the result is 0 it is set to 7 (sunday) Now jan1 2001 is a monday so table[0] needs to be 6 so year(=01)+day(=01)=2+6 =8mod 7=1=Monday add 31 days for jan
31 mod 7 is 3 table[0]=6 so table[1]=6+3=9 9 mod 7 is 2 (table[1]=2) and so on.
Now the table could be developed for any starting situation but 1/1/01 makes for easy calculation. There is no provision made for centuries not being leap years and 400 year centuries being leap years hence the 2099
restriction. I believe the code below works but if it causes you to miss your [spam] anniversary please don't blame me.
Code: | int8 day_of_week(int8 day,int8 mth,int8 yr)
{int8 val;
const int8 table[12]= {6,2,2,5,0,3,5,1,4,6,2,4};
//// yr is last two digits Ex 2007 is 07
/// mth is month Jan=1.....Dec=12
val=yr+yr/4; /// leap year adj good 2007 to 2099
val=val+table[mth-1]; /// table contains modulo 7 adjustments for mths
val=val+day; /// actual day 1..31
if((yr%4==0) &&(mth<3))val=val-1; /// adjust jan and feb down one for leap year
val=val%7; /// modulo 7
if(val==0)val=7;
/// val is now the day of week 1=Mon .......7=Sun
return(val);
} |
Certain words are considered as spam and this board messes with them in this case the anniversary was the one that repeats every year of marriage
and the word beginning wed and ending in ing is considered spam .. |
|
|
cnsnt
Joined: 29 Nov 2007 Posts: 6
|
|
Posted: Sat Dec 01, 2007 2:55 am |
|
|
I solved problem thank you for all.. |
|
|
|