Your leaking thatched hut during the restoration of a pre-Enlightenment state.

 

Hello, my name is Judas Gutenberg and this is my blaag (pronounced as you would the vomit noise "hyroop-bleuach").



links

decay & ruin
Biosphere II
Chernobyl
dead malls
Detroit
Irving housing

got that wrong
Paleofuture.com

appropriate tech
Arduino μcontrollers
Backwoods Home
Fractal antenna

fun social media stuff


Like asecular.com
(nobody does!)

Like my brownhouse:
   to deciminutes and back
Sunday, February 20 2011
The old Arduino-based solar controller had a poor-man's real time clock in it that worked as follows: every six seconds a variable of type LONG (four bytes) would be incremented by one. And every 1000 seconds the value would be stored in the Arduino's non-volatile EEPROM to preserve it to within a thousand seconds of where it left off in case of a power failure. I would have stored the time more often, but a location within the EEPROM of an Atmega microcontroller can only be written to 100,000 times before it wears out and becomes useless. Even writing to it only once every thousand seconds, I was blowing through a third of its write cycles every year.
I called these six second units "deciminutes" because they are a tenth of a minute in length. Arduino math is best done with integer values, and (as with decidegrees Fahrenheit), the deciminute is used throughout my controller code as the standard yardstick of time. Now that I was moving to a controller with a real sense of actual time (using a DS1307 real time clock), I would need to be able to convert back and forth between deciminutes and binary-coded-decimal (BCD) years, months, days, hours, minutes, and seconds. It didn't take me long to track down an effective algorithms for doing these calculations, and though they appeared to be written in Basic or Fortran and used days as the standard metric, it wasn't difficult to port them to C and make them communicate in deciminutes. It was also important to be able to convert from deciminutes back to a formatted time for the purposes of printing them in human-readable form either via the serial port or on the LCD. Similarly, I expect to be writing a function that places a decimal point before the last digit of a temperature in decidegrees, thereby making it readable as a temperature in degrees Fahrenheit (in the past I just knew to mentally add that decimal point).
What follows are today's fun deciminute functions in C. One of these refers to a time-setting function in the DS1307, details of which can be found in the second link above. long dayoftoday(int yin, int mmin, int din) { int out; mmin = (mmin + 9) % 12; yin = yin - mmin/10; out=365*yin + (int)yin/4 - (int)yin/100 + (int)yin/400 + (int)(mmin*306 + 5)/10 + ( din - 1 ); return (out); } void deciminutestousefultimeparts(unsigned long deciminutes, byte type) { unsigned long y, ddd, mi, mm, dd, g, rdeciminutes, hours, minutes, seconds; g=deciminutes/14400; rdeciminutes=deciminutes-g*14400; hours=rdeciminutes/600; rdeciminutes=rdeciminutes-hours*600; minutes=rdeciminutes/10; seconds=(rdeciminutes-minutes*10)*6; y = (10000*g + 14780)/3652425; ddd = g - (365*y + y/4 - y/100 + y/400); if (ddd < 0) { y = y - 1; ddd = g - (365*y + y/4 - y/100 + y/400); } mi = (100*ddd + 52)/3060; mm = (mi + 2)%12 + 1; y = y + (mi + 2)/12; dd = ddd - (mi*306 + 5)/10 + 1; if(type==0)//print to serial { Serial.print(y); Serial.print("-"); Serial.print(mm); Serial.print("-"); Serial.print(dd); Serial.print(" "); Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.print(seconds); } else if(type==1) { setDateDs1307(seconds, minutes, hours, 1, dd, mm, y); //set time -- don't know what to do about dayofweek yet } else if(type==2) { } //Serial.print(":"); //Serial.print(g); } unsigned long deciminutessince2000() { //globals we reference: byte second, minute, hour, //dayOfWeek, dayOfMonth, month, year; long days, out; days=dayoftoday(year,month,dayOfMonth); out=days*24*600 + hour*600 + minute *10 + (int)second/6; return (out); }


For linking purposes this article's URL is:
http://asecular.com/blog.php?110220

feedback
previous | next