Pages: [1]   Go Down
 Author Topic: Understanding bits translations  (Read 161 times) 0 Members and 1 Guest are viewing this topic.
Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « on: January 14, 2013, 04:19:40 am » Bigger Smaller Reset

This is specific to a DS3231 RTC.  I'm using a library that sets the alarm in the following syntax:
Code:
void DS3231::setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM) {
// Sets the alarm-1 date and time on the DS3231, using A1* information
byte temp_buffer;
Wire.write(0x07); // A1 starts at 07h
// Send A1 second and A1M1
Wire.write(decToBcd(A1Second) | ((AlarmBits & 0b00000001) << 7));
// Send A1 Minute and A1M2
Wire.write(decToBcd(A1Minute) | ((AlarmBits & 0b00000010) << 6));
// Figure out A1 hour
if (A1h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A1Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A1Hour = A1Hour - 12;
A1PM = true;
}
if (A1PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = decToBcd(A1Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = decToBcd(A1Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = decToBcd(A1Hour);
}
temp_buffer = temp_buffer | ((AlarmBits & 0b00000100)<<5);
// A1 hour is figured out, send it
Wire.write(temp_buffer);
// Figure out A1 day/date and A1M4
temp_buffer = ((AlarmBits & 0b00001000)<<4) | decToBcd(A1Day);
if (A1Dy) {
// Set A1 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
Wire.write(temp_buffer);
// All done!
Wire.endTransmission();
}

Where I'm stuck is how to set the proper AlarmBits.  The examples that come with the library always passes '0x0' to that, which translates to "Alarm when date, hours, minutes, and seconds match" if A1Dy is FALSE, or "Alarm when day, hours, minutes, and seconds match" if A1Dy is TRUE, according to the datasheet.

I need to change that to "Alarm when hours, minutes, and seconds match".  Reading the datasheet, there's this specific table for those bits:

Here's my problem, well two of them:
First, it shows DY/DT as either X, 0, or 1.  That's A1Dy in the library code.  How do I not set either TRUE nor FALSE to that so I can use one of the other options for alarms.
Second, how do I translate what the table is giving me into what I need to pass to the AlarmBits variable?  Specifically, the one I need has bits set at 1,0,0,0 (or 1,0,0 for the second alarm.)
 « Last Edit: January 14, 2013, 04:27:39 am by KirAsh4 » Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #1 on: January 14, 2013, 04:45:45 am » Bigger Smaller Reset

X means "don't care", meaning the value of that particular bit in the shown combination is irrelevant.
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « Reply #2 on: January 14, 2013, 04:57:04 am » Bigger Smaller Reset

Got it, so it doesn't matter what it's set at as soon as I can figure out how to set the AlarmBits to something other than 0x0 ... based on the table.  Thanks AWOL.

Also, reached 1,000 posts with this one.
 Logged

Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « Reply #3 on: January 14, 2013, 02:15:40 pm » Bigger Smaller Reset

Bump ... still trying to figure out how to pass the proper bits to the AlarmBits variable based on the table provided.  Right now, the library simply passes 0x0 to it.  I need to pass the equivalent of bits 1,0,0,0 for Alarm 1, and 1,0,0 for Alarm 2.
 Logged

Pittsburgh, PA, USA
Offline
Karma: 31
Posts: 2947
I only know some basic electricity....
 « Reply #4 on: January 14, 2013, 02:29:06 pm » Bigger Smaller Reset

Google on C bit fields, you can define the RAM to give specific bits and bit-groups names.

 Logged

Examples can be found at Learning in the Main Site and at the Playground

Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « Reply #5 on: January 14, 2013, 02:52:30 pm » Bigger Smaller Reset

Hrm yes.  I somewhat understand that.  But I still don't know what I should be passing to AlarmBits ...  I see it getting passed within the library code and what not ...
 Logged

Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « Reply #6 on: January 14, 2013, 03:10:43 pm » Bigger Smaller Reset

Figured it out!  Thanks for the hint!

The solution is to pass those bits in as hex values, so
Code:
once a second                  1111  = 0x0f
when secs match                1110  = 0x0e
when mins + secs match         1100  = 0x0c
when hrs + mins + secs match   1000  = 0x08
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #7 on: January 14, 2013, 03:13:04 pm » Bigger Smaller Reset

Quote
The solution is to pass those bits in as hex values, so
Or as binary values,
0b1111  etc
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline
Edison Member
Karma: 41
Posts: 1260
Reviving dead brain cells with Arduinos.
 « Reply #8 on: January 14, 2013, 03:16:46 pm » Bigger Smaller Reset

I suppose that would work too AWOL.  I just stuck to the same format that the library was using.  Though looking at it in binary format makes more sense since it actually matches exactly what the table shows and one doesn't have to convert it to HEX values.
 Logged

Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
 « Reply #9 on: January 14, 2013, 03:19:34 pm » Bigger Smaller Reset

Or, if you've got a lot of these operations, give the individual bits names, and use OR operations to combine them.
Easier to read and therefore to maintain.
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

 Pages: [1]   Go Up