amc2020:group_n:ds3231rtc
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
amc2020:group_n:ds3231rtc [2020/07/26 11:55] – [4.2 The Code Explained] jonas001 | amc2020:group_n:ds3231rtc [2020/08/29 21:55] – [4. Programming the DS3231] jonas001 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | < | ||
+ | < | ||
+ | <a href=" | ||
+ | <span style=" | ||
+ | ↩ Back to the main page | ||
+ | </ | ||
+ | </a> | ||
+ | </ | ||
+ | </ | ||
+ | |||
====== DS3231 Real Time Clock ====== | ====== DS3231 Real Time Clock ====== | ||
Line 86: | Line 96: | ||
There are a number of libraries available for the DS3231 RTC module, multiple of which were tested out. However, most of them are rather poorly documented, most have different features and some of them did not include the necessary function for the scope of the project. | There are a number of libraries available for the DS3231 RTC module, multiple of which were tested out. However, most of them are rather poorly documented, most have different features and some of them did not include the necessary function for the scope of the project. | ||
- | In the end, after being inspired by the approach of {{https:// | + | In the end, after being inspired by the approach of {{https:// |
The sketch by Ralph Bacon only included setting and reading the time from the module and printing the result to the serial monitor. Most of this was used with only minor changes in this sketch. The implementation of the interrupt and the setting and clearing of alarm 1 was done by me. | The sketch by Ralph Bacon only included setting and reading the time from the module and printing the result to the serial monitor. Most of this was used with only minor changes in this sketch. The implementation of the interrupt and the setting and clearing of alarm 1 was done by me. | ||
+ | |||
+ | <WRAP center round info 100%> | ||
+ | For anyone interested, I also made my own library for the DS3231 RTC module. The library allows setting and reading the time as well as using alarm 1 and 2 and also allows to read and change the alarm and control registers. There are also functions included for directly printing the time and the register settings to the serial monitor. | ||
+ | |||
+ | The library can be downloaded as a zip file from {{ : | ||
+ | |||
+ | To understand what is done by the functions, I recommend checking the cpp file in the library folder and the [[https:// | ||
+ | </ | ||
+ | |||
==== 4.1 The Code ==== | ==== 4.1 The Code ==== | ||
Line 247: | Line 266: | ||
Serial.print(Year); | Serial.print(Year); | ||
Serial.print(" | Serial.print(" | ||
- | switch(Day){ | + | switch(Day){ |
case 1: | case 1: | ||
Serial.println(" | Serial.println(" | ||
Line 279: | Line 298: | ||
<ol> | <ol> | ||
<li> | <li> | ||
- | As explained before, no external library needs to be included, only the <font face=" | + | As explained before, no external library needs to be included, only the <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The I2C address of the DS3231 needs to be defined. By default (if no solder connections are used) it is hexadecimal <font face=" | + | The I2C address of the DS3231 needs to be defined. By default (if no solder connections are used) it is hexadecimal |
</li> | </li> | ||
<li> | <li> | ||
Line 289: | Line 308: | ||
<br> | <br> | ||
<li> | <li> | ||
- | As explained in section 2, the alarm register mask bits define the alarm rate. For example, choosing the <font face=" | + | As explained in section 2, the alarm register mask bits define the alarm rate. For example, choosing the <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The <font face=" | + | The <b><font face=" |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | Here the Arduino pins for the interrupt (<font face=" | + | Here the Arduino pins for the interrupt (<b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | When an interrupt is triggered, the MCU executes the attached interrupt service routine ISR which is defined later. The variable <font face=" | + | When an interrupt is triggered, the MCU executes the attached interrupt service routine ISR which is defined later. The variable |
<br> | <br> | ||
Configuring a variable as volatile tells the Compiler to load the variable from the RAM instead of the a storage register. This is necessary when the variable can be changed from somewhere else than the code that it is appearing in, for example a concurrently executed function like an ISR. | Configuring a variable as volatile tells the Compiler to load the variable from the RAM instead of the a storage register. This is necessary when the variable can be changed from somewhere else than the code that it is appearing in, for example a concurrently executed function like an ISR. | ||
Line 306: | Line 325: | ||
<br> | <br> | ||
<li> | <li> | ||
- | In the <font face=" | + | In the <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The interrupt pin <font face=" | + | The interrupt pin <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The pin for the LED needs to be configured as an <font face=" | + | The pin for the LED needs to be configured as an <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | Here the later defined function <font face=" | + | Here the later defined function |
</li> | </li> | ||
<li> | <li> | ||
- | The function <font face=" | + | The function |
</li> | </li> | ||
<li> | <li> | ||
- | To attach an interrupt on the Arduino UNO, the function <font face=" | + | To attach an interrupt on the Arduino UNO, the function |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | The main <font face=" | + | The main <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The <font face=" | + | The <b><font face=" |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | The function <font face=" | + | The function |
</li> | </li> | ||
<li> | <li> | ||
Line 339: | Line 358: | ||
<br> | <br> | ||
<li> | <li> | ||
- | The function <font face=" | + | The function |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | As explained before (11), <font face=" | + | As explained before (11), <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | The method <font face=" | + | The method |
</li> | </li> | ||
<li> | <li> | ||
- | The method <font face=" | + | The method |
</li> | </li> | ||
<li> | <li> | ||
- | Now the value for <font face=" | + | Now the value for <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | When all the registers are configured and the DS3231 has a new time set, the I2C transmission is stopped with <font face=" | + | When all the registers are configured and the DS3231 has a new time set, the I2C transmission is stopped with <b><font face=" |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | Here follows the definition of function <font face=" | + | Here follows the definition of function |
</li> | </li> | ||
<li> | <li> | ||
To configure the four registers of alarm 1, it is necessary to convert the seconds, minutes, hours and date/day into BCD format again. However, as can be seen in table 1, the alarm registers also contain the mask bits for the alarm rate which are stored as A1M1 to A1M4 in the MSBs of the four. The Which mask bit is used for which register is determined by the element chosen in the mask bit array (5). | To configure the four registers of alarm 1, it is necessary to convert the seconds, minutes, hours and date/day into BCD format again. However, as can be seen in table 1, the alarm registers also contain the mask bits for the alarm rate which are stored as A1M1 to A1M4 in the MSBs of the four. The Which mask bit is used for which register is determined by the element chosen in the mask bit array (5). | ||
<br> | <br> | ||
- | The function <font face=" | + | The function |
<br> | <br> | ||
This same process is repeated for the minutes hours and days. What changes is the position of the respective mask bit in the mask bit array element; for minutes it is 4, for hours 5 and for days or date 6. | This same process is repeated for the minutes hours and days. What changes is the position of the respective mask bit in the mask bit array element; for minutes it is 4, for hours 5 and for days or date 6. | ||
<br> | <br> | ||
- | The hour format will automatically be 24 hours like in the time register because bit 6 stays always 0 (see table 1). However, when the alarm rate is chosen to be either once a week or once a month, i.e. days/date are matched, that has to be changed in the code. Therefore, the MSB of the array element contains a 0 for matching the date and a 1 for matching the day (table 2). This value is read from the array with another <font face=" | + | The hour format will automatically be 24 hours like in the time register because bit 6 stays always 0 (see table 1). However, when the alarm rate is chosen to be either once a week or once a month, i.e. days/date are matched, that has to be changed in the code. Therefore, the MSB of the array element contains a 0 for matching the date and a 1 for matching the day (table 2). This value is read from the array with another |
</li> | </li> | ||
<li> | <li> | ||
- | After configuring the values of the bytes for setting the alarm, another transmission to the RTC is started and the register pointer is set to register <font face=" | + | After configuring the values of the bytes for setting the alarm, another transmission to the RTC is started and the register pointer is set to register |
</li> | </li> | ||
<li> | <li> | ||
- | To activate alarm 1 and enable the interrupt output throug the SQW pin, another transmission is started and the register pointer is set to <font face=" | + | To activate alarm 1 and enable the interrupt output throug the SQW pin, another transmission is started and the register pointer is set to <b><font face=" |
</li> | </li> | ||
<li> | <li> | ||
- | Then the byte <font face=" | + | Then the byte <b><font face=" |
B: 0 - turn on the oscillator; 0 - no square wave output; 0 - no temperature conversion command; 1 - square wave frequency setting - 1 square wave frequency setting; 1 -activate interrupt output; 0 - deactivate alarm 2; 1 - activate alarm 1. | B: 0 - turn on the oscillator; 0 - no square wave output; 0 - no temperature conversion command; 1 - square wave frequency setting - 1 square wave frequency setting; 1 -activate interrupt output; 0 - deactivate alarm 2; 1 - activate alarm 1. | ||
<br> | <br> | ||
Line 384: | Line 403: | ||
<br> | <br> | ||
<li> | <li> | ||
- | Whenever the alarm is activated, the alarm flag bit in the DS3231 status register is switched to a 1 and needs to be reset manually by changing the bit back to a 0. The function <font face=" | + | Whenever the alarm is activated, the alarm flag bit in the DS3231 status register is switched to a 1 and needs to be reset manually by changing the bit back to a 0. The function |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | The last two functions are not used in the sketch but are useful to check whether the DS3231 has a correctly configured time register. The function <font face=" | + | The last two functions are not used in the sketch but are useful to check whether the DS3231 has a correctly configured time register. The function |
</li> | </li> | ||
<li> | <li> | ||
Line 394: | Line 413: | ||
</li> | </li> | ||
<li> | <li> | ||
- | | + | |
</li> | </li> | ||
<li> | <li> | ||
- | The dereferencing operator, the asterisk *, is used to change the value at the address of the pointer variable, in this case the value for the local variables from the <font face=" | + | The dereferencing operator, the asterisk *, is used to change the value at the address of the pointer variable, in this case the value for the local variables from the <b><font face=" |
</li> | </li> | ||
<br> | <br> | ||
<li> | <li> | ||
- | In the <font face=" | + | In the <b><font face=" |
</ | </ | ||
</ol> | </ol> | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | ==== 4.3 Results ==== | ||
+ | |||
+ | When uploading the sketch to the Arduino after connecting it according to the setup, the time of the module is set to Friday, 24th of July 2020, 10:15:00 which can be checked by calling the < | ||
+ | |||
+ | In the example the setting < | ||
+ | |||
+ | The 1 second intervals are of course only chosen to check whether the alarm works properly. As said, the ESP32 is to wake up once an hour, take measurements, | ||
+ | |||
+ | Furthermore, | ||
+ | |||
+ | After programming the alarm and time registers, most of the code will not be needed anymore and can be left out of the final sketch. The ESP32 will only need the I2C library, an interrupt service routine to wake it up, and the < | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <a href=" | ||
+ | <span style=" | ||
+ | Back to the top ⤴ | ||
+ | </ | ||
+ | </a> | ||
+ | </ | ||
+ | </ | ||
</ | </ |
amc2020/group_n/ds3231rtc.txt · Last modified: 2021/08/24 17:35 by 127.0.0.1