M41T56 is a chip of Real Time Clock, which is an analogue of the popular DS1307. And even though the pinout of the microcircuits coincides, they have significant differences, which I will try to tell about.
Short description
I will not stop at the work of the IIC bus, I’ll just note that both chips have the address 0xd0. To work with the time of the chip contain seven registers of the account and the control register. Counting registers contain numbers in binary coded decimal format, however some bits have a special meaning.
Registersxxx - the value of a bit is undefined.
Bitsx - after switching on, the bit state can be any.
Differences begin in the assignment of bits 7, 6 and 5 of the watch register. In M41T56, bits 7 and 6 are used to indicate the transition to the new century, and bits 5 and 4 are used to count dozens of hours. Moreover, the counting hours is possible only in mode 24, AM / PM mode is not available. In DS1307, bit 7 is not used, zero in bit 6 indicates that the count mode is 24, and in this case bits 5 and 4 contain tens of hours. If bit 6 is one, then bit 5 becomes the AM / PM flag, and bit 4 contains dozens of hours.
Significant differences exist in the control register, which contains the word for stroke correction.
')
Stroke correction
M41T56 allows you to compensate for the error of the quartz resonator in the range from -62 to +124 ppm, which gives a deviation of no more than ± 5 seconds per month. For the compensation of the six lower bits of the control register. Bits 4-0 contain an integer without the sign of the correction value, and bit 5 specifies the direction of the correction. If bit 5 contains zero, the course slows down in 2,034 ppm increments; otherwise, the RTC is accelerated in 4,068 ppm increments. This is inconvenient, so I sketched a couple of simple functions to convert from ppm to word correction and back.
#define MASK_CALIBR ((1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0)) #define MASK_CALIBR_SIGN (1 << 5) int8_t caliber_to_ppm(uint8_t caliber) { int8_t result = caliber & MASK_CALIBR; result = (uint8_t) result * 2; if ((caliber & MASK_CALIBR_SIGN) != 0) { result = -result; } else { result = (uint8_t) result * 2; } return result; } uint8_t ppm_to_caliber(int8_t ppm) { uint8_t result; if (ppm < 0) { result = (uint8_t) (-ppm + 1) / 2; result |= MASK_CALIBR_SIGN; } else { result = (uint8_t) (ppm + 2) / 4; } return result; }
Crash Detection
Neither DS1307, but M41T56 are not able to detect generation failures, but they guarantee that when power is turned on, some bits will be in a certain state. M41T56 when enabled in the control register will be 10xxxxxx. To track crashes in a program, you can follow the following algorithm. If when turning on the microcontroller, the RTC control register contains 10xxxxxx, then there was a power failure and the register needs to write a value whose high bits are not 10. The simplest thing is to write the correction word with bits 7 and 6 equal to zero.
Literature