|
mcp7940 1.0
mcp7940 driver library
|
Header file with declarations and macros for driving an mcp7940. More...
#include "../../../core_types/format/time.h"#include "../../../utils/systick/systick.h"#include "../../../utils/time/validate.h"#include "../../../utils/macros/stringify.h"#include "../../../hal/avr0/twi/twi.h"

Go to the source code of this file.
Macros | |
| #define | MCP7940_HAL_PLATFORM avr0 |
| Sets the target platform for the MCP7940 hardware abstraction layer (HAL), e.g., avr or avr0. | |
| #define | MCP7940_ADDRESS 0x6F |
| Defines the TWI/I2C address of the MCP7940 device. | |
| #define | MCP7940_USE_EXTOSC |
| Selects external clock input mode for the MCP7940 RTC device. | |
| #define | MCP7940_BATTERY_BACKUP_EN |
| Enables battery backup operation and power-fail time-stamping in the MCP7940 RTC. | |
| #define | MCP7940_MFP_MODE_OUTPUT 0x00 |
| #define | MCP7940_MFP_MODE_SQUARE_WAVE 0x01 |
| #define | MCP7940_MFP_MODE_ALARM 0x02 |
| #define | MCP7940_MFP_MODE MCP7940_MFP_MODE_OUTPUT |
| Selects the operating mode of the MCP7940 multi-function pin (MFP). | |
| #define | MCP7940_MFP_SQUARE_WAVE_PRESCALER MCP7940_SQWFS_1HZ |
| Selects the output frequency of the MCP7940 square-wave signal on the MFP pin. | |
| #define | MCP7940_MFP_ALARM_MODE_ALM0 (MCP7940_ALM0EN_bm) |
| #define | MCP7940_MFP_ALARM_MODE_ALM1 (MCP7940_ALM1EN_bm) |
| #define | MCP7940_MFP_ALARM_MODE_BOTH (MCP7940_ALM1EN_bm | MCP7940_ALM0EN_bm) |
| #define | MCP7940_MFP_ALARM_MODE MCP7940_MFP_ALARM_MODE_BOTH |
| Selects which RTC alarm sources can assert the MCP7940 multi-function pin (MFP). | |
| #define | MCP7940_MFP_ALARM_POLARITY_NORMAL 0x00 |
| #define | MCP7940_MFP_ALARM_POLARITY_INVERTED RTC_ALMPOL_bm |
| #define | MCP7940_MFP_ALARM1_POLARITY MCP7940_MFP_ALARM_POLARITY_NORMAL |
| Configures the output polarity of the MCP7940 multi-function pin (MFP) when driven by Alarm 1 events. | |
| #define | MCP7940_MFP_ALARM2_POLARITY MCP7940_MFP_ALARM_POLARITY_NORMAL |
| Configures the output polarity of the MCP7940 multi-function pin (MFP) when driven by Alarm 2 events. | |
| #define | MCP7940_IO_TIMEOUT_MS 1 |
| Specifies the I/O timeout for MCP7940 bus operations in milliseconds. | |
| #define | MCP7940_OSC_ENABLE_MS 1000UL |
| Delay time in milliseconds before oscillator run check after enabling the MCP7940 oscillator. | |
| #define | MCP7940_RTCSEC 0x00 |
| Address of the MCP7940 RTC seconds register. | |
| #define | MCP7940_ST_bm 0x80 |
| #define | MCP7940_SECTEN_bm 0x70 |
| #define | MCP7940_SECTEN_bp 4 |
| #define | MCP7940_RTCMIN 0x01 |
| Address of the MCP7940 RTC minutes register. | |
| #define | MCP7940_MINTEN_bm 0x70 |
| #define | MCP7940_MINTEN_bp 4 |
| #define | MCP7940_RTCHOUR 0x02 |
| Address of the MCP7940 RTC hours register. | |
| #define | MCP7940_FORMAT_bm 0x40 |
| #define | MCP7940_HRTEN_bm 0x30 |
| #define | MCP7940_HRTEN_bp 4 |
| #define | MCP7940_RTCWKDAY 0x03 |
| Address of the MCP7940 RTC weekday register. | |
| #define | MCP7940_OSCRUN_bm 0x20 |
| #define | MCP7940_PWRFAIL_bm 0x10 |
| #define | MCP7940_VBATEN_bm 0x08 |
| #define | MCP7940_WKDAY_bp 0 |
| #define | MCP7940_RTCDATE 0x04 |
| Address of the MCP7940 RTC date (day-of-month) register. | |
| #define | MCP7940_DATETEN_bm 0x30 |
| #define | MCP7940_DATETEN_bp 4 |
| #define | MCP7940_RTCMTH 0x05 |
| Address of the MCP7940 RTC month register. | |
| #define | MCP7940_LPYR_bm 0x20 |
| #define | MCP7940_MTHTEN_bm 0x10 |
| #define | MCP7940_LPYR_bp 5 |
| #define | MCP7940_MTHTEN_bp 4 |
| #define | MCP7940_RTCYEAR 0x06 |
| Address of the MCP7940 RTC year register. | |
| #define | MCP7940_YRTEN_bm 0xF0 |
| #define | MCP7940_YRTEN_bp 4 |
| #define | MCP7940_CONTROL 0x07 |
| Address of the MCP7940 RTC control register. | |
| #define | MCP7940_OUT_bm 0x80 |
| #define | MCP7940_SQWEN_bm 0x40 |
| #define | MCP7940_ALM1EN_bm 0x20 |
| #define | MCP7940_ALM0EN_bm 0x10 |
| #define | MCP7940_EXTOSC_bm 0x08 |
| #define | MCP7940_CSTRIM_bm 0x04 |
| #define | MCP7940_SQWFS1_bm 0x02 |
| #define | MCP7940_SQWFS0_bm 0x01 |
| #define | MCP7940_SQWFS_32768HZ (MCP7940_SQWFS1_bm | MCP7940_SQWFS0_bm) |
| #define | MCP7940_SQWFS_8192HZ MCP7940_SQWFS1_bm |
| #define | MCP7940_SQWFS_4096HZ MCP7940_SQWFS0_bm |
| #define | MCP7940_SQWFS_1HZ 0x00 |
| #define | MCP7940_OSCTRIM 0x08 |
| Address of the MCP7940 oscillator trim register. | |
| #define | MCP7940_SIGN_bm 0x80 |
| #define | MCP7940_WEEKDAY_MONDAY_gc 0x00 |
| #define | MCP7940_WEEKDAY_TUESDAY_gc 0x01 |
| #define | MCP7940_WEEKDAY_WEDNESDAY_gc 0x02 |
| #define | MCP7940_WEEKDAY_THURSDAY_gc 0x03 |
| #define | MCP7940_WEEKDAY_FRIDAY_gc 0x04 |
| #define | MCP7940_WEEKDAY_SATURDAY_gc 0x05 |
| #define | MCP7940_WEEKDAY_SUNDAY_gc 0x06 |
| #define | MCP7940_ALM_SECTEN_bp 4 |
| #define | MCP7940_ALM_SECONE_bp 0 |
| #define | MCP7940_ALM0SEC 0x0A |
| Address of the MCP7940 Alarm 0 seconds register. | |
| #define | MCP7940_ALM1SEC 0x11 |
| Address of the MCP7940 Alarm 1 seconds register. | |
| #define | MCP7940_ALM_MINTEN_bp 4 |
| #define | MCP7940_ALM_MINONE_bp 0 |
| #define | MCP7940_ALM0MIN 0x0B |
| Address of the MCP7940 Alarm 0 minutes register. | |
| #define | MCP7940_ALM1MIN 0x12 |
| Address of the MCP7940 Alarm 1 minutes register. | |
| #define | MCP7940_ALM_FORMAT_bm 0x40 |
| #define | MCP7940_ALM_HRTEN_bp 4 |
| #define | MCP7940_ALM_HRONE_bp 0 |
| #define | MCP7940_ALM0HOUR 0x0C |
| Address of the MCP7940 Alarm 0 hours register. | |
| #define | MCP7940_ALM1HOUR 0x13 |
| Address of the MCP7940 Alarm 1 hours register. | |
| #define | MCP7940_ALARM_ALMPOL_bm 0x80 |
| #define | MCP7940_ALARM_ALMMSK2_bm 0x40 |
| #define | MCP7940_ALARM_ALMMSK1_bm 0x20 |
| #define | MCP7940_ALARM_ALMMSK0_bm 0x10 |
| #define | MCP7940_ALARM_ALMIF_bm 0x08 |
| #define | MCP7940_ALARM_WKDAY_bp 0 |
| #define | MCP7940_ALARM_ALMMSK_bp 4 |
| #define | MCP7940_ALARM_SECOND_MATCH_gc (0x00<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALARM_MINUTE_MATCH (0x01<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALARM_HOUR_MATCH (0x02<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALARM_DAY_MATCH (0x03<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALARM_DATE_MATCH (0x04<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALARM_FULL_MATCH (0x07<<MCP7940_ALARM_ALMMSK_bp) |
| #define | MCP7940_ALM0WKDAY 0x0D |
| Address of the MCP7940 Alarm 0 weekday register. | |
| #define | MCP7940_ALM1WKDAY 0x14 |
| Address of the MCP7940 Alarm 1 weekday register. | |
| #define | MCP7940_ALM_DATETEN_bp 4 |
| #define | MCP7940_ALM_DATEONE_bp 0 |
| #define | MCP7940_ALM0DATE 0x0E |
| Address of the MCP7940 Alarm 0 date (day-of-month) register. | |
| #define | MCP7940_ALM1DATE 0x15 |
| Address of the MCP7940 Alarm 1 date (day-of-month) register. | |
| #define | MCP7940_ALM_MTHTEN_bp 4 |
| #define | MCP7940_ALM_MTHONE_bp 0 |
| #define | MCP7940_ALM0MTH 0x0F |
| Address of the MCP7940 Alarm 0 month register. | |
| #define | MCP7940_ALM1MTH 0x16 |
| Address of the MCP7940 Alarm 1 month register. | |
| #define | MCP7940_PWRDNMIN 0x18 |
| Address of the MCP7940 power-down minutes timestamp register. | |
| #define | MCP7940_PWRUPMIN 0x1C |
| Address of the MCP7940 power-up minutes timestamp register. | |
| #define | MCP7940_PWRDNHOUR 0x19 |
| Address of the MCP7940 power-down hours timestamp register. | |
| #define | MCP7940_PWRUPHOUR 0x1D |
| Address of the MCP7940 power-up hours timestamp register. | |
| #define | MCP7940_PWRDNDATE 0x1A |
| Address of the MCP7940 power-down date (day-of-month) timestamp register. | |
| #define | MCP7940_PWRUPDATE 0x1E |
| Address of the MCP7940 power-up date (day-of-month) timestamp register. | |
| #define | MCP7940_PWRWEEKDAY_bp 5 |
| #define | MCP7940_PWRDNMTH 0x1B |
| Address of the MCP7940 power-down month timestamp register. | |
| #define | MCP7940_PWRUPMTH 0x1F |
| Address of the MCP7940 power-up month timestamp register. | |
Typedefs | |
| typedef enum MCP7940_Status_t | MCP7940_Status |
| Alias for enum MCP7940_Status_t representing MCP7940 RTC status flags. | |
| typedef enum MCP7940_Error_t | MCP7940_Error |
| Alias for enum MCP7940_Error_t representing MCP7940 driver error codes. | |
| typedef enum MCP7940_Mode_t | MCP7940_Mode |
| Alias for enum MCP7940_Mode_t representing MCP7940 enable/disable modes. | |
| typedef enum MCP7940_Trim_t | MCP7940_Trim |
| Alias for enum MCP7940_Trim_t representing MCP7940 trim direction selection. | |
| typedef enum MCP7940_LeapYear_t | MCP7940_LeapYear |
| Alias for enum MCP7940_LeapYear_t representing MCP7940 leap year status. | |
| typedef enum MCP7940_Register_t | MCP7940_Register |
| Alias for enum MCP7940_Register_t representing MCP7940 register block selections. | |
Enumerations | |
| enum | MCP7940_Status_t { MCP7940_Status_None = 0x00 , MCP7940_Status_Battery_Enabled = 0x08 , MCP7940_Status_Power_Fail = 0x10 , MCP7940_Status_Oscillator_Running = 0x20 } |
| Represents status and configuration flags of the MCP7940 RTC. More... | |
| enum | MCP7940_Error_t { MCP7940_Error_None = 0 , MCP7940_Error_Fail } |
| Represents error conditions reported by the MCP7940 driver. More... | |
| enum | MCP7940_Mode_t { MCP7940_Mode_Disable = 0 , MCP7940_Mode_Enable } |
| Selects enabled or disabled mode for MCP7940 features. More... | |
| enum | MCP7940_Trim_t { MCP7940_Trim_Substract = 0 , MCP7940_Trim_Add } |
| Selects the trim direction for MCP7940 oscillator calibration. More... | |
| enum | MCP7940_LeapYear_t { MCP7940_LeapYear_False = 0 , MCP7940_LeapYear_True } |
| Indicates whether the MCP7940 calendar year is a leap year. More... | |
| enum | MCP7940_Register_t { MCP7940_Register_Current_Time = 0 , MCP7940_Register_Power_Down_Time , MCP7940_Register_Power_Up_Time } |
| Selects MCP7940 time-stamp register blocks for access operations. More... | |
Functions | |
| void | mcp7940_init (void) |
| Initializes the MCP7940 RTC with battery backup, MFP mode, and oscillator settings. | |
| MCP7940_Error | mcp7940_trimming (MCP7940_Trim mode, unsigned char value) |
| Configures the MCP7940 oscillator trimming value and verifies the write. | |
| void | mcp7940_oscillator (MCP7940_Mode mode) |
| Enables or disables the MCP7940 RTC oscillator or external clock input. | |
| MCP7940_Status | mcp7940_status (void) |
| Reads and returns the current MCP7940 status flags from the weekday register. | |
| void | mcp7940_mfp_output (MCP7940_Mode output) |
| Controls the MCP7940 MFP pin when configured as a general-purpose output. | |
| const char * | mcp7940_weekday_string (unsigned char day) |
| Returns a three-letter weekday abbreviation for a given day value. | |
| unsigned char | mcp7940_weekday (MCP7940_Register data) |
| Reads the weekday value from the MCP7940 for the selected timestamp register set. | |
| void | mcp7940_time (FORMAT_Time *time, MCP7940_Register reg) |
| Reads hour, minute, and second fields from the MCP7940 into a FORMAT_Time structure. | |
| void | mcp7940_date (FORMAT_Date *date, MCP7940_Register reg) |
| Reads day, month, and year fields from the MCP7940 into a FORMAT_Date structure. | |
| void | mcp7940_datetime (FORMAT_DateTime *datetime, MCP7940_Register reg) |
| Reads both time and date from the MCP7940 into a FORMAT_DateTime structure. | |
| MCP7940_LeapYear | mcp7940_leapyear (void) |
| Returns the current leap year status from the MCP7940 device. | |
| MCP7940_Error | mcp7940_setweekday (unsigned char weekday) |
| Sets the MCP7940 weekday field in the RTCWKDAY register. | |
| MCP7940_Error | mcp7940_settime (const FORMAT_Time *time) |
| Sets the current time of the MCP7940 RTC from a FORMAT_Time structure. | |
| MCP7940_Error | mcp7940_setdate (const FORMAT_Date *date) |
| Sets the current calendar date of the MCP7940 RTC from a FORMAT_Date structure. | |
| MCP7940_Error | mcp7940_setdatetime (const FORMAT_DateTime *datetime) |
| Sets the current MCP7940 date and time from a FORMAT_DateTime structure. | |
Header file with declarations and macros for driving an mcp7940.
This file provides function prototypes, type definitions, and constants for communication with an mcp7940 rtc chip.
| #define MCP7940_ADDRESS 0x6F |
Defines the TWI/I2C address of the MCP7940 device.
This macro specifies the 7-bit I2C slave address used to communicate with the MCP7940 device on the TWI/I2C bus. The value can be overridden by defining MCP7940_ADDRESS before including this header if the hardware configuration uses a different address.
| #define MCP7940_ALARM_ALMIF_bm 0x08 |
Bit mask for the ALMIF flag indicating that an alarm event has occurred.
| #define MCP7940_ALARM_ALMMSK0_bm 0x10 |
Bit mask for the ALMMSK0 bit, part of the alarm match mask field.
| #define MCP7940_ALARM_ALMMSK1_bm 0x20 |
Bit mask for the ALMMSK1 bit, part of the alarm match mask field.
| #define MCP7940_ALARM_ALMMSK2_bm 0x40 |
Bit mask for the ALMMSK2 bit, part of the alarm match mask field.
| #define MCP7940_ALARM_ALMMSK_bp 4 |
Bit position of the least significant bit of the alarm match mask (ALMMSK) field.
| #define MCP7940_ALARM_ALMPOL_bm 0x80 |
Bit mask for the ALMPOL bit controlling the active polarity of the alarm output on MFP.
| #define MCP7940_ALARM_DATE_MATCH (0x04<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: match on date, hours, minutes, and seconds.
| #define MCP7940_ALARM_DAY_MATCH (0x03<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: match on weekday, hours, minutes, and seconds.
| #define MCP7940_ALARM_FULL_MATCH (0x07<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: full match on all alarm time/date fields.
| #define MCP7940_ALARM_HOUR_MATCH (0x02<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: match on hours, minutes, and seconds.
| #define MCP7940_ALARM_MINUTE_MATCH (0x01<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: match on minutes and seconds.
| #define MCP7940_ALARM_SECOND_MATCH_gc (0x00<<MCP7940_ALARM_ALMMSK_bp) |
Alarm match mode: match on seconds only.
| #define MCP7940_ALARM_WKDAY_bp 0 |
Bit position of the least significant bit of the weekday BCD field.
| #define MCP7940_ALM0DATE 0x0E |
Address of the MCP7940 Alarm 0 date (day-of-month) register.
This macro defines the register address of the Alarm 0 date register, which stores the day-of-month value for Alarm 0 in BCD format as part of the alarm time/date configuration.
| #define MCP7940_ALM0EN_bm 0x10 |
Bit mask for the ALM0EN bit, enabling Alarm 0 to assert the MFP pin.
| #define MCP7940_ALM0HOUR 0x0C |
Address of the MCP7940 Alarm 0 hours register.
This macro defines the register address of the Alarm 0 hours register, which stores the hours value for Alarm 0 in BCD format and includes the hour format selection bit as part of the alarm time configuration.
| #define MCP7940_ALM0MIN 0x0B |
Address of the MCP7940 Alarm 0 minutes register.
This macro defines the register address of the Alarm 0 minutes register, which stores the minutes value for Alarm 0 in BCD format as part of the alarm time configuration.
| #define MCP7940_ALM0MTH 0x0F |
Address of the MCP7940 Alarm 0 month register.
This macro defines the register address of the Alarm 0 month register, which stores the month value for Alarm 0 in BCD format as part of the alarm time/date configuration.
| #define MCP7940_ALM0SEC 0x0A |
Address of the MCP7940 Alarm 0 seconds register.
This macro defines the register address of the Alarm 0 seconds register, which stores the seconds value for Alarm 0 in BCD format.
| #define MCP7940_ALM0WKDAY 0x0D |
Address of the MCP7940 Alarm 0 weekday register.
This macro defines the register address of the Alarm 0 weekday register, which stores the weekday value and alarm control bits (polarity, match mode, and interrupt flag) for Alarm 0.
| #define MCP7940_ALM1DATE 0x15 |
Address of the MCP7940 Alarm 1 date (day-of-month) register.
This macro defines the register address of the Alarm 1 date register, which stores the day-of-month value for Alarm 1 in BCD format as part of the alarm time/date configuration.
| #define MCP7940_ALM1EN_bm 0x20 |
Bit mask for the ALM1EN bit, enabling Alarm 1 to assert the MFP pin.
| #define MCP7940_ALM1HOUR 0x13 |
Address of the MCP7940 Alarm 1 hours register.
This macro defines the register address of the Alarm 1 hours register, which stores the hours value for Alarm 1 in BCD format and includes the hour format selection bit as part of the alarm time configuration.
| #define MCP7940_ALM1MIN 0x12 |
Address of the MCP7940 Alarm 1 minutes register.
This macro defines the register address of the Alarm 1 minutes register, which stores the minutes value for Alarm 1 in BCD format as part of the alarm time configuration.
| #define MCP7940_ALM1MTH 0x16 |
Address of the MCP7940 Alarm 1 month register.
This macro defines the register address of the Alarm 1 month register, which stores the month value for Alarm 1 in BCD format as part of the alarm time/date configuration.
| #define MCP7940_ALM1SEC 0x11 |
Address of the MCP7940 Alarm 1 seconds register.
This macro defines the register address of the Alarm 1 seconds register, which stores the seconds value for Alarm 1 in BCD format.
| #define MCP7940_ALM1WKDAY 0x14 |
Address of the MCP7940 Alarm 1 weekday register.
This macro defines the register address of the Alarm 1 weekday register, which stores the weekday value and alarm control bits (polarity, match mode, and interrupt flag) for Alarm 1.
| #define MCP7940_ALM_DATEONE_bp 0 |
Bit position of the least significant bit of the ones-of-day BCD field in the alarm date registers.
| #define MCP7940_ALM_DATETEN_bp 4 |
Bit position of the least significant bit of the tens-of-day BCD field in the alarm date registers.
| #define MCP7940_ALM_FORMAT_bm 0x40 |
Bit mask for the hour format selection bit (12/24-hour mode) in the alarm hours registers.
| #define MCP7940_ALM_HRONE_bp 0 |
Bit position of the least significant bit of the ones-of-hours BCD field in the alarm hours registers.
| #define MCP7940_ALM_HRTEN_bp 4 |
Bit position of the least significant bit of the tens-of-hours BCD field in the alarm hours registers.
| #define MCP7940_ALM_MINONE_bp 0 |
Bit position of the least significant bit of the ones-of-minutes BCD field in the alarm minutes registers.
| #define MCP7940_ALM_MINTEN_bp 4 |
Bit position of the least significant bit of the tens-of-minutes BCD field in the alarm minutes registers.
| #define MCP7940_ALM_MTHONE_bp 0 |
Bit position of the least significant bit of the ones-of-month BCD field in the alarm month registers.
| #define MCP7940_ALM_MTHTEN_bp 4 |
Bit position of the least significant bit of the tens-of-month BCD field in the alarm month registers.
| #define MCP7940_ALM_SECONE_bp 0 |
Bit position of the least significant bit of the ones-of-seconds BCD field in the alarm seconds registers.
| #define MCP7940_ALM_SECTEN_bp 4 |
Bit position of the least significant bit of the tens-of-seconds BCD field in the alarm seconds registers.
| #define MCP7940_BATTERY_BACKUP_EN |
Enables battery backup operation and power-fail time-stamping in the MCP7940 RTC.
When this macro is defined, the driver enables the VBAT battery backup feature of the MCP7940 by setting the VBATEN bit in the RTC weekday register. This allows the device to automatically switch to the VBAT supply on main power loss, maintain timekeeping and RTC/SRAM contents, and record power-down and power-up timestamps in the dedicated PWRDN and PWRUP registers.
If this macro is not defined, the VBAT pin is treated as unused (or must be tied to GND), and the battery backup and power-fail logging features are disabled to minimize leakage current.
| #define MCP7940_CONTROL 0x07 |
Address of the MCP7940 RTC control register.
This macro defines the register address of the control register, which provides control bits for the MFP output, square-wave generation, alarm enables, oscillator source selection, and coarse trimming.
| #define MCP7940_CSTRIM_bm 0x04 |
Bit mask for the CSTRIM bit, enabling coarse trimming of the oscillator.
| #define MCP7940_DATETEN_bm 0x30 |
Bit mask for the tens-of-day BCD field in the date register.
| #define MCP7940_DATETEN_bp 4 |
Bit position of the least significant bit of the tens-of-day field.
| #define MCP7940_EXTOSC_bm 0x08 |
Bit mask for the EXTOSC bit, selecting an external clock input instead of the crystal.
| #define MCP7940_FORMAT_bm 0x40 |
Bit mask for the hour format selection bit (12/24-hour mode) in the hours register.
| #define MCP7940_HAL_PLATFORM avr0 |
Sets the target platform for the MCP7940 hardware abstraction layer (HAL), e.g., avr or avr0.
Define this macro with the name of the target platform to select the corresponding platform-specific HAL implementation (such as TWI communication functions) for the MCP7940 RTC driver. Common values are avr (classic AVR architecture) or avr0 (AVR0/1 series).
| #define MCP7940_HRTEN_bm 0x30 |
Bit mask for the tens-of-hours BCD field in the hours register.
| #define MCP7940_HRTEN_bp 4 |
Bit position of the least significant bit of the tens-of-hours field.
| #define MCP7940_IO_TIMEOUT_MS 1 |
Specifies the I/O timeout for MCP7940 bus operations in milliseconds.
This macro defines the maximum time the driver will wait for a TWI/I2C transaction with the MCP7940 device to complete before treating it as a timeout condition. It can be adjusted to match the timing requirements and performance characteristics of the target platform and bus speed.
| #define MCP7940_LPYR_bm 0x20 |
Bit mask for the leap-year indicator (LPYR) in the month register.
| #define MCP7940_LPYR_bp 5 |
Bit position of the leap-year indicator (LPYR) in the month register.
| #define MCP7940_MFP_ALARM1_POLARITY MCP7940_MFP_ALARM_POLARITY_NORMAL |
Configures the output polarity of the MCP7940 multi-function pin (MFP) when driven by Alarm 1 events.
This macro determines whether the MFP pin is asserted in an active-high or active-low manner when Alarm 1 conditions occur. It is defined in terms of the available polarity selector macros and allows for flexible integration with different types of interrupt inputs or signaling requirements.
The following polarity values are available:
| #define MCP7940_MFP_ALARM2_POLARITY MCP7940_MFP_ALARM_POLARITY_NORMAL |
Configures the output polarity of the MCP7940 multi-function pin (MFP) when driven by Alarm 2 events.
This macro determines whether the MFP pin is asserted in an active-high or active-low manner when Alarm 2 conditions occur. It is defined in terms of the available polarity selector macros and allows for flexible integration with different types of interrupt inputs or signaling requirements.
The following polarity values are available:
| #define MCP7940_MFP_ALARM_MODE MCP7940_MFP_ALARM_MODE_BOTH |
Selects which RTC alarm sources can assert the MCP7940 multi-function pin (MFP).
This macro configures the alarm routing for the MFP pin when it is used in alarm/interrupt mode. It is defined in terms of the available alarm mode selector macros and determines whether Alarm 0, Alarm 1, or both alarms are allowed to drive the MFP pin.
The following mode values are available:
| #define MCP7940_MFP_ALARM_MODE_ALM0 (MCP7940_ALM0EN_bm) |
| #define MCP7940_MFP_ALARM_MODE_ALM1 (MCP7940_ALM1EN_bm) |
| #define MCP7940_MFP_ALARM_MODE_BOTH (MCP7940_ALM1EN_bm | MCP7940_ALM0EN_bm) |
| #define MCP7940_MFP_ALARM_POLARITY_INVERTED RTC_ALMPOL_bm |
| #define MCP7940_MFP_ALARM_POLARITY_NORMAL 0x00 |
| #define MCP7940_MFP_MODE MCP7940_MFP_MODE_OUTPUT |
Selects the operating mode of the MCP7940 multi-function pin (MFP).
This macro specifies how the MFP pin of the MCP7940 RTC is used by the driver. Depending on the selected value, the pin can be disabled and used as a general-purpose output, configured as a square-wave (CLKOUT) output, or used as an alarm/interrupt output driven by the internal alarm logic.
The following mode values are available:
| #define MCP7940_MFP_MODE_ALARM 0x02 |
| #define MCP7940_MFP_MODE_OUTPUT 0x00 |
| #define MCP7940_MFP_MODE_SQUARE_WAVE 0x01 |
| #define MCP7940_MFP_SQUARE_WAVE_PRESCALER MCP7940_SQWFS_1HZ |
Selects the output frequency of the MCP7940 square-wave signal on the MFP pin.
This macro defines the prescaler setting used to generate the square-wave (CLKOUT) output on the MFP pin when coarse trimming support (MCP7940_CRSTRIM_EN) is disabled. It determines the nominal frequency of the square-wave derived from the 32.768 kHz time base and is applied when the MFP pin is configured for square-wave mode.
The following prescaler values are available:
| #define MCP7940_MINTEN_bm 0x70 |
Bit mask for the tens-of-minutes BCD field in the minutes register.
| #define MCP7940_MINTEN_bp 4 |
Bit position of the least significant bit of the tens-of-minutes field.
| #define MCP7940_MTHTEN_bm 0x10 |
Bit mask for the tens-of-month BCD field in the month register.
| #define MCP7940_MTHTEN_bp 4 |
Bit position of the least significant bit of the tens-of-month field.
| #define MCP7940_OSC_ENABLE_MS 1000UL |
Delay time in milliseconds before oscillator run check after enabling the MCP7940 oscillator.
Specifies the period after enabling the oscillator (ST/EXTOSC) before the OSCRUN signals that the MCP7940 oscillator is running. During this delay, the device needs time to complete the required number of clock cycles before the status bit (OSCRUN) is asserted.
The MCP7940 requires approximately 32 cycles of the 32.768 kHz time base before OSCRUN goes high, so MCP7940_OSC_ENABLE_MS must be at least (32 / 32768) s ≈ 0.98 ms. Choosing a slightly larger value (for example 1–2 ms) provides additional margin for crystal start-up variation.
| #define MCP7940_OSCRUN_bm 0x20 |
Bit mask for the OSCRUN flag indicating that the oscillator is running.
| #define MCP7940_OSCTRIM 0x08 |
Address of the MCP7940 oscillator trim register.
This macro defines the register address of the oscillator trim register, which is used to apply fine or coarse trimming adjustments to the RTC oscillator frequency.
| #define MCP7940_OUT_bm 0x80 |
Bit mask for the OUT control bit, which drives the MFP output level in output mode.
| #define MCP7940_PWRDNDATE 0x1A |
Address of the MCP7940 power-down date (day-of-month) timestamp register.
This macro defines the register address of the date field in the power-down timestamp, which stores the day-of-month value (in BCD format) recorded when the main power supply was lost.
| #define MCP7940_PWRDNHOUR 0x19 |
Address of the MCP7940 power-down hours timestamp register.
This macro defines the register address of the hours field in the power-down timestamp, which stores the hours value (in BCD format) recorded when the main power supply was lost.
| #define MCP7940_PWRDNMIN 0x18 |
Address of the MCP7940 power-down minutes timestamp register.
This macro defines the register address of the minutes field in the power-down timestamp, which stores the minutes value (in BCD format) recorded when the main power supply was lost.
| #define MCP7940_PWRDNMTH 0x1B |
Address of the MCP7940 power-down month timestamp register.
This macro defines the register address of the month field in the power-down timestamp, which stores the month value (in BCD format) and weekday information recorded when the main power supply was lost.
| #define MCP7940_PWRFAIL_bm 0x10 |
Bit mask for the PWRFAIL flag indicating that a power failure has occurred.
| #define MCP7940_PWRUPDATE 0x1E |
Address of the MCP7940 power-up date (day-of-month) timestamp register.
This macro defines the register address of the date field in the power-up timestamp, which stores the day-of-month value (in BCD format) recorded when the main power supply was restored.
| #define MCP7940_PWRUPHOUR 0x1D |
Address of the MCP7940 power-up hours timestamp register.
This macro defines the register address of the hours field in the power-up timestamp, which stores the hours value (in BCD format) recorded when the main power supply was restored.
| #define MCP7940_PWRUPMIN 0x1C |
Address of the MCP7940 power-up minutes timestamp register.
This macro defines the register address of the minutes field in the power-up timestamp, which stores the minutes value (in BCD format)recorded when the main power supply was restored.
| #define MCP7940_PWRUPMTH 0x1F |
Address of the MCP7940 power-up month timestamp register.
This macro defines the register address of the month field in the power-up timestamp, which stores the month value (in BCD format) and weekday information recorded when the main power supply was restored.
| #define MCP7940_PWRWEEKDAY_bp 5 |
Bit position of the least significant bit of the weekday field in the power-fail timestamp month registers.
| #define MCP7940_RTCDATE 0x04 |
Address of the MCP7940 RTC date (day-of-month) register.
This macro defines the register address of the date register, which stores the current day-of-month value in BCD format.
| #define MCP7940_RTCHOUR 0x02 |
Address of the MCP7940 RTC hours register.
This macro defines the register address of the hours register, which stores the current hours value in BCD format and includes the hour format selection bit (12/24-hour mode).
| #define MCP7940_RTCMIN 0x01 |
Address of the MCP7940 RTC minutes register.
This macro defines the register address of the minutes register, which stores the current minutes value in BCD format.
| #define MCP7940_RTCMTH 0x05 |
Address of the MCP7940 RTC month register.
This macro defines the register address of the month register, which stores the current month value in BCD format and includes the leap-year indicator bit.
| #define MCP7940_RTCSEC 0x00 |
Address of the MCP7940 RTC seconds register.
This macro defines the register address of the seconds register, which stores the current seconds value in BCD format and contains control bits related to the RTC oscillator.
| #define MCP7940_RTCWKDAY 0x03 |
Address of the MCP7940 RTC weekday register.
This macro defines the register address of the weekday register, which stores the current day of week and several status/control flags related to oscillator state, power failure detection, and battery backup.
| #define MCP7940_RTCYEAR 0x06 |
Address of the MCP7940 RTC year register.
This macro defines the register address of the year register, which stores the current year value (last two digits) in BCD format.
| #define MCP7940_SECTEN_bm 0x70 |
Bit mask for the tens-of-seconds BCD field in the seconds register.
| #define MCP7940_SECTEN_bp 4 |
Bit position of the least significant bit of the tens-of-seconds field.
| #define MCP7940_SIGN_bm 0x80 |
Bit mask for the SIGN bit indicating the trim direction (positive or negative adjustment).
| #define MCP7940_SQWEN_bm 0x40 |
Bit mask for the SQWEN bit, enabling or disabling the square-wave output on MFP.
| #define MCP7940_SQWFS0_bm 0x01 |
Bit mask for the SQWFS0 bit, low bit of the square-wave frequency select field.
| #define MCP7940_SQWFS1_bm 0x02 |
Bit mask for the SQWFS1 bit, high bit of the square-wave frequency select field.
| #define MCP7940_SQWFS_1HZ 0x00 |
Square-wave frequency select: 1 Hz output.
| #define MCP7940_SQWFS_32768HZ (MCP7940_SQWFS1_bm | MCP7940_SQWFS0_bm) |
Square-wave frequency select: 32.768 kHz output.
| #define MCP7940_SQWFS_4096HZ MCP7940_SQWFS0_bm |
Square-wave frequency select: 4.096 kHz output.
| #define MCP7940_SQWFS_8192HZ MCP7940_SQWFS1_bm |
Square-wave frequency select: 8.192 kHz output.
| #define MCP7940_ST_bm 0x80 |
Oscillator start bit mask (ST) in the seconds register.
| #define MCP7940_USE_EXTOSC |
Selects external clock input mode for the MCP7940 RTC device.
This macro enables operation of the MCP7940 using an external 32.768 kHz clock signal applied to the X1 pin instead of the crystal oscillator. When this macro is defined, the driver configures the device to use the external clock input; when it is not defined, the MCP7940 is assumed to run from a standard external 32.768 kHz crystal connected to the oscillator pins (X1/X2).
| #define MCP7940_VBATEN_bm 0x08 |
Bit mask for the VBATEN flag enabling battery-backed timekeeping.
| #define MCP7940_WEEKDAY_FRIDAY_gc 0x04 |
Encoded weekday value for Friday
| #define MCP7940_WEEKDAY_MONDAY_gc 0x00 |
Encoded weekday value for Monday
| #define MCP7940_WEEKDAY_SATURDAY_gc 0x05 |
Encoded weekday value for Saturday
| #define MCP7940_WEEKDAY_SUNDAY_gc 0x06 |
Encoded weekday value for Sunday
| #define MCP7940_WEEKDAY_THURSDAY_gc 0x03 |
Encoded weekday value for Thursday
| #define MCP7940_WEEKDAY_TUESDAY_gc 0x01 |
Encoded weekday value for Tuesday
| #define MCP7940_WEEKDAY_WEDNESDAY_gc 0x02 |
Encoded weekday value for Wednesday
| #define MCP7940_WKDAY_bp 0 |
Bit position of the least significant bit of the weekday BCD field.
| #define MCP7940_YRTEN_bm 0xF0 |
Bit mask for the tens-of-years BCD field in the year register.
| #define MCP7940_YRTEN_bp 4 |
Bit position of the least significant bit of the tens-of-years field.
| typedef enum MCP7940_Error_t MCP7940_Error |
Alias for enum MCP7940_Error_t representing MCP7940 driver error codes.
| typedef enum MCP7940_LeapYear_t MCP7940_LeapYear |
Alias for enum MCP7940_LeapYear_t representing MCP7940 leap year status.
| typedef enum MCP7940_Mode_t MCP7940_Mode |
Alias for enum MCP7940_Mode_t representing MCP7940 enable/disable modes.
| typedef enum MCP7940_Register_t MCP7940_Register |
Alias for enum MCP7940_Register_t representing MCP7940 register block selections.
| typedef enum MCP7940_Status_t MCP7940_Status |
Alias for enum MCP7940_Status_t representing MCP7940 RTC status flags.
| typedef enum MCP7940_Trim_t MCP7940_Trim |
Alias for enum MCP7940_Trim_t representing MCP7940 trim direction selection.
| enum MCP7940_Error_t |
Represents error conditions reported by the MCP7940 driver.
This enumeration defines generic error codes used by the MCP7940 access routines. It currently distinguishes between the absence of an error and a generic failure condition, which may cover I2C communication issues, invalid parameters, or unexpected device responses. Additional error codes can be added as the driver implementation is extended.
| Enumerator | |
|---|---|
| MCP7940_Error_None | No error occurred, operation completed successfully |
| MCP7940_Error_Fail | A generic failure occurred during an MCP7940 operation |
| enum MCP7940_LeapYear_t |
Indicates whether the MCP7940 calendar year is a leap year.
This enumeration corresponds to the leap year indication used by the MCP7940 RTC calendar logic. It can be used to represent or interpret the device's internal leap year status bit, which affects how the device handles the length of February in its automatic datekeeping.
| Enumerator | |
|---|---|
| MCP7940_LeapYear_False | Year is not a leap year (LPYR bit cleared) |
| MCP7940_LeapYear_True | Year is a leap year (LPYR bit set) |
| enum MCP7940_Mode_t |
Selects enabled or disabled mode for MCP7940 features.
This enumeration provides simple enable/disable selections used by MCP7940 driver functions, for example when configuring the oscillator, alarms, or battery backup-related options. It is intended as a generic mode selector to make function calls more readable than using raw boolean or integer values.
| Enumerator | |
|---|---|
| MCP7940_Mode_Disable | Feature or function is disabled |
| MCP7940_Mode_Enable | Feature or function is enabled |
| enum MCP7940_Register_t |
Selects MCP7940 time-stamp register blocks for access operations.
This enumeration identifies the different time-related register sets within the MCP7940 device that can be read from or written to by the driver. It allows higher-level code to specify whether the current time/calendar registers, the power-down time-stamp registers, or the power-up time-stamp registers should be addressed in an access operation.
| enum MCP7940_Status_t |
Represents status and configuration flags of the MCP7940 RTC.
This enumeration defines bit flags derived from the MCP7940 RTC status/control register. The flags indicate whether the battery backup is enabled, whether a power-fail event has been detected, and whether the oscillator is currently running. The value MCP7940_Status_None can be used when no status flags are set or when no status information is available.
| enum MCP7940_Trim_t |
Selects the trim direction for MCP7940 oscillator calibration.
This enumeration specifies whether the MCP7940 digital trimming logic should add or subtract clock cycles when writing to the oscillator trim register. Adding clocks is typically used to correct for a slow-running oscillator, while subtracting clocks compensates for a fast-running oscillator, as described in the MCP7940 datasheet and trimming guides.
| Enumerator | |
|---|---|
| MCP7940_Trim_Substract | Subtract clock cycles to correct for a fast-running clock |
| MCP7940_Trim_Add | Add clock cycles to correct for a slow-running clock |
| void mcp7940_date | ( | FORMAT_Date * | date, |
| MCP7940_Register | reg ) |
Reads day, month, and year fields from the MCP7940 into a FORMAT_Date structure.
| date | Pointer to a FORMAT_Date structure that will be populated with calendar information from the MCP7940 registers. On return:
|
| reg | Selects which MCP7940 register set to read, using a value from MCP7940_Register:
|
This function uses helper routines (mcp7940_day(), mcp7940_month(), and optionally mcp7940_year()) to extract the calendar date fields from the selected MCP7940 time or timestamp registers and write them into date. For the current time selection, the year field is read from the device via mcp7940_year(). For power-down and power-up timestamp registers, the year field is not read and date->year is set to 0, as this implementation does not associate a stored year with those timestamp records.

