Go Down

Topic: Problems with DS3231 Library (Read 1 time) previous topic - next topic

KirAsh4

I'm wondering if anyone is using this library that Eric Ayars released quite some time ago.  I modified it so it would compile under v1.0.x of the IDE.  Everything I've tried to do with this library so far worked without a problem, from setting the clock, setting simple alarms, reading the clock, etc., etc.  Till now.  I've run into what seems to be a bug but I can't verify that it is a bug or not, or if the example file provided with the library has a problem.

Basically, when setting the alarms, the syntax for Alarm 1 is:
Code: [Select]
Clock.setA1Time(DoW, Hour, Minute+1, Second, 0x0, true, false, false);
... and the syntax for Alarm 2 is similar except it doesn't have a "Second" variable to pass.  Alarm 2 doesn't support that.

The issue here is that whatever I put in for the AlarmBits part (in the above example it's the 0x0) for Alarm 2, it seems to get overwritten by whatever the bits are for Alarm 1.  In the _set example file, I changed the setA1Time() syntax to have 0b1000 as AlarmBits and setA2Time syntax to have 0b111.  However when I read it back, both of them had 0b1000 in them.

I thought it was a fluke so I changed the bits only on Alarm 2 but nothing changed when I read the data back.  Then I changed the bits on Alarm 1 and sure enough, reading the data back and both alarms have the same information, namely what I just changed Alarm 1 to.

So at this point I don't know where the problem sits.  For one, the AlarmBits for Alarm 2 should only be 0b000 to 0b111, so why do I see 0b0000 (four digits), I don't know.  And second I don't understand why it's copying whatever the bits are for Alarm 1 into Alarm 2.

I've attached the library here in the hopes that someone can look over it and figure out what may be the problem.  The author doesn't seem to be responding or supporting it anymore.  At least, his last posting was early last year and I haven't gotten a response from him from the messages I've sent.

PaulS

There is a problem with adding attachments to posts this week. Can you post a link to where you got the library?

KirAsh4

Of course.  The original download came from Eric Ayars' website, located here
http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html

Keep in mind that it was written pre 1.0x, so you have to make the necessary changes to the example files.

PaulS

The AlarmBits variable defines the bits for BOTH alarms. You need to use bitRead() and bitWrite() to diddle with the bits for each alarm individually.

Code: [Select]
/* Retrieves everything you could want to know about alarm
* one.
* A1Dy true makes the alarm go on A1Day = Day of Week,
* A1Dy false makes the alarm go on A1Day = Date of month.
*
* byte AlarmBits sets the behavior of the alarms:
* Dy A1M4 A1M3 A1M2 A1M1 Rate
* X 1 1 1 1 Once per second
* X 1 1 1 0 Alarm when seconds match
* X 1 1 0 0 Alarm when min, sec match
* X 1 0 0 0 Alarm when hour, min, sec match
* 0 0 0 0 0 Alarm when date, h, m, s match
* 1 0 0 0 0 Alarm when DoW, h, m, s match
*
* Dy A2M4 A2M3 A2M2 Rate
* X 1 1 1 Once per minute (at seconds = 00)
* X 1 1 0 Alarm when minutes match
* X 1 0 0 Alarm when hours and minutes match
* 0 0 0 0 Alarm when date, hour, min match
* 1 0 0 0 Alarm when DoW, hour, min match
*/

KirAsh4

#4
Jan 15, 2013, 05:55 pm Last Edit: Jan 15, 2013, 06:00 pm by KirAsh4 Reason: 1
It shouldn't be.  At least, if I understand the code correctly, it's not.  If I read the top of A1's setA1Time function, I see this:
Code: [Select]
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.beginTransmission(CLOCK_ADDRESS);
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));
       ...
}

And for A2:
Code: [Select]
void DS3231::setA2Time(byte A2Day, byte A2Hour, byte A2Minute, byte AlarmBits, bool A2Dy, bool A2h12, bool A2PM) {
// Sets the alarm-2 date and time on the DS3231, using A2* information
byte temp_buffer;
Wire.beginTransmission(CLOCK_ADDRESS);
Wire.write(0x0b); // A1 starts at 0bh    <-- this should read A2 starts at 0bh
// Send A2 Minute and A2M2
Wire.write(decToBcd(A2Minute) | ((AlarmBits & 0b00010000) << 3));
       ...
}

So it appears that they each use the AlarmBits passed to their respective function as opposed to copying from A1.  So what am I missing?  Where does it set both Alarmbits to the same thing?

And even if, let's say A1 is already set to some previous value, and I only set A2, it completely ignores whatever I put in for AlarmBits.  The time gets set properly, but AlarmBits does not.

<note_to_self>Dude, it's time to learn C/C++ ... get a book.</note_to_self>

Go Up