I'm not usually in favour of re-inventing the wheel - but I noticed a comment about DS3231 alarms in the forum recently..
I alo wanted an interrupt using alarm 2 (hours and minutes match) - to wake a low power logger (so this can occur on the battery backup of the RTC and wake an ATTiny) (Note I used a 1s SQW - though this required the RTC clock to be powered and a PP3 lasted ~24hours not good)
So - without checking the datasheet - anyone know the correct values for SQWandINTControl.. Okay - you can check the datasheet. Still struggling?
So I knocked together a simple 'talk to the RTC' proto-component. I was aslo slightly dissatisified with the current syntax - but seeking suggestions here.
I came up with
Set/GetTime(hms[3]) (Where hms is a 3 byte array with hours, mins.... as hms[0] hms[1] etc)
Set/GetAlarm1(hms[3]) and Get/SetAlarm2[fm[2]) (or Get/SetAlarm(hm(s), alarmNo)
An alternative for Set is SetTime(hrs, mins, secs) - unfortunately this is not so easy for retrieving multiple values, and I like the idea of get and set being symetrical.
And I'd anticipate - Get/SetDate(dmy[]) for RTC and Alarm1
GetTime/Datestring
SetAlarmFlags
SetAlarm1Flags(flags) and SetAlarm2Flags(flags) or SetAlarmFlags(alarm, flags) - any thoughts?
I added GetTemperature().
I'm hoping the component will save a fair bit of memory compared to the current (which seems based on an Arduino one - it seems to have all the i2c code in every function - rather than a Get/SendBytes(addr, n, data[]) - I'm aiming for ATTinies here.
Any other ideas / suggestions (currently I have a SetMode(12h = true) to set 12h mode rather than passing this as an argument - and set bit 7 of the hours for PM? This seems slightly clumsy - and I also thought of only supporting 24hour mode, or a PM flag passed to the Set macros?
Martin
DS3231 revisited
Moderator: Benj
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: DS3231 revisited
Not much interest...
Still I made an alpha version. Seems to work well (with some provisos - alarms can be set, but not the sqw and 12h mode isn't really supported yet)
It also isn't made into a component - so extraneous macros.
On the plus side it works - demo main here sets a minute alarm on alarm 2 and dumps time date and RTC temp and control registers to UART every 60s
Also ~ 50% size of FC version....
So if anyone is feeling brave and would like to do some testing.... Edit 29/6 - updated with version with EnableSQW
Martin
Still I made an alpha version. Seems to work well (with some provisos - alarms can be set, but not the sqw and 12h mode isn't really supported yet)
It also isn't made into a component - so extraneous macros.
On the plus side it works - demo main here sets a minute alarm on alarm 2 and dumps time date and RTC temp and control registers to UART every 60s
Also ~ 50% size of FC version....
So if anyone is feeling brave and would like to do some testing.... Edit 29/6 - updated with version with EnableSQW
Martin
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: DS3231 revisited
Hi Martin,
Thank you.
I like the sound of setting alarms from the DS3231
That is one thing the RTC within Flowcode (compatible with DS3232) is lacking: When I get a chance I will do some testing & let you know how it goes.
Thank you.
I like the sound of setting alarms from the DS3231
That is one thing the RTC within Flowcode (compatible with DS3232) is lacking: When I get a chance I will do some testing & let you know how it goes.
Martin
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: DS3231 revisited
The DS3231 doesn't quite do what I'd hoped - alarm when running on battery backup. I may have to look at something like the MCP7940n - which seems able to do it. (The SQW does work on battery on the ds3231/2)
General layout and details of control are similar though and the current FC component seems fairly basic (no alarms but a set register..)
I was 'inspired' by some temp loggers we receive with high-value cold chain injections.. These 'run' for more than a year on a CR2032 cell (and output a PDF to a PC when plugged in) they have an LCD display too. They are also single use (! - wasteful) - with no way to reset / restart them. A fairly determined effort to take one apart failed (that lid is (very) firmly glued on) - though I guess the dremel might persuade it otherwise..
Martin
General layout and details of control are similar though and the current FC component seems fairly basic (no alarms but a set register..)
I was 'inspired' by some temp loggers we receive with high-value cold chain injections.. These 'run' for more than a year on a CR2032 cell (and output a PDF to a PC when plugged in) they have an LCD display too. They are also single use (! - wasteful) - with no way to reset / restart them. A fairly determined effort to take one apart failed (that lid is (very) firmly glued on) - though I guess the dremel might persuade it otherwise..
Martin
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: DS3231 revisited
So for anyone to test:
As a (pre)component (there is no simulation) - It's pretty much complete - apart from 12h mode (anyone any good suggestions on a suitable syntax - I lean towards Set12h mode and then taking the cue from the RTC value of this flag (and have a pm flag in time setters), and of course simulation.
Alarms seem to work well.
A trap for the unwary (which I fell into) - if you set an alarm and then sleep the MCU (and I output quite a bit of debug before the sleep) then if the alarm has occurred before the sleep then the CPU will never be restarted. With the 1s (every 0s) on alarm 2 - this is easy(ish) to hit - so check the seconds first and if they == 59 then wait before enabling the alarm.
Note that the alarms need to be cleared (using CheckAlarm) before they will re-occur.
Edit: - I need to export the i2c settings. Will add a read / write ram for the DS3232.
Martin
As a (pre)component (there is no simulation) - It's pretty much complete - apart from 12h mode (anyone any good suggestions on a suitable syntax - I lean towards Set12h mode and then taking the cue from the RTC value of this flag (and have a pm flag in time setters), and of course simulation.
Alarms seem to work well.
A trap for the unwary (which I fell into) - if you set an alarm and then sleep the MCU (and I output quite a bit of debug before the sleep) then if the alarm has occurred before the sleep then the CPU will never be restarted. With the 1s (every 0s) on alarm 2 - this is easy(ish) to hit - so check the seconds first and if they == 59 then wait before enabling the alarm.
Note that the alarms need to be cleared (using CheckAlarm) before they will re-occur.
Edit: - I need to export the i2c settings. Will add a read / write ram for the DS3232.
Martin
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: DS3231 revisited
Anyone had a chance to play - esp. thoughts on the interface?
I've just managed to get a mcp7940n up and running on a breadboard - I used the smallest value caps I had for C1 and C2 - and actually the crystal seems to be running within a gnat's whisker of 32kHz - but suggestions on how to get the correct values (how to measure Cstray for example) welcome.
The current mcp7940n component has a completely different interface to the DS3231 chip - though the 'internal' workings should be pretty similar. The basic register set/layout is pretty similar to the ds1307 - and microchip helpfully publish a guide on the differences.
So far I've got the square wave output working at all frequencies - so i2c comms etc all working..
I missed a trick - I could have used the 32kHz square wave from a DS3231 instead of a crystal...
Martin
I've just managed to get a mcp7940n up and running on a breadboard - I used the smallest value caps I had for C1 and C2 - and actually the crystal seems to be running within a gnat's whisker of 32kHz - but suggestions on how to get the correct values (how to measure Cstray for example) welcome.
The current mcp7940n component has a completely different interface to the DS3231 chip - though the 'internal' workings should be pretty similar. The basic register set/layout is pretty similar to the ds1307 - and microchip helpfully publish a guide on the differences.
So far I've got the square wave output working at all frequencies - so i2c comms etc all working..
I missed a trick - I could have used the 32kHz square wave from a DS3231 instead of a crystal...
Martin