| void mcp7940_datetime | ( | FORMAT_DateTime * | datetime, |
| MCP7940_Register | reg ) |
Reads both time and date from the MCP7940 into a FORMAT_DateTime structure.
| datetime | Pointer to a FORMAT_DateTime structure that will be filled with the time and date fields obtained from the MCP7940 registers corresponding to reg. On return, datetime->time and datetime->date contain the decoded values. |
| reg | Selects which MCP7940 register set to read, using a value from MCP7940_Register:
|
This function is a convenience wrapper that combines mcp7940_time() and mcp7940_date() to populate a FORMAT_DateTime instance in a single call. It first reads the time portion into datetime->time, then reads the date portion into datetime->date, using the same reg selection for both operations to keep time and date information consistent.

| void mcp7940_init | ( | void | ) |
Initializes the MCP7940 RTC with battery backup, MFP mode, and oscillator settings.
This function configures the MCP7940 device according to the compile-time configuration macros. It first enables or disables the battery backup feature using mcp7940_battery() depending on MCP7940_BATTERY_BACKUP_EN. It then reads the current CONTROL register, preserves the EXTOSC bit, and updates control flags related to coarse trimming (MCP7940_CSTRIM_bm), square-wave output and prescaler (MCP7940_SQWEN_bm and MCP7940_MFP_SQUARE_WAVE_PRESCALER) or alarm mode (MCP7940_MFP_ALARM_MODE), depending on MCP7940_SQW_CRSTRIM_EN and MCP7940_MFP_MODE. Finally, it enables the RTC oscillator via mcp7940_oscillator(), allowing the device to begin timekeeping.
< Bit mask for the EXTOSC bit, selecting an external clock input instead of the crystal.

