# Features RTC M41T56

M41T56 is a Real Time Clock chip, which is an analogue of the popular DS1307. And although even the pinout of the microcircuit is the same, they have significant differences, which I will try to talk about.

I will not dwell on the operation of the IIC bus, I note just that both chips have the address 0xd0. To work with time, the microcircuit contains seven account registers and a control register. Account registers contain numbers in binary decimal format, however some bits have special meaning.

xxx - the value of the bit is not defined.

x - after switching on, the state of the bit can be any.

The differences begin in the assignment of bits 7, 6 and 5 of the clock register. In the M41T56, bits 7 and 6 are used to indicate the transition to the new century, and bits 5 and 4 are used to count tens of hours. Moreover, hours can be counted only in mode 24, AM / PM mode is not available. In DS1307, bit 7 is not used, a zero in bit 6 indicates that counting mode 24 is used, 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 tens of hours.

Significant differences are in the control register, which contains the word correction stroke.

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 not more than ± 5 seconds per month. For compensation, the six least significant bits of the control register are responsible. Bits 4-0 contain an unsigned integer of the correction value, and bit 5 sets the correction direction. If bit 5 contains zero, then the stroke slows down in increments of 2.034 ppm, otherwise RTC accelerates in increments of 4.068 ppm. This is inconvenient, so I sketched a couple of simple functions for converting from ppm to correction word and vice versa.

Neither DS1307, but M41T56 can detect generation errors, but they guarantee that when the power is turned on, some bits will be in a certain state. M41T56 when enabled in the control register will be 10xxxxxx. To track program crashes, you can use the following algorithm. If, when the microcontroller is turned on, the RTC control register contains 10xxxxxx, then there was a power failure and a value must be written into the register whose high bits are not equal to 10. The simplest thing is to write the correction word with bits 7 and 6 equal to zero.

## Short description

I will not dwell on the operation of the IIC bus, I note just that both chips have the address 0xd0. To work with time, the microcircuit contains seven account registers and a control register. Account registers contain numbers in binary decimal format, however some bits have special meaning.

**Registers**xxx - the value of the bit is not defined.

function | index | bits | possible values | |||||||
---|---|---|---|---|---|---|---|---|---|---|

7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |||

seconds | 0x00 | ST | tens of seconds | units of seconds | 0 - 59 | |||||

minutes | 0x01 | xxx | tens of minutes | units of minutes | 0 - 59 | |||||

clock | 0x02 | Seb | SB | dozens of hours | hours units | 0 - 23 | ||||

day | 0x03 | xxx | xxx | xxx | xxx | xxx | day of the week | 1 - 7 | ||

day of the month | 0x04 | xxx | xxx | dozens of days | units of the day | 1 - (28 | 29 | 30 | 31) | ||||

month | 0x05 | xxx | xxx | xxx | des | units of the month | 1 - 12 | |||

year | 0x06 | tens of years | units of the year | 0 - 99 | ||||||

control register | 0x07 | OUT | FT | S | correction amount | - |

**Bits**x - after switching on, the state of the bit can be any.

bit | after reset | description |
---|---|---|

ST | x | When writing 1 to this bit, the generator stops |

CEB | x | When the CEB (Century Enable Bit) bit is set, then when a new century is reached, the CB (Century Bit) bit will change the value to the opposite. |

CB | x | |

OUT | 1 | When the OUT (OUTput level) and FT (Frequency Test) bits are equal to zero, the FT / OUT pin is pressed to the ground. With FT = 1, the output FT / OUT is 512 Hz and the OUT bit does not matter. |

FT | 0 | |

S | x | RTC stroke calibration mark. |

The differences begin in the assignment of bits 7, 6 and 5 of the clock register. In the M41T56, bits 7 and 6 are used to indicate the transition to the new century, and bits 5 and 4 are used to count tens of hours. Moreover, hours can be counted only in mode 24, AM / PM mode is not available. In DS1307, bit 7 is not used, a zero in bit 6 indicates that counting mode 24 is used, 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 tens of hours.

Significant differences are in the control register, which contains the word correction stroke.

## Course 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 not more than ± 5 seconds per month. For compensation, the six least significant bits of the control register are responsible. Bits 4-0 contain an unsigned integer of the correction value, and bit 5 sets the correction direction. If bit 5 contains zero, then the stroke slows down in increments of 2.034 ppm, otherwise RTC accelerates in increments of 4.068 ppm. This is inconvenient, so I sketched a couple of simple functions for converting from ppm to correction word and vice versa.

```
/* Маска для получения только значения калибровки без знака. */
#define MASK_CALIBR ((1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0))
/* Маска для получения только значения калибровки без знака. */
#define MASK_CALIBR_SIGN (1 << 5)
/**
* Получение значения коррекции хода.
* @param caliber Калибровочное слово из RTC.
* @return Коррекция хода в ppm.
*/
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;
}
/**
* Получение калибровочного слова.
* @param ppm Коррекция хода в ppm.
* @return Калибровочное слово для RTC.
*/
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;
}
```

## Fault detection

Neither DS1307, but M41T56 can detect generation errors, but they guarantee that when the power is turned on, some bits will be in a certain state. M41T56 when enabled in the control register will be 10xxxxxx. To track program crashes, you can use the following algorithm. If, when the microcontroller is turned on, the RTC control register contains 10xxxxxx, then there was a power failure and a value must be written into the register whose high bits are not equal to 10. The simplest thing is to write the correction word with bits 7 and 6 equal to zero.

## Literature

- Fact Sheet M41T56
- DS1307 Data Sheet