mcp7940 1.0
mcp7940 driver library
Loading...
Searching...
No Matches
mcp7940

Version: 1.0 Release Build License GPLv3

MCP7940 RTC Driver

Ask DeepWiki

This hardware abstracted driver can be used to interact with an MCP7940 over TWI/I2C. The hardware layer is fully abstract an can be switched between different plattforms. The TWI/I2C library has to impelement the twi.h-header used in this repository.

File Structure

File Structure

drivers/
└── rtc/
└── mcp7940/
├── mcp7940.c
└── mcp7940.h
hal/
├── common/
| ├── defines/
| | └── TWI_defines.h
| └── enums/
| └── TWI_enums.h
└── avr0/
└── twi/
├── twi.c
└── twi.h
core_types/
├── format/
| └── time.h
└── return/
└── status.h
utils/
├── macros/
| └── stringify.h
└── systick/
├── systick.c
└── systick.h

The plattform avr0 can completely be exchanged with any other hardware abstraction library.

Downloads

The library can be downloaded (zip or tar), cloned or used as submodule in a project.

Type File Description
Library zip / tar MCP7940 rtc library including all required libraries (including hal-avr0-twi).

Using with git clone

mkdir -p ./core_types/
git clone https://github.com/0x007E/core_types-format.git ./core_types
mv ./core_types/core_types-format ./core_types/format
git clone https://github.com/0x007E/core_types-return.git ./core_types
mv ./core_types/core_types-return ./core_types/return
mkdir -p ./drivers/rtc/
git clone https://github.com/0x007E/drivers-rtc-mcp7940.git ./drivers/rtc
mv ./drivers/rtc/drivers-rtc-mcp7940 ./drivers/rtc/mcp7940
mkdir -p ./hal/
git clone https://github.com/0x007E/hal-common.git ./hal
mv ./hal/hal-common ./hal/common
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Hardware abstraction layer of TWI (Must fit the used plattform)
mkdir -p ./hal/avr0/
git clone https://github.com/0x007E/hal-avr0-twi.git ./hal/avr0
mv ./hal/avr0/hal-avr0-twi ./hal/avr0/twi
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mkdir -p ./utils/
git clone https://github.com/0x007E/utils-macros.git ./utils
git clone https://github.com/0x007E/utils-systick.git ./utils
git clone https://github.com/0x007E/utils-time.git ./utils
mv ./utils/utils-macros ./utils/macros
mv ./utils/utils-systick ./utils/systick
mv ./utils/utils-time ./utils/time

Using as git submodule

git submodule add https://github.com/0x007E/core_types-format.git core_types/format
git submodule add https://github.com/0x007E/core_types-return.git core_types/return
git submodule add https://github.com/0x007E/drivers-rtc-mcp7940.git drivers/rtc/mcp7940
git submodule add https://github.com/0x007E/hal-common.git hal/common
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Hardware abstraction layer of TWI (Must fit the used plattform)
git submodule add https://github.com/0x007E/hal-avr0-twi.git hal/avr0/twi
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
git submodule add https://github.com/0x007E/utils-macros.git utils/macros
git submodule add https://github.com/0x007E/utils-systick.git utils/systick
git submodule add https://github.com/0x007E/utils-time.git utils/time

Programming