| MCP7940_LeapYear mcp7940_leapyear | ( | void | ) |
Returns the current leap year status from the MCP7940 device.
This function reads the leap-year indicator bits from the MCP7940 and masks and shifts them into the MCP7940_LeapYear enumeration domain. The returned value reflects the RTC’s internal leap-year status, which influences how February 29 is handled in the device’s calendar logic.
< Bit mask for the leap-year indicator (LPYR) in the month register.
< Bit position of the leap-year indicator (LPYR) in the month register.
| void mcp7940_mfp_output | ( | MCP7940_Mode | output | ) |
Controls the MCP7940 MFP pin when configured as a general-purpose output.
| output | Desired output mode for the MFP pin, using a value from MCP7940_Mode:
|
This function is available only when MCP7940_MFP_MODE is set to MCP7940_MFP_MODE_OUTPUT at compile time. In this mode, the MFP pin behaves as an open-drain general-purpose output controlled by the OUT bit in the CONTROL register. The function reads the current CONTROL value, sets or clears the OUT bit according to output, and writes the updated value back, preserving all other control bits.
< Bit mask for the OUT control bit, which drives the MFP output level in output mode.
< Bit mask for the OUT control bit, which drives the MFP output level in output mode.
| void mcp7940_oscillator | ( | MCP7940_Mode | mode | ) |
Enables or disables the MCP7940 RTC oscillator or external clock input.
| mode | Selects the desired oscillator mode, using a value from MCP7940_Mode:
|
Depending on the compile-time configuration, this function controls either the external oscillator input (MCP7940_USE_EXTOSC defined) by setting or clearing the EXTOSC bit in the CONTROL register, or the internal RTC oscillator by setting or clearing the ST (start oscillator) bit in the seconds register RTCSEC. The corresponding register is read first and then updated with the appropriate bit set or cleared while preserving the other bits.
< Bit mask for the EXTOSC bit, selecting an external clock input instead of the crystal.
< Bit mask for the EXTOSC bit, selecting an external clock input instead of the crystal.

