

View previous topic :: View next topic 
Author 
Message 
temtronic
Joined: 01 Jul 2010 Posts: 7628 Location: Greensville,Ontario


Posted: Sun Sep 06, 2020 4:03 pm 


Here's the functions for 'packing' time (hours and minutes) into 7 bits.
variable rtchrs, is the RTC (DS3231) hours value
variable rtcmin, is the RTC (DS3231) minutes value
variable cat, is 'Current Actual Time'
variable phr, is the packed hours
variable pmn, is the packed minutes
Code: 
// converts time hh mm into packed time
char pack_RTC_time() {
ch=rtchrs;
cm=rtcmin;
cat=128+(4*rtchrs) +( rtcmin/15);
return(cat);
}
// converts packed time into hh mm
void dsp_packed_time(int8 cat) {
int8 n;
n=cat128;
phr=(int)(n/4);
pmn=(n(phr*4))*15;
}

You take 24 hrs * 60 minutes, divide by 128 and get 11+ minutes per bit, so 0 is midnight, 1 is 15 minutes after midnight, 2 is 1/2past midnight, etc.
I used this for my remote energy control systems for 'heat on, heat off' times. Using this method only 7 bits are needed for a 'time', the spare bit of the byte was used as a 'control' bit. In my case if set, it'd automatically perform the 'heat onheat off' control. In your case it's a 'free' bit.
I can't easily find the 'Day of Year' to 'monthday' algorithms. I didn't need them for the energy control systems as schedules were similar to 'smart thermostats'. Pretty sure Google can find how to do it. You need a 12 element array for storing the 1st day of each month, then some 'math' to compute monthday into a number from 1 to 365. There's a test for leap years of course.
hope this helps
jay 


Ttelmah
Joined: 11 Mar 2010 Posts: 16342


Posted: Sun Sep 06, 2020 11:46 pm 


If you look, the difference is just 2678400 seconds. This is exactly 31 days.
The reason for the error is simple. Months start at 0, not 1. So you are
converting October, not September.
Month tm_mon; // month of the year (011) 


temtronic
Joined: 01 Jul 2010 Posts: 7628 Location: Greensville,Ontario


Posted: Mon Sep 07, 2020 4:44 am 


hmm given 4 bytes for date/time...
5 bits for year(031)
4 bits for month(015)
5 bits for day(031)
6 for hours(031)
6 for minutes(063)
total is 26 bits, 6 left 'free'
if you 'pack' the hours/minutes as I do, you use 7 bits instead of 12, so you save another 5 bits, total of 21... so 3 bytes, with 3 spare bits.
I hope someone checks my 'math', up all night with bad storm, power on/off, flooding, lots of 'fun'....coffeepot didn't come on either !!!!!
Jay 




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