ISR(...)
{
}
void systick_timer_wait_ms(unsigned int ms)
{
}
int main(void)
{
// Only works with #define MCP7940_BATTERY_BACKUP_EN enabled!!!
{
FORMAT_DateTime power_down;
// Do something with the time
FORMAT_DateTime power_up;
// Do something with the time
}
// Set date and time
// For data integrity the time and date should be set before the clock starts running.
FORMAT_Date d = { 17, 2, 26 };
FORMAT_Time t = { 13, 37, 0 };
{ 17, 2, 26},
{ 13, 37, 0 }
};
// Set weekday
// Blocking method to check if oscillator is running
{
asm volatile("NOP");
}
// Non-blocking method to check if oscillator is running
// Check if Oscillator is Running
{
// Error -> Oscillator is not running!
}
// Fetch time and date from RTC
FORMAT_DateTime datetime;
// Fetch weekday from RTC
// Weekday as string
// Check if current year is a leap year
{
}
MCP7940_Error mcp7940_trimming(MCP7940_Trim mode, unsigned char value)
Configures the MCP7940 oscillator trimming value and verifies the write.
Definition mcp7940.c:157
MCP7940_LeapYear mcp7940_leapyear(void)
Returns the current leap year status from the MCP7940 device.
Definition mcp7940.c:450
MCP7940_Status mcp7940_status(void)
Reads and returns the current MCP7940 status flags from the weekday register.
Definition mcp7940.c:221
MCP7940_Error mcp7940_setdate(const FORMAT_Date *date)
Sets the current calendar date of the MCP7940 RTC from a FORMAT_Date structure.
Definition mcp7940.c:550
MCP7940_Error mcp7940_settime(const FORMAT_Time *time)
Sets the current time of the MCP7940 RTC from a FORMAT_Time structure.
Definition mcp7940.c:519
void mcp7940_init(void)
Initializes the MCP7940 RTC with battery backup, MFP mode, and oscillator settings.
Definition mcp7940.c:112
MCP7940_Error mcp7940_setweekday(unsigned char weekday)
Sets the MCP7940 weekday field in the RTCWKDAY register.
Definition mcp7940.c:473
unsigned char mcp7940_weekday(MCP7940_Register data)
Reads the weekday value from the MCP7940 for the selected timestamp register set.
Definition mcp7940.c:306
MCP7940_Error mcp7940_setdatetime(const FORMAT_DateTime *datetime)
Sets the current MCP7940 date and time from a FORMAT_DateTime structure.
Definition mcp7940.c:580
void mcp7940_time(FORMAT_Time *time, MCP7940_Register reg)
Reads hour, minute, and second fields from the MCP7940 into a FORMAT_Time structure.
Definition mcp7940.c:378
void mcp7940_datetime(FORMAT_DateTime *datetime, MCP7940_Register reg)
Reads both time and date from the MCP7940 into a FORMAT_DateTime structure.
Definition mcp7940.c:434
void mcp7940_date(FORMAT_Date *date, MCP7940_Register reg)
Reads day, month, and year fields from the MCP7940 into a FORMAT_Date structure.
Definition mcp7940.c:408
const char * mcp7940_weekday_string(unsigned char day)
Returns a three-letter weekday abbreviation for a given day value.
Definition mcp7940.c:52
Header file with declarations and macros for driving an mcp7940.
#define MCP7940_OSC_ENABLE_MS
Delay time in milliseconds before oscillator run check after enabling the MCP7940 oscillator.
Definition mcp7940.h:219
@ MCP7940_Trim_Add
Definition mcp7940.h:661
@ MCP7940_LeapYear_True
Definition mcp7940.h:679
@ MCP7940_Register_Power_Down_Time
Definition mcp7940.h:697
@ MCP7940_Register_Power_Up_Time
Definition mcp7940.h:698
@ MCP7940_Register_Current_Time
Definition mcp7940.h:696
#define MCP7940_WEEKDAY_TUESDAY_gc
Definition mcp7940.h:359
@ MCP7940_Status_Power_Fail
Definition mcp7940.h:605
@ MCP7940_Status_Oscillator_Running
Definition mcp7940.h:606
void systick_init(void)
Initializes the SysTick timer module.
Definition systick.c:35
void systick_tick(void)
Increments the SysTick timer tick counter.
Definition systick.c:61
void systick_timer_wait(unsigned int ticks)
Blocks execution for a specified number of SysTick ticks.
Definition systick.c:168
Header file with declarations and macros for systick utility.
void systick_timer_wait_ms(unsigned int ms)
struct FORMAT_DateTime_t FORMAT_DateTime
Alias for struct FORMAT_DateTime_t representing a combined date-time value.
Definition time.h:92
struct FORMAT_Time_t FORMAT_Time
Alias for struct FORMAT_Time_t representing a formatted time of day.
Definition time.h:45
struct FORMAT_Date_t FORMAT_Date
Alias for struct FORMAT_Date_t representing a formatted calendar date.
Definition time.h:69
unsigned char twi_init(void)
Initialize the TWI (I2C) hardware interface in master mode.
Definition twi.c:36
Header file with declarations and macros for hardware TWI/I2C.

Additional Information

Type Link Description
MCP7940 pdf Battery-Backed TWI/I2C Real-Time Clock/Calendar with SRAM

R. GAECHTER