| MCP7940_Error mcp7940_setdate | ( | const FORMAT_Date * | date | ) |
Sets the current calendar date of the MCP7940 RTC from a FORMAT_Date structure.
| date | Pointer to a FORMAT_Date structure containing the date to be set. The following fields are used:
|
This function first validates the date fields using validate_date(). If validation fails, it returns MCP7940_Error_Fail immediately. Otherwise, it writes the day, month, and year values to the MCP7940 RTCDATE, RTCMTH, and RTCYEAR registers via mcp7940_setdata(). The implementation assumes that any required conversion to the device’s register format (for example, BCD encoding and leap-year bit handling) is performed inside mcp7940_setdata() or the lower-level I2C access functions.


| MCP7940_Error mcp7940_setdatetime | ( | const FORMAT_DateTime * | datetime | ) |
Sets the current MCP7940 date and time from a FORMAT_DateTime structure.
| datetime | Pointer to a FORMAT_DateTime structure containing both time and date components to be written to the MCP7940. The following subfields are used:
|
This function is a convenience wrapper that programs both the current time and calendar date of the MCP7940 in one call. It first calls mcp7940_settime() with datetime->time and then mcp7940_setdate() with datetime->date. The individual error codes are bitwise OR-combined and returned to the caller, allowing detection of failures in either step.

