CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Working with bits
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Sep 06, 2020 4:03 pm     Reply with quote

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=cat-128;
   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 on-heat off' control. In your case it's a 'free' bit.

I can't easily find the 'Day of Year' to 'month-day' 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 month-day 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: 19195

View user's profile Send private message

PostPosted: Sun Sep 06, 2020 11:46 pm     Reply with quote

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 (0-11)
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 07, 2020 4:44 am     Reply with quote

hmm given 4 bytes for date/time...

5 bits for year(0-31)
4 bits for month(0-15)
5 bits for day(0-31)
6 for hours(0-31)
6 for minutes(0-63)
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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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