Updated to latest release level. This posting matches the released code and the attached documentation.
See here for example code. The driver now supports Software I2C and Hardware I2C.
The MCP7940N.h include file provides both high- and low-level support for the MCP7940N real-time clock chip, two alarms with a backup battery, additional eeprom and a general purpose IO port. Insert the following directive in your code to make these new commands available:
~~~~
include <MCP7940N.h>
~~~~~
Recognition goes to the following:
Initial work created myself in Oct 2013 for the DS1307,
adapted works by Thomas Henry, May 26, 2014 for DS1307,
adapted further for MCP7940N Special Christmas 2014 release by Pete Everett, Dec 14, 2014,
revised and added new functionality to comply with MCP7940N datasheet by Evan R Venn - Jan 1 2015
and revised and added MCP7940_ReadFailureClock functionality to comply with MCP7940N datasheet by me.
Here follows a list of the commands. See the attachment also and the include file.
MCP7940N_Enable(flag)
enables the clock when flag is TRUE,
disables the clock when flag is FALSE
MCP7940N_ResetClock
resets clock completely to manufacturer’s original condition,
time to 00:00:00, day of the week to 01, date to 01/01/00,
also sets 24-hour mode and enables the clock.
MCP7940_BatteryBackup(flag)
enables the battery backup functions when flag is TRUE,
disables the battery backup functions when flag is FALSE
MCP7940N_SetTime(hour, minute, second)
sets the time only: hours, minutes, seconds,.
also sets 24-hour mode and enables the clock.
MCP7940N_SetDate(date, month, year)
sets the date only: date, month, year,
there is no error detection for out-of-range dates, (e.g., April 31)
MCP7940N_SetClock(hour, minute, second, DOW, date, month, year)
sets the entire clock: hours, minutes, seconds, day of week, date, month, year.
there is no error detection for out-of-range dates, (e.g., April 31)
also sets 24-hour mode and enables the clock.
MCP7940N_ReadTime(hour, minute, second, flag)
reads the time only: hours, minutes, seconds, a.m. or p.m.,
flag = FALSE means a.m.,
flag = TRUE means p.m.
MCP7940N_ReadDate(date, month, year)
reads the date only: date, month, year
MCP7940N_ReadClock(hour, minute, second, flag, DOW, date, month, year)
reads the entire clock: hours, minutes, seconds, flag, day of week, date, month, year
flag = FALSE means a.m.,
flag = TRUE means p.m.
MCP7940N_SetHourMode(12|24)
sets the hour mode,
12 = 12-hour
24 = 24-hour
any other value defaults to 24-hour mode
MCP7940N_ReadHourMode(value)
returns the current hour mode,
MCP7940N_SetSQW(rate)
sets the square wave output pin mode:
0 = disable square wave output
1 = 1 Hz output
4 = 4096 Hz
8 = 8192 Hz
32 = 32768 Hz
any other value defaults to 1 Hz
MCP7940N_Write(address, value)
writes to the internal registers or RAM,
registers: 0x00 to 0x07
RAM: 0x08 to 0x3F
writing beyond this wraps around to the register space again, so be careful with multibyte writes
MCP7940N_Read(address, value)
reads from the internal registers or RAM,
see the notes, above.
MCP7940_IsLeapYear ( value )
Reads the leap year status of the current year
MCP7940_SetMFP(MFP_Value)
Sets Multifunction Pin status to the value of the variable MFP_Value
MCP7940_SetControl(MFP_Value)
Sets Control address status to the value of the variable MFP_Value. The control - rtcc control register is at address 0x07. Direct access to the control register permits reading and writing of the controls.
Bits usage as below:
7 OUT: Logic Level for General Purpose Output bit
6 SQWEN: Square Wave Output Enable bit
5 ALM1EN: Alarm 1 Module Enable bit
4 ALM0EN: Alarm 0 Module Enable bit
3 EXTOSC: External Oscillator Input bit
2 CRSTRIM: Coarse Trim Mode Enable bit
1 SQWFS<1:0>: Square Wave Clock Output Frequency Select bits
0 See bit 1
See the datasheet for more information
MCP7940_ReadControl
This function returns the current value of the Control address
MCP7940_SetAlarm ( Alarm#[0|1], Hour, Min, Sec, DOW, Date )
sets the alarm: hours, minutes, seconds, day of week, date, month, year.
there is no error detection for out-of-range dates, (e.g., April 31)
also sets 24-hour mode.
MCP7940_SetAlarmMask (Alarm#[0|1], Value )
sets the alarm where Value can be any of the following.
MCP7940_AlarmAssertion_Seconds
MCP7940_AlarmAssertion_Minutes
MCP7940_AlarmAssertion_Hours
MCP7940_AlarmAssertion_DayofWeek
MCP7940_AlarmAssertion_Date
MCP7940_AlarmAssertion_All
The alarm can be set to go off if any of the following conditions are met: MCP7940_AlarmAssertion_Seconds 0x00 a match of the seconds
MCP7940_AlarmAssertion_Minutes 0x01 a match of the minutes
MCP7940_AlarmAssertion_Hours 0x02 a match of the hours
MCP7940_AlarmAssertion_DayofWeek 0x03 a match of the day of the week
MCP7940_AlarmAssertion_Date 0x04 a match of the date
MCP7940_AlarmAssertion_All 0x07 a match of all parameters equals all seconds, minutes, hours, day of week, day and month match.
A match of these assertions will raise the alarm.
MCP7940_ReadAlarm (Alarm#[0|1], Hour, Min, Sec, DOW, Date )
Returns the current settings for a specific alarm.
MCP7940_ClearAlarm ( Alarm#[0|1] )
Clears a specific alarm after an alarm assertion.
MCP7940_AlarmStatus ( Alarm#[0|1] )
This is a function. Returns a specific alarm status.
FALSE means the specific alarm has not met the assertion criteria
TRUE means the specific alarm has met the assertion criteria
MCP7940_SetAlarmPolarity ( Alarm#[0|1] , Value)
Sets the general purpose port status.
Value = 1 then the port with be normally high, low on assertion.
Value = 0 then the port with be normally low, high on assertion.
MCP7940_EnableAlarm ( Alarm#[0|1] )
Enables a specific alarm.
MCP7940_DisableAlarm ( Alarm#[0|1] )
Disables a specific alarm.
MCP7940_ClearPowerFail
Clears the power failure status with the device. Required to be reset after each power failure.
MCP7940_PowerFailStatus
Returns the the power failure status with the device.
MCP7940_ReadFailureClock( Alarm#[0|1] , Hour, Min, DOW, Date, Month)
Reads the failure information of a specific failure event: hours, minutes, seconds, day of week, date and month.
Attached is a demo. A fully featured calendar/clock. This code has been adapted from the previous clock demonstration code.
The code is extensively commented and includes operating instructions as well. It only needs two pushbuttons to get all the options which will make it great for chips with fewer pins.
The example include alarms and is extensively documented.
If you want to try this without using the LCD display - you can via the serial port to a PC terminal. It works just the same.
A few things you need.
1) Use a terminal application that is ANSI compatible. I use Putty.
2) Add the following to your code. This will redirect the LCD to the Serial Ports.
2a. Somewhere near the top of your code add
' THIS CONFIG OF THE SERIAL PORT WORKS WITH max232 THEN TO PC
' USART settings
#define USART_BAUD_RATE 9600
Dir PORTc.6 Out 'correct for your device.
#define USART_DELAY 10 ms
#define USART_BLOCKING
~~~~~
-2b. Add the following redirects.
define locate ANSI_rev
define CLS ANSIERASECREEN
define print hserprint
' Position cursor on Terminal
Sub ANSI ( IN xpos, IN ypos )
HSerSend 27
HSerPrint "["
HSerPrint ypos
HSerSend 59
HSerPrint xpos
HSerPrint "H"
End Sub
' Position cursor on Terminal
Sub ANSI_rev ( IN ypos, IN xpos )
ypos++
HSerSend 27
HSerPrint "["
HSerPrint ypos
HSerSend 59
HSerPrint xpos
HSerPrint "H"
End Sub
I wrote some test code and I can't get it to compile using GCB@Syn and the compiler/chipdata that you sent me yesterday. It gets to 'compiling subroutines/functions: Main' then hangs. If I comment out the line "#include <MCP7940N.h>", it compiles fine.
There is something wrong with my installation which I need to sort out (reverting to the previous compiler/chipdata version didn't help either and gave an error)
It works with an older version of GCGB (from November). I can set the clock running and display it on an LCD so far.
I've have proper look at it later.
Last edit: Peter 2015-01-02
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Peter. I do get same error here. I just took a copy of the May14 hot release, updated with the compiler (I provided to you), updated the .dat files and replaced the relavant include files and all is OK. Hope this helps your investigations.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've tested everything except MCP7940_IsLeapYear and the battery backup/power failure functions and I only checked alarm0, didn't bother doing it all again for alarm1.
I've only come across one possible bug:
: MCP7940_SetAlarm seems to be missing the month parameter? (DS_Date is just the day of the month)
Everything else works as expected.
And not really bugs as such but a couple of observations:
: Does MCP7940_SetControl need the comments altering to say what it does?
: Does MCP7940_SetAlarmMask need some inline comments to explain what values to use for alarmAssertionMatch e.g.
;The alarm can be set to go off if any of the following conditions are met
;000 Seconds (0)
;001 Minutes (1)
;010 Hours (2)
;011 Day of week (3)
;100 Date (4)
;111 All of seconds, minutes, hours, day of week, day and month (7)
I also get a strange problem where I write the time to the MCP7940, then set the oscillator going and the clock steps back a few seconds. I think this is a problem with my particular setup/problem with my device as it's not happened on my other setup and sometimes cycling the power on the PIC gives the correct result.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
MCP7940N Real-Time Clock
Updated to latest release level. This posting matches the released code and the attached documentation.
See here for example code. The driver now supports Software I2C and Hardware I2C.
The MCP7940N.h include file provides both high- and low-level support for the MCP7940N real-time clock chip, two alarms with a backup battery, additional eeprom and a general purpose IO port. Insert the following directive in your code to make these new commands available:
~~~~
include <MCP7940N.h>
~~~~~
Recognition goes to the following:
Initial work created myself in Oct 2013 for the DS1307,
adapted works by Thomas Henry, May 26, 2014 for DS1307,
adapted further for MCP7940N Special Christmas 2014 release by Pete Everett, Dec 14, 2014,
revised and added new functionality to comply with MCP7940N datasheet by Evan R Venn - Jan 1 2015
and revised and added MCP7940_ReadFailureClock functionality to comply with MCP7940N datasheet by me.
The latest file is here
Here follows a list of the commands. See the attachment also and the include file.
MCP7940N_Enable(flag)
enables the clock when flag is TRUE,
disables the clock when flag is FALSE
MCP7940N_ResetClock
resets clock completely to manufacturer’s original condition,
time to 00:00:00, day of the week to 01, date to 01/01/00,
also sets 24-hour mode and enables the clock.
MCP7940_BatteryBackup(flag)
enables the battery backup functions when flag is TRUE,
disables the battery backup functions when flag is FALSE
MCP7940N_SetTime(hour, minute, second)
sets the time only: hours, minutes, seconds,.
also sets 24-hour mode and enables the clock.
MCP7940N_SetDate(date, month, year)
sets the date only: date, month, year,
there is no error detection for out-of-range dates, (e.g., April 31)
MCP7940N_SetClock(hour, minute, second, DOW, date, month, year)
sets the entire clock: hours, minutes, seconds, day of week, date, month, year.
there is no error detection for out-of-range dates, (e.g., April 31)
also sets 24-hour mode and enables the clock.
MCP7940N_ReadTime(hour, minute, second, flag)
reads the time only: hours, minutes, seconds, a.m. or p.m.,
flag = FALSE means a.m.,
flag = TRUE means p.m.
MCP7940N_ReadDate(date, month, year)
reads the date only: date, month, year
MCP7940N_ReadClock(hour, minute, second, flag, DOW, date, month, year)
reads the entire clock: hours, minutes, seconds, flag, day of week, date, month, year
flag = FALSE means a.m.,
flag = TRUE means p.m.
MCP7940N_SetHourMode(12|24)
sets the hour mode,
12 = 12-hour
24 = 24-hour
any other value defaults to 24-hour mode
MCP7940N_ReadHourMode(value)
returns the current hour mode,
MCP7940N_SetSQW(rate)
sets the square wave output pin mode:
0 = disable square wave output
1 = 1 Hz output
4 = 4096 Hz
8 = 8192 Hz
32 = 32768 Hz
any other value defaults to 1 Hz
MCP7940N_Write(address, value)
writes to the internal registers or RAM,
registers: 0x00 to 0x07
RAM: 0x08 to 0x3F
writing beyond this wraps around to the register space again, so be careful with multibyte writes
MCP7940N_Read(address, value)
reads from the internal registers or RAM,
see the notes, above.
MCP7940_IsLeapYear ( value )
Reads the leap year status of the current year
MCP7940_SetMFP(MFP_Value)
Sets Multifunction Pin status to the value of the variable MFP_Value
MCP7940_SetControl(MFP_Value)
Sets Control address status to the value of the variable MFP_Value. The control - rtcc control register is at address 0x07. Direct access to the control register permits reading and writing of the controls.
Bits usage as below:
7 OUT: Logic Level for General Purpose Output bit
6 SQWEN: Square Wave Output Enable bit
5 ALM1EN: Alarm 1 Module Enable bit
4 ALM0EN: Alarm 0 Module Enable bit
3 EXTOSC: External Oscillator Input bit
2 CRSTRIM: Coarse Trim Mode Enable bit
1 SQWFS<1:0>: Square Wave Clock Output Frequency Select bits
0 See bit 1
See the datasheet for more information
MCP7940_ReadControl
This function returns the current value of the Control address
MCP7940_SetAlarm ( Alarm#[0|1], Hour, Min, Sec, DOW, Date )
sets the alarm: hours, minutes, seconds, day of week, date, month, year.
there is no error detection for out-of-range dates, (e.g., April 31)
also sets 24-hour mode.
MCP7940_SetAlarmMask (Alarm#[0|1], Value )
sets the alarm where Value can be any of the following.
MCP7940_AlarmAssertion_Seconds
MCP7940_AlarmAssertion_Minutes
MCP7940_AlarmAssertion_Hours
MCP7940_AlarmAssertion_DayofWeek
MCP7940_AlarmAssertion_Date
MCP7940_AlarmAssertion_All
The alarm can be set to go off if any of the following conditions are met: MCP7940_AlarmAssertion_Seconds 0x00 a match of the seconds
MCP7940_AlarmAssertion_Minutes 0x01 a match of the minutes
MCP7940_AlarmAssertion_Hours 0x02 a match of the hours
MCP7940_AlarmAssertion_DayofWeek 0x03 a match of the day of the week
MCP7940_AlarmAssertion_Date 0x04 a match of the date
MCP7940_AlarmAssertion_All 0x07 a match of all parameters equals all seconds, minutes, hours, day of week, day and month match.
A match of these assertions will raise the alarm.
MCP7940_ReadAlarm (Alarm#[0|1], Hour, Min, Sec, DOW, Date )
Returns the current settings for a specific alarm.
MCP7940_ClearAlarm ( Alarm#[0|1] )
Clears a specific alarm after an alarm assertion.
MCP7940_AlarmStatus ( Alarm#[0|1] )
This is a function. Returns a specific alarm status.
FALSE means the specific alarm has not met the assertion criteria
TRUE means the specific alarm has met the assertion criteria
MCP7940_SetAlarmPolarity ( Alarm#[0|1] , Value)
Sets the general purpose port status.
Value = 1 then the port with be normally high, low on assertion.
Value = 0 then the port with be normally low, high on assertion.
MCP7940_EnableAlarm ( Alarm#[0|1] )
Enables a specific alarm.
MCP7940_DisableAlarm ( Alarm#[0|1] )
Disables a specific alarm.
MCP7940_ClearPowerFail
Clears the power failure status with the device. Required to be reset after each power failure.
MCP7940_PowerFailStatus
Returns the the power failure status with the device.
MCP7940_ReadFailureClock( Alarm#[0|1] , Hour, Min, DOW, Date, Month)
Reads the failure information of a specific failure event: hours, minutes, seconds, day of week, date and month.
Last edit: Anobium 2015-01-12
Attached is a demo. A fully featured calendar/clock. This code has been adapted from the previous clock demonstration code.
The code is extensively commented and includes operating instructions as well. It only needs two pushbuttons to get all the options which will make it great for chips with fewer pins.
The example include alarms and is extensively documented.
Last edit: Anobium 2015-01-02
If you want to try this without using the LCD display - you can via the serial port to a PC terminal. It works just the same.
A few things you need.
1) Use a terminal application that is ANSI compatible. I use Putty.
2) Add the following to your code. This will redirect the LCD to the Serial Ports.
define locate ANSI_rev
define CLS ANSIERASECREEN
define print hserprint
' Position cursor on Terminal
Sub ANSI ( IN xpos, IN ypos )
HSerSend 27
HSerPrint "["
HSerPrint ypos
HSerSend 59
HSerPrint xpos
HSerPrint "H"
End Sub
' Position cursor on Terminal
Sub ANSI_rev ( IN ypos, IN xpos )
ypos++
HSerSend 27
HSerPrint "["
HSerPrint ypos
HSerSend 59
HSerPrint xpos
HSerPrint "H"
End Sub
' Eraase Terminal
Sub ANSIERASECREEN
HSerSend 27
HSerPrint "["
HSerPrint "2"
HSerPrint "J"
Ansi ( 0, 0 )
end Sub
~~~~
And, all should work to the ANSI terminal.
I wrote some test code and I can't get it to compile using GCB@Syn and the compiler/chipdata that you sent me yesterday. It gets to 'compiling subroutines/functions: Main' then hangs. If I comment out the line "#include <MCP7940N.h>", it compiles fine.
There is something wrong with my installation which I need to sort out (reverting to the previous compiler/chipdata version didn't help either and gave an error)
It works with an older version of GCGB (from November). I can set the clock running and display it on an LCD so far.
I've have proper look at it later.
Last edit: Peter 2015-01-02
@Peter. I do get same error here. I just took a copy of the May14 hot release, updated with the compiler (I provided to you), updated the .dat files and replaced the relavant include files and all is OK. Hope this helps your investigations.
I just recompiled on another computer with a 16f877a. Only changes I made were as follows - all tested ok.
I've tested everything except MCP7940_IsLeapYear and the battery backup/power failure functions and I only checked alarm0, didn't bother doing it all again for alarm1.
I've only come across one possible bug:
: MCP7940_SetAlarm seems to be missing the month parameter? (DS_Date is just the day of the month)
Everything else works as expected.
And not really bugs as such but a couple of observations:
: Does MCP7940_SetControl need the comments altering to say what it does?
: Does MCP7940_SetAlarmMask need some inline comments to explain what values to use for alarmAssertionMatch e.g.
I also get a strange problem where I write the time to the MCP7940, then set the oscillator going and the clock steps back a few seconds. I think this is a problem with my particular setup/problem with my device as it's not happened on my other setup and sometimes cycling the power on the PIC gives the correct result.
Thank you for reviewing and testing. Most grateful.
Changes all completed.
I have posted <here>
See here for example code. The driver now supports Software I2C and Hardware I2C.