| MCP7940_Error mcp7940_settime | ( | const FORMAT_Time * | time | ) |
Sets the current time of the MCP7940 RTC from a FORMAT_Time structure.
| time | Pointer to a FORMAT_Time structure containing the time to be set. The following fields are used:
|
This function first validates the time fields using validate_time(). If validation fails, it returns MCP7940_Error_Fail immediately. Otherwise, it writes the hour, minute, and second values to the MCP7940 RTCHOUR, RTCMIN, and RTCSEC registers using mcp7940_setdata(), then enables the RTC oscillator via mcp7940_oscillator() so that timekeeping starts or continues from the new value. The implementation assumes that conversion to the device’s register format (for example BCD) is handled inside mcp7940_setdata().


| MCP7940_Error mcp7940_setweekday | ( | unsigned char | weekday | ) |
Sets the MCP7940 weekday field in the RTCWKDAY register.
| weekday | Zero-based weekday index to be written to the device. Valid values are:
|
Values greater than or equal to 7 are rejected.
weekday is within range and the RTCWKDAY register was updated.weekday is out of range (>= 7) and the register is not modified.This function programs the weekday field of the MCP7940 RTCWKDAY register. The MCP7940 encodes weekday values in the range 1–7, so the provided zero-based weekday is incremented by 1 and masked with 0x07 before being written. The existing upper bits of RTCWKDAY (such as VBATEN, PWRFAIL, and OSCRUN) are preserved by masking with 0xF8 and OR-ing in the new weekday value.
| MCP7940_Status mcp7940_status | ( | void | ) |
Reads and returns the current MCP7940 status flags from the weekday register.
If none of these bits are set, the function returns MCP7940_Status_None.
This function reads the RTCWKDAY register of the MCP7940 and masks out the OSCRUN, PWRFAIL, and VBATEN bits to construct an MCP7940_Status value. The resulting status can be used by higher-level code to determine whether the RTC oscillator is running, whether a power failure has occurred, and whether battery backup is configured.
< Bit mask for the OSCRUN flag indicating that the oscillator is running.
< Bit mask for the PWRFAIL flag indicating that a power failure has occurred.
< Bit mask for the VBATEN flag enabling battery-backed timekeeping.
| void mcp7940_time | ( | FORMAT_Time * | time, |
| MCP7940_Register | reg ) |
Reads hour, minute, and second fields from the MCP7940 into a FORMAT_Time structure.
| time | Pointer to a FORMAT_Time structure that will be populated with time information from the MCP7940 registers. On return:
|
| reg | Selects which MCP7940 register set to read, using a value from MCP7940_Register:
|
This function uses helper routines (mcp7940_hour(), mcp7940_minute(), and optionally mcp7940_second()) to extract the hour and minute from the selected MCP7940 time/timestamp registers and write them into time. For the current time selection, seconds are also read via mcp7940_second(). For power-down and power-up timestamp registers, the seconds field is not read and time->second is set to 0, since those registers do not store a separate seconds value in this implementation.

| MCP7940_Error mcp7940_trimming | ( | MCP7940_Trim | mode, |
| unsigned char | value ) |
Configures the MCP7940 oscillator trimming value and verifies the write.
| mode | Selects the trim direction, using a value from MCP7940_Trim:
|
| value | Raw trim magnitude to apply to the OSCTRIM register. Only the lower 7 bits (bit 6:0) are used as the trim value; bit 7 is set or cleared internally by this function according to mode to encode the sign of the adjustment. A value of 0 disables digital trimming. |
This function masks value to seven bits, applies the sign bit according to the selected trim mode, and writes the resulting byte to the MCP7940 OSCTRIM register to adjust the RTC oscillator frequency. It then reads back the OSCTRIM register and compares it to the written value to confirm that the configuration was accepted by the device.
| unsigned char mcp7940_weekday | ( | MCP7940_Register | data | ) |
Reads the weekday value from the MCP7940 for the selected timestamp register set.
| data | Selects which MCP7940 register block to read the weekday from, using a value from MCP7940_Register:
|
This function extracts the weekday field from the appropriate MCP7940 register depending on data. For the power-down and power-up timestamp registers, the weekday bits are located in the upper three bits of the month register (PWRDNMTH or PWRUPMTH) and are right-shifted by MCP7940_PWRWEEKDAY_bp after masking. For the current time, the weekday is read directly from the RTCWKDAY register and masked with 0x07 to return only the weekday bits.
< Bit position of the least significant bit of the weekday field in the power-fail timestamp month registers.
< Bit position of the least significant bit of the weekday field in the power-fail timestamp month registers.
| const char * mcp7940_weekday_string | ( | unsigned char | day | ) |
Returns a three-letter weekday abbreviation for a given day value.
| day | Encoded weekday value in the MCP7940 format (1–7 expected), where:
|
day.This function converts the MCP7940 weekday encoding into a human-readable three-character abbreviation by masking and normalizing the input value into the index range of the internal weeksdays lookup table. The returned pointer refers to a static string and remains valid for the lifetime of the program.