kopia lustrzana https://github.com/g7uhn/ft817_buddy
Merge branch 'rtc' into master
commit
1c83d8d4ca
|
@ -0,0 +1,57 @@
|
|||
SparkFun License Information
|
||||
============================
|
||||
|
||||
SparkFun uses two different licenses for our files - one for hardware and one for code.
|
||||
|
||||
Hardware
|
||||
---------
|
||||
|
||||
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
|
||||
|
||||
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
|
||||
|
||||
You are free to:
|
||||
|
||||
Share — copy and redistribute the material in any medium or format
|
||||
Adapt — remix, transform, and build upon the material
|
||||
for any purpose, even commercially.
|
||||
The licensor cannot revoke these freedoms as long as you follow the license terms.
|
||||
Under the following terms:
|
||||
|
||||
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
|
||||
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
|
||||
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
|
||||
Notices:
|
||||
|
||||
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
|
||||
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
|
||||
|
||||
|
||||
Code
|
||||
--------
|
||||
|
||||
**SparkFun code, firmware, and software is released under the [MIT License](http://opensource.org/licenses/MIT).**
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 SparkFun Electronics, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
SparkFun Real Time Clock Module (DS1307) Arduino Library
|
||||
========================================
|
||||
|
||||
[![SparkFun Real Time Clock Module](https://cdn.sparkfun.com//assets/parts/9/4/5/4/12708-01.jpg)](https://www.sparkfun.com/products/12708)
|
||||
|
||||
[*SparkFun Real Time Clock Module (BOB-12708)*](https://www.sparkfun.com/products/12708)
|
||||
|
||||
Arduino library for the I<sup>2</sup>C-based DS1307 real time clock (RTC). Allows you to set and read the time and date, and configure the SQW output pin.
|
||||
|
||||
Repository Contents
|
||||
-------------------
|
||||
|
||||
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
|
||||
* **/src** - Source files for the library (.cpp, .h).
|
||||
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
|
||||
* **library.properties** - General library properties for the Arduino package manager.
|
||||
|
||||
Documentation
|
||||
--------------
|
||||
|
||||
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
|
||||
* **[Product Repository](https://github.com/sparkfun/RTC-Module/tree/v1.4)** - Main repository (including hardware files) for the Real Time Clock Module.
|
||||
* **[Hookup Guide](https://learn.sparkfun.com/tutorials/real-time-clock-module-hookup-guide)** - Basic hookup guide for the Real Time Clock Module.
|
||||
|
||||
Products that use this Library
|
||||
---------------------------------
|
||||
|
||||
* [SparkFun Real Time Clock Module (BOB-12708)](https://www.sparkfun.com/products/12708)- Basic breakout for the DS1307, including pull-up resistors and a coin cell battery holder.
|
||||
|
||||
Version History
|
||||
---------------
|
||||
|
||||
* [V_1.0.0](https://github.com/sparkfun/SparkFun_DS1307_RTC_Arduino_Library/tree/V_1.0.0) - Initial library release
|
||||
|
||||
License Information
|
||||
-------------------
|
||||
|
||||
This product is _**open source**_!
|
||||
|
||||
Please review the LICENSE.md file for license information.
|
||||
|
||||
If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com.
|
||||
|
||||
Distributed as-is; no warranty is given.
|
||||
|
||||
- Your friends at SparkFun.
|
|
@ -0,0 +1,104 @@
|
|||
/******************************************************************************
|
||||
DS1307_RTC_Demo.ino
|
||||
Jim Lindblom @ SparkFun Electronics
|
||||
original creation date: October 2, 2016
|
||||
https://github.com/sparkfun/SparkFun_DS1307_RTC_Arduino_Library
|
||||
|
||||
Configures, sets, and reads from the DS1307 real-time clock (RTC).
|
||||
|
||||
Resources:
|
||||
Wire.h - Arduino I2C Library
|
||||
|
||||
Development environment specifics:
|
||||
Arduino 1.6.8
|
||||
SparkFun RedBoard
|
||||
SparkFun Real Time Clock Module (v14)
|
||||
******************************************************************************/
|
||||
#include <SparkFunDS1307RTC.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// Comment out the line below if you want month printed before date.
|
||||
// E.g. October 31, 2016: 10/31/16 vs. 31/10/16
|
||||
#define PRINT_USA_DATE
|
||||
|
||||
#define SQW_INPUT_PIN 2 // Input pin to read SQW
|
||||
#define SQW_OUTPUT_PIN 13 // LED to indicate SQW's state
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Use the serial monitor to view time/date output
|
||||
Serial.begin(9600);
|
||||
pinMode(SQW_INPUT_PIN, INPUT_PULLUP);
|
||||
pinMode(SQW_OUTPUT_PIN, OUTPUT);
|
||||
digitalWrite(SQW_OUTPUT_PIN, digitalRead(SQW_INPUT_PIN));
|
||||
|
||||
rtc.begin(); // Call rtc.begin() to initialize the library
|
||||
// (Optional) Sets the SQW output to a 1Hz square wave.
|
||||
// (Pull-up resistor is required to use the SQW pin.)
|
||||
rtc.writeSQW(SQW_SQUARE_1);
|
||||
|
||||
// Now set the time...
|
||||
// You can use the autoTime() function to set the RTC's clock and
|
||||
// date to the compiliers predefined time. (It'll be a few seconds
|
||||
// behind, but close!)
|
||||
rtc.autoTime();
|
||||
// Or you can use the rtc.setTime(s, m, h, day, date, month, year)
|
||||
// function to explicitly set the time:
|
||||
// e.g. 7:32:16 | Monday October 31, 2016:
|
||||
// rtc.setTime(16, 32, 7, 2, 31, 10, 16); // Uncomment to manually set time
|
||||
// rtc.set12Hour(); // Use rtc.set12Hour to set to 12-hour mode
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static int8_t lastSecond = -1;
|
||||
|
||||
// Call rtc.update() to update all rtc.seconds(), rtc.minutes(),
|
||||
// etc. return functions.
|
||||
rtc.update();
|
||||
|
||||
if (rtc.second() != lastSecond) // If the second has changed
|
||||
{
|
||||
printTime(); // Print the new time
|
||||
|
||||
lastSecond = rtc.second(); // Update lastSecond value
|
||||
}
|
||||
|
||||
// Read the state of the SQW pin and show it on the
|
||||
// pin 13 LED. (It should blink at 1Hz.)
|
||||
digitalWrite(SQW_OUTPUT_PIN, digitalRead(SQW_INPUT_PIN));
|
||||
}
|
||||
|
||||
void printTime()
|
||||
{
|
||||
Serial.print(String(rtc.hour()) + ":"); // Print hour
|
||||
if (rtc.minute() < 10)
|
||||
Serial.print('0'); // Print leading '0' for minute
|
||||
Serial.print(String(rtc.minute()) + ":"); // Print minute
|
||||
if (rtc.second() < 10)
|
||||
Serial.print('0'); // Print leading '0' for second
|
||||
Serial.print(String(rtc.second())); // Print second
|
||||
|
||||
if (rtc.is12Hour()) // If we're in 12-hour mode
|
||||
{
|
||||
// Use rtc.pm() to read the AM/PM state of the hour
|
||||
if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
|
||||
else Serial.print(" AM");
|
||||
}
|
||||
|
||||
Serial.print(" | ");
|
||||
|
||||
// Few options for printing the day, pick one:
|
||||
Serial.print(rtc.dayStr()); // Print day string
|
||||
//Serial.print(rtc.dayC()); // Print day character
|
||||
//Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
|
||||
Serial.print(" - ");
|
||||
#ifdef PRINT_USA_DATE
|
||||
Serial.print(String(rtc.month()) + "/" + // Print month
|
||||
String(rtc.date()) + "/"); // Print date
|
||||
#else
|
||||
Serial.print(String(rtc.date()) + "/" + // (or) print date
|
||||
String(rtc.month()) + "/"); // Print month
|
||||
#endif
|
||||
Serial.println(String(rtc.year())); // Print year
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#######################################
|
||||
# Syntax Coloring Map
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
rtc KEYWORD1
|
||||
SparkFunDS1307RTC KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
setTime KEYWORD2
|
||||
autoTime KEYWORD2
|
||||
setSecond KEYWORD2
|
||||
setMinute KEYWORD2
|
||||
setHour KEYWORD2
|
||||
setDay KEYWORD2
|
||||
setDate KEYWORD2
|
||||
setMonth KEYWORD2
|
||||
setYear KEYWORD2
|
||||
update KEYWORD2
|
||||
second KEYWORD2
|
||||
minute KEYWORD2
|
||||
hour KEYWORD2
|
||||
day KEYWORD2
|
||||
dayChar KEYWORD2
|
||||
dayStr KEYWORD2
|
||||
date KEYWORD2
|
||||
month KEYWORD2
|
||||
year KEYWORD2
|
||||
getSecond KEYWORD2
|
||||
getMinute KEYWORD2
|
||||
getHour KEYWORD2
|
||||
getDay KEYWORD2
|
||||
getDate KEYWORD2
|
||||
getMonth KEYWORD2
|
||||
getYear KEYWORD2
|
||||
is12Hour KEYWORD2
|
||||
pm KEYWORD2
|
||||
writeSQW KEYWORD2
|
||||
enable KEYWORD2
|
||||
disable KEYWORD2
|
||||
set12Hour KEYWORD2
|
||||
set24Hour KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
AM LITERAL1
|
||||
PM LITERAL1
|
||||
SQW_SQUARE_1 LITERAL1
|
||||
SQW_SQUARE_4K LITERAL1
|
||||
SQW_SQUARE_8K LITERAL1
|
||||
SQW_SQUARE_32K LITERAL1
|
||||
SQW_LOW LITERAL1
|
||||
SQW_HIGH LITERAL1
|
|
@ -0,0 +1,10 @@
|
|||
name=SparkFun DS1307 Real-Time Clock (RTC)
|
||||
version=1.0.1
|
||||
author=SparkFun Electronics <techsupport@sparkfun.com>
|
||||
maintainer=SparkFun Electronics <sparkfun.com>
|
||||
sentence=The DS1307 is an I2C-based real-time clock (RTC).
|
||||
paragraph=This library allows you to set and read the DS1307 RTC.
|
||||
category=Sensors
|
||||
url=https://github.com/sparkfun/SparkFun_DS1307_RTC_Arduino_Library
|
||||
architectures=*
|
||||
includes=Wire.h, SparkFunDS1307RTC.h
|
|
@ -0,0 +1,499 @@
|
|||
/******************************************************************************
|
||||
SparkFunDS1307RTC.cpp
|
||||
Jim Lindblom @ SparkFun Electronics
|
||||
original creation date: October 2, 2016
|
||||
https://github.com/sparkfun/SparkFun_DS1307_RTC_Arduino_Library
|
||||
|
||||
Implementation of DS1307 real time clock functions
|
||||
|
||||
Resources:
|
||||
Wire.h - Arduino I2C Library
|
||||
|
||||
Development environment specifics:
|
||||
Arduino 1.6.8
|
||||
SparkFun RedBoard
|
||||
SparkFun Real Time Clock Module (v14)
|
||||
******************************************************************************/
|
||||
|
||||
#include "SparkFunDS1307RTC.h"
|
||||
|
||||
// Parse the __DATE__ predefined macro to generate date defaults:
|
||||
// __Date__ Format: MMM DD YYYY (First D may be a space if <10)
|
||||
// <MONTH>
|
||||
#define BUILD_MONTH_JAN ((__DATE__[0] == 'J') && (__DATE__[1] == 'a')) ? 1 : 0
|
||||
#define BUILD_MONTH_FEB (__DATE__[0] == 'F') ? 2 : 0
|
||||
#define BUILD_MONTH_MAR ((__DATE__[0] == 'M') && (__DATE__[1] == 'a') && (__DATE__[2] == 'r')) ? 3 : 0
|
||||
#define BUILD_MONTH_APR ((__DATE__[0] == 'A') && (__DATE__[1] == 'p')) ? 4 : 0
|
||||
#define BUILD_MONTH_MAY ((__DATE__[0] == 'M') && (__DATE__[1] == 'a') && (__DATE__[2] == 'y')) ? 5 : 0
|
||||
#define BUILD_MONTH_JUN ((__DATE__[0] == 'J') && (__DATE__[1] == 'u') && (__DATE__[2] == 'n')) ? 6 : 0
|
||||
#define BUILD_MONTH_JUL ((__DATE__[0] == 'J') && (__DATE__[1] == 'u') && (__DATE__[2] == 'l')) ? 7 : 0
|
||||
#define BUILD_MONTH_AUG ((__DATE__[0] == 'A') && (__DATE__[1] == 'u')) ? 8 : 0
|
||||
#define BUILD_MONTH_SEP (__DATE__[0] == 'S') ? 9 : 0
|
||||
#define BUILD_MONTH_OCT (__DATE__[0] == 'O') ? 10 : 0
|
||||
#define BUILD_MONTH_NOV (__DATE__[0] == 'N') ? 11 : 0
|
||||
#define BUILD_MONTH_DEC (__DATE__[0] == 'D') ? 12 : 0
|
||||
#define BUILD_MONTH BUILD_MONTH_JAN | BUILD_MONTH_FEB | BUILD_MONTH_MAR | \
|
||||
BUILD_MONTH_APR | BUILD_MONTH_MAY | BUILD_MONTH_JUN | \
|
||||
BUILD_MONTH_JUL | BUILD_MONTH_AUG | BUILD_MONTH_SEP | \
|
||||
BUILD_MONTH_OCT | BUILD_MONTH_NOV | BUILD_MONTH_DEC
|
||||
// <DATE>
|
||||
#define BUILD_DATE_0 ((__DATE__[4] == ' ') ? 0 : (__DATE__[4] - 0x30))
|
||||
#define BUILD_DATE_1 (__DATE__[5] - 0x30)
|
||||
#define BUILD_DATE ((BUILD_DATE_0 * 10) + BUILD_DATE_1)
|
||||
// <YEAR>
|
||||
#define BUILD_YEAR (((__DATE__[7] - 0x30) * 1000) + ((__DATE__[8] - 0x30) * 100) + \
|
||||
((__DATE__[9] - 0x30) * 10) + ((__DATE__[10] - 0x30) * 1))
|
||||
|
||||
// Parse the __TIME__ predefined macro to generate time defaults:
|
||||
// __TIME__ Format: HH:MM:SS (First number of each is padded by 0 if <10)
|
||||
// <HOUR>
|
||||
#define BUILD_HOUR_0 ((__TIME__[0] == ' ') ? 0 : (__TIME__[0] - 0x30))
|
||||
#define BUILD_HOUR_1 (__TIME__[1] - 0x30)
|
||||
#define BUILD_HOUR ((BUILD_HOUR_0 * 10) + BUILD_HOUR_1)
|
||||
// <MINUTE>
|
||||
#define BUILD_MINUTE_0 ((__TIME__[3] == ' ') ? 0 : (__TIME__[3] - 0x30))
|
||||
#define BUILD_MINUTE_1 (__TIME__[4] - 0x30)
|
||||
#define BUILD_MINUTE ((BUILD_MINUTE_0 * 10) + BUILD_MINUTE_1)
|
||||
// <SECOND>
|
||||
#define BUILD_SECOND_0 ((__TIME__[6] == ' ') ? 0 : (__TIME__[6] - 0x30))
|
||||
#define BUILD_SECOND_1 (__TIME__[7] - 0x30)
|
||||
#define BUILD_SECOND ((BUILD_SECOND_0 * 10) + BUILD_SECOND_1)
|
||||
|
||||
// Constructor -- Initialize class variables to 0
|
||||
DS1307::DS1307()
|
||||
{
|
||||
for (int i=0; i<TIME_ARRAY_LENGTH; i++)
|
||||
{
|
||||
_time[i] = 0;
|
||||
}
|
||||
_pm = false;
|
||||
}
|
||||
|
||||
// Begin -- Initialize I2C interface
|
||||
void DS1307::begin(void)
|
||||
{
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
// setTime -- Set time and date/day registers of DS1307
|
||||
bool DS1307::setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t date, uint8_t month, uint8_t year)
|
||||
{
|
||||
_time[TIME_SECONDS] = DECtoBCD(sec);
|
||||
_time[TIME_MINUTES] = DECtoBCD(min);
|
||||
_time[TIME_HOURS] = DECtoBCD(hour);
|
||||
_time[TIME_DAY] = DECtoBCD(day);
|
||||
_time[TIME_DATE] = DECtoBCD(date);
|
||||
_time[TIME_MONTH] = DECtoBCD(month);
|
||||
_time[TIME_YEAR] = DECtoBCD(year);
|
||||
|
||||
return setTime(_time, TIME_ARRAY_LENGTH);
|
||||
}
|
||||
|
||||
// setTime -- Set time and date/day registers of DS1307 (using data array)
|
||||
bool DS1307::setTime(uint8_t * time, uint8_t len)
|
||||
{
|
||||
if (len != TIME_ARRAY_LENGTH)
|
||||
return false;
|
||||
|
||||
return i2cWriteBytes(DS1307_RTC_ADDRESS, DS1307_REGISTER_BASE, time, TIME_ARRAY_LENGTH);
|
||||
}
|
||||
|
||||
// autoTime -- Fill DS1307 time registers with compiler time/date
|
||||
bool DS1307::autoTime()
|
||||
{
|
||||
_time[TIME_SECONDS] = DECtoBCD(BUILD_SECOND);
|
||||
_time[TIME_MINUTES] = DECtoBCD(BUILD_MINUTE);
|
||||
_time[TIME_HOURS] = BUILD_HOUR;
|
||||
if (is12Hour())
|
||||
{
|
||||
uint8_t pmBit = 0;
|
||||
if (_time[TIME_HOURS] <= 11)
|
||||
{
|
||||
if (_time[TIME_HOURS] == 0)
|
||||
_time[TIME_HOURS] = 12;
|
||||
}
|
||||
else
|
||||
{
|
||||
pmBit = TWELVE_HOUR_PM;
|
||||
if (_time[TIME_HOURS] >= 13)
|
||||
_time[TIME_HOURS] -= 12;
|
||||
}
|
||||
DECtoBCD(_time[TIME_HOURS]);
|
||||
_time[TIME_HOURS] |= pmBit;
|
||||
_time[TIME_HOURS] |= TWELVE_HOUR_MODE;
|
||||
}
|
||||
else
|
||||
{
|
||||
DECtoBCD(_time[TIME_HOURS]);
|
||||
}
|
||||
|
||||
_time[TIME_MONTH] = DECtoBCD(BUILD_MONTH);
|
||||
_time[TIME_DATE] = DECtoBCD(BUILD_DATE);
|
||||
_time[TIME_YEAR] = DECtoBCD(BUILD_YEAR - 2000); //! Not Y2K (or Y2.1K)-proof :\
|
||||
|
||||
// Calculate weekday (from here: http://stackoverflow.com/a/21235587)
|
||||
// 0 = Sunday, 6 = Saturday
|
||||
int d = BUILD_DATE;
|
||||
int m = BUILD_MONTH;
|
||||
int y = BUILD_YEAR;
|
||||
int weekday = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 + 1;
|
||||
_time[TIME_DAY] = DECtoBCD(weekday);
|
||||
|
||||
setTime(_time, TIME_ARRAY_LENGTH);
|
||||
}
|
||||
|
||||
// update -- Read all time/date registers and update the _time array
|
||||
bool DS1307::update(void)
|
||||
{
|
||||
uint8_t rtcReads[7];
|
||||
|
||||
if (i2cReadBytes(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS, rtcReads, 7))
|
||||
{
|
||||
for (int i=0; i<TIME_ARRAY_LENGTH; i++)
|
||||
{
|
||||
_time[i] = rtcReads[i];
|
||||
}
|
||||
|
||||
_time[TIME_SECONDS] &= 0x7F; // Mask out CH bit
|
||||
|
||||
if (_time[TIME_HOURS] & TWELVE_HOUR_MODE)
|
||||
{
|
||||
if (_time[TIME_HOURS] & TWELVE_HOUR_PM)
|
||||
_pm = true;
|
||||
else
|
||||
_pm = false;
|
||||
_time[TIME_HOURS] &= 0x1F; // Mask out 24-hour bit from hours
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// getSecond -- read/return seconds register of DS1307
|
||||
uint8_t DS1307::getSecond(void)
|
||||
{
|
||||
_time[TIME_SECONDS] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS);
|
||||
_time[TIME_SECONDS] &= 0x7F; // Mask out CH bit
|
||||
|
||||
return BCDtoDEC(_time[TIME_SECONDS]);
|
||||
}
|
||||
|
||||
// getMinute -- read/return minutes register of DS1307
|
||||
uint8_t DS1307::getMinute(void)
|
||||
{
|
||||
_time[TIME_MINUTES] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_MINUTES);
|
||||
|
||||
return BCDtoDEC(_time[TIME_MINUTES]);
|
||||
}
|
||||
|
||||
// getHour -- read/return hour register of DS1307
|
||||
uint8_t DS1307::getHour(void)
|
||||
{
|
||||
uint8_t hourRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS);
|
||||
|
||||
if (hourRegister & TWELVE_HOUR_MODE)
|
||||
hourRegister &= 0x1F; // Mask out am/pm, 24-hour bit
|
||||
_time[TIME_HOURS] = hourRegister;
|
||||
|
||||
return BCDtoDEC(_time[TIME_HOURS]);
|
||||
}
|
||||
|
||||
// getDay -- read/return day register of DS1307
|
||||
uint8_t DS1307::getDay(void)
|
||||
{
|
||||
_time[TIME_DAY] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_DAY);
|
||||
|
||||
return BCDtoDEC(_time[TIME_DAY]);
|
||||
}
|
||||
|
||||
// getDate -- read/return date register of DS1307
|
||||
uint8_t DS1307::getDate(void)
|
||||
{
|
||||
_time[TIME_DATE] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_DATE);
|
||||
|
||||
return BCDtoDEC(_time[TIME_DATE]);
|
||||
}
|
||||
|
||||
// getMonth -- read/return month register of DS1307
|
||||
uint8_t DS1307::getMonth(void)
|
||||
{
|
||||
_time[TIME_MONTH] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_MONTH);
|
||||
|
||||
return BCDtoDEC(_time[TIME_MONTH]);
|
||||
}
|
||||
|
||||
// getYear -- read/return year register of DS1307
|
||||
uint8_t DS1307::getYear(void)
|
||||
{
|
||||
_time[TIME_YEAR] = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_YEAR);
|
||||
|
||||
return BCDtoDEC(_time[TIME_YEAR]);
|
||||
}
|
||||
|
||||
// setSecond -- set the second register of the DS1307
|
||||
bool DS1307::setSecond(uint8_t s)
|
||||
{
|
||||
if (s <= 59)
|
||||
{
|
||||
uint8_t _s = DECtoBCD(s);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS, _s);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setMinute -- set the minute register of the DS1307
|
||||
bool DS1307::setMinute(uint8_t m)
|
||||
{
|
||||
if (m <= 59)
|
||||
{
|
||||
uint8_t _m = DECtoBCD(m);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_MINUTES, _m);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setHour -- set the hour register of the DS1307
|
||||
bool DS1307::setHour(uint8_t h)
|
||||
{
|
||||
if (h <= 23)
|
||||
{
|
||||
uint8_t _h = DECtoBCD(h);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS, _h);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setDay -- set the day register of the DS1307
|
||||
bool DS1307::setDay(uint8_t d)
|
||||
{
|
||||
if ((d >= 1) && (d <= 7))
|
||||
{
|
||||
uint8_t _d = DECtoBCD(d);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_DAY, _d);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setDate -- set the date register of the DS1307
|
||||
bool DS1307::setDate(uint8_t d)
|
||||
{
|
||||
if (d <= 31)
|
||||
{
|
||||
uint8_t _d = DECtoBCD(d);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_DATE, _d);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setMonth -- set the month register of the DS1307
|
||||
bool DS1307::setMonth(uint8_t mo)
|
||||
{
|
||||
if ((mo >= 1) && (mo <= 12))
|
||||
{
|
||||
uint8_t _mo = DECtoBCD(mo);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_MONTH, _mo);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// setYear -- set the year register of the DS1307
|
||||
bool DS1307::setYear(uint8_t y)
|
||||
{
|
||||
if (y <= 99)
|
||||
{
|
||||
uint8_t _y = DECtoBCD(y);
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_YEAR, _y);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// set12Hour -- set (or not) to 12-hour mode) | enable12 defaults to true
|
||||
bool DS1307::set12Hour(bool enable12)
|
||||
{
|
||||
if (enable12)
|
||||
set24Hour(false);
|
||||
else
|
||||
set24Hour(true);
|
||||
}
|
||||
|
||||
// set24Hour -- set (or not) to 24-hour mode) | enable24 defaults to true
|
||||
bool DS1307::set24Hour(bool enable24)
|
||||
{
|
||||
uint8_t hourRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS);
|
||||
|
||||
bool hour12 = hourRegister & TWELVE_HOUR_MODE;
|
||||
if ((hour12 && !enable24) || (!hour12 && enable24))
|
||||
return true;
|
||||
|
||||
uint8_t oldHour = hourRegister & 0x1F; // Mask out am/pm and 12-hour mode
|
||||
oldHour = BCDtoDEC(oldHour); // Convert to decimal
|
||||
uint8_t newHour = oldHour;
|
||||
|
||||
if (enable24)
|
||||
{
|
||||
bool hourPM = hourRegister & TWELVE_HOUR_PM;
|
||||
if ((hourPM) && (oldHour >= 1)) newHour += 12;
|
||||
else if (!(hourPM) && (oldHour == 12)) newHour = 0;
|
||||
newHour = DECtoBCD(newHour);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oldHour == 0)
|
||||
newHour = 12;
|
||||
else if (oldHour >= 13)
|
||||
newHour -= 12;
|
||||
|
||||
newHour = DECtoBCD(newHour);
|
||||
newHour |= TWELVE_HOUR_MODE; // Set bit 6 to set 12-hour mode
|
||||
if (oldHour >= 12)
|
||||
newHour |= TWELVE_HOUR_PM; // Set PM bit if necessary
|
||||
}
|
||||
|
||||
return i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS, newHour);
|
||||
}
|
||||
|
||||
// is12Hour -- check if the DS1307 is in 12-hour mode
|
||||
bool DS1307::is12Hour(void)
|
||||
{
|
||||
uint8_t hourRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS);
|
||||
|
||||
return hourRegister & TWELVE_HOUR_MODE;
|
||||
}
|
||||
|
||||
// pm -- Check if 12-hour state is AM or PM
|
||||
bool DS1307::pm(void) // Read bit 5 in hour byte
|
||||
{
|
||||
uint8_t hourRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_HOURS);
|
||||
|
||||
return hourRegister & TWELVE_HOUR_PM;
|
||||
}
|
||||
|
||||
// enable -- enable the DS1307's oscillator
|
||||
void DS1307::enable(void) // Write 0 to CH bit
|
||||
{
|
||||
uint8_t secondRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS);
|
||||
|
||||
secondRegister &= ~(1<<7);
|
||||
|
||||
i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS, secondRegister);
|
||||
}
|
||||
|
||||
// disable -- disable the DS1307's oscillator
|
||||
void DS1307::disable(void) // Write 1 to CH bit
|
||||
{
|
||||
uint8_t secondRegister = i2cReadByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS);
|
||||
|
||||
secondRegister |= (1<<7);
|
||||
|
||||
i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_SECONDS, secondRegister);
|
||||
}
|
||||
|
||||
// writeSQW -- Set the SQW pin high or low
|
||||
void DS1307::writeSQW(uint8_t high)
|
||||
{
|
||||
if (high)
|
||||
writeSQW(SQW_HIGH);
|
||||
else
|
||||
writeSQW(SQW_LOW);
|
||||
}
|
||||
|
||||
// writeSQW -- Set the SQW pin high, low, or to one of the square wave frequencies
|
||||
void DS1307::writeSQW(sqw_rate value)
|
||||
{
|
||||
uint8_t controlRegister = 0;
|
||||
if (value == SQW_HIGH)
|
||||
{
|
||||
controlRegister |= CONTROL_BIT_OUT;
|
||||
}
|
||||
else if (value == SQW_LOW)
|
||||
{
|
||||
// Do nothing, just leave 0
|
||||
}
|
||||
else
|
||||
{
|
||||
controlRegister |= CONTROL_BIT_SQWE;
|
||||
controlRegister |= value;
|
||||
}
|
||||
|
||||
i2cWriteByte(DS1307_RTC_ADDRESS, DS1307_REGISTER_CONTROL, controlRegister);
|
||||
}
|
||||
|
||||
// BCDtoDEC -- convert binary-coded decimal (BCD) to decimal
|
||||
uint8_t DS1307::BCDtoDEC(uint8_t val)
|
||||
{
|
||||
return ( ( val / 0x10) * 10 ) + ( val % 0x10 );
|
||||
}
|
||||
|
||||
// BCDtoDEC -- convert decimal to binary-coded decimal (BCD)
|
||||
uint8_t DS1307::DECtoBCD(uint8_t val)
|
||||
{
|
||||
return ( ( val / 10 ) * 0x10 ) + ( val % 10 );
|
||||
}
|
||||
|
||||
// i2cWriteBytes -- write a set number of bytes to an i2c device, incrementing from a register
|
||||
bool DS1307::i2cWriteBytes(uint8_t deviceAddress, ds1307_registers reg, uint8_t * values, uint8_t len)
|
||||
{
|
||||
Wire.beginTransmission(deviceAddress);
|
||||
Wire.write(reg);
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
Wire.write(values[i]);
|
||||
}
|
||||
Wire.endTransmission();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// i2cWriteByte -- write a byte value to an i2c device's register
|
||||
bool DS1307::i2cWriteByte(uint8_t deviceAddress, ds1307_registers reg, uint8_t value)
|
||||
{
|
||||
Wire.beginTransmission(deviceAddress);
|
||||
Wire.write(reg);
|
||||
Wire.write(value);
|
||||
Wire.endTransmission();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// i2cReadByte -- read a byte from an i2c device's register
|
||||
uint8_t DS1307::i2cReadByte(uint8_t deviceAddress, ds1307_registers reg)
|
||||
{
|
||||
uint8_t readTemp;
|
||||
|
||||
Wire.beginTransmission(deviceAddress);
|
||||
Wire.write(reg);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.requestFrom(deviceAddress, (uint8_t) 1);
|
||||
|
||||
return Wire.read();
|
||||
}
|
||||
|
||||
// i2cReadBytes -- read a set number of bytes from an i2c device, incrementing from a register
|
||||
bool DS1307::i2cReadBytes(uint8_t deviceAddress, ds1307_registers reg, uint8_t * dest, uint8_t len)
|
||||
{
|
||||
Wire.beginTransmission(deviceAddress);
|
||||
Wire.write(reg);
|
||||
Wire.endTransmission();
|
||||
|
||||
Wire.requestFrom(deviceAddress, len);
|
||||
for (int i=0; i<len; i++)
|
||||
{
|
||||
dest[i] = Wire.read();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DS1307 rtc; // Use rtc in sketches
|
|
@ -0,0 +1,179 @@
|
|||
/******************************************************************************
|
||||
SparkFunDS1307RTC.h
|
||||
Jim Lindblom @ SparkFun Electronics
|
||||
original creation date: October 2, 2016
|
||||
https://github.com/sparkfun/SparkFun_DS1307_RTC_Arduino_Library
|
||||
|
||||
Prototypes the DS1307 class. Defines sketch-usable global variables.
|
||||
|
||||
Resources:
|
||||
Wire.h - Arduino I2C Library
|
||||
|
||||
Development environment specifics:
|
||||
Arduino 1.6.8
|
||||
SparkFun RedBoard
|
||||
SparkFun Real Time Clock Module (v14)
|
||||
******************************************************************************/
|
||||
#ifndef SPARKFUNDS1307RTC_H
|
||||
#define SPARKFUNDS1307RTC_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define DS1307_RTC_ADDRESS 0x68 // DS1307 only has one I2C address - 0x68
|
||||
|
||||
#define TWELVE_HOUR_MODE (1<<6) // 12/24-hour Mode bit in Hour register
|
||||
#define TWELVE_HOUR_PM (1<<5) // am/pm bit in hour register
|
||||
|
||||
#define AM 0
|
||||
#define PM 1
|
||||
|
||||
#define CONTROL_BIT_RS0 (1<<0) // RS0 bit in control register
|
||||
#define CONTROL_BIT_RS1 (1<<1) // RS1 bit in control register
|
||||
#define CONTROL_BIT_SQWE (1<<4) // Square wave enable bit in control register
|
||||
#define CONTROL_BIT_OUT (1<<7) // SQW Output value in control register
|
||||
|
||||
#define TIME_ARRAY_LENGTH 7 // Total number of writable values in device
|
||||
enum time_order {
|
||||
TIME_SECONDS, // 0
|
||||
TIME_MINUTES, // 1
|
||||
TIME_HOURS, // 2
|
||||
TIME_DAY, // 3
|
||||
TIME_DATE, // 4
|
||||
TIME_MONTH, // 5
|
||||
TIME_YEAR, // 6
|
||||
};
|
||||
|
||||
// sqw_rate -- enum for possible SQW pin output settings
|
||||
enum sqw_rate {
|
||||
SQW_SQUARE_1,
|
||||
SQW_SQUARE_4K,
|
||||
SQW_SQUARE_8K,
|
||||
SQW_SQUARE_32K,
|
||||
SQW_LOW,
|
||||
SQW_HIGH
|
||||
};
|
||||
|
||||
// ds1307_register -- Definition of DS1307 registers
|
||||
enum ds1307_registers {
|
||||
DS1307_REGISTER_SECONDS, // 0x00
|
||||
DS1307_REGISTER_MINUTES, // 0x01
|
||||
DS1307_REGISTER_HOURS, // 0x02
|
||||
DS1307_REGISTER_DAY, // 0x03
|
||||
DS1307_REGISTER_DATE, // 0x04
|
||||
DS1307_REGISTER_MONTH, // 0x05
|
||||
DS1307_REGISTER_YEAR, // 0x06
|
||||
DS1307_REGISTER_CONTROL // 0x07
|
||||
};
|
||||
|
||||
// Base register for complete time/date readings
|
||||
#define DS1307_REGISTER_BASE DS1307_REGISTER_SECONDS
|
||||
|
||||
// dayIntToStr -- convert day integer to the string
|
||||
static const char *dayIntToStr[7] {
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday"
|
||||
};
|
||||
|
||||
// dayIntToChar -- convert day integer to character
|
||||
static const char dayIntToChar[7] = {'U', 'M', 'T', 'W', 'R', 'F', 'S'};
|
||||
|
||||
class DS1307
|
||||
{
|
||||
public:
|
||||
|
||||
////////////////////
|
||||
// Initialization //
|
||||
////////////////////
|
||||
// Constructor -- Initialize class variables to 0
|
||||
DS1307();
|
||||
// Begin -- Initialize I2C interface
|
||||
void begin(void);
|
||||
|
||||
///////////////////////
|
||||
// Setting the Clock //
|
||||
///////////////////////
|
||||
// setTime -- Set time and date/day registers of DS1307
|
||||
bool setTime(uint8_t sec, uint8_t min, uint8_t hour,
|
||||
uint8_t day, uint8_t date, uint8_t month, uint8_t year);
|
||||
// setTime -- Set time and date/day registers of DS1307 (using data array)
|
||||
bool setTime(uint8_t * time, uint8_t len);
|
||||
// autoTime -- Set time with compiler time/date
|
||||
bool autoTime();
|
||||
|
||||
// To set specific values of the clock, use the set____ functions:
|
||||
bool setSecond(uint8_t s);
|
||||
bool setMinute(uint8_t m);
|
||||
bool setHour(uint8_t h);
|
||||
bool setDay(uint8_t d);
|
||||
bool setDate(uint8_t d);
|
||||
bool setMonth(uint8_t mo);
|
||||
bool setYear(uint8_t y);
|
||||
|
||||
///////////////////////
|
||||
// Reading the Clock //
|
||||
///////////////////////
|
||||
// update -- Read all time/date registers and update the _time array
|
||||
bool update(void);
|
||||
// update should be performed before any of the following. It will update
|
||||
// all values at one time.
|
||||
inline uint8_t second(void) { return BCDtoDEC(_time[TIME_SECONDS]); };
|
||||
inline uint8_t minute(void) { return BCDtoDEC(_time[TIME_MINUTES]); };
|
||||
inline uint8_t hour(void) { return BCDtoDEC(_time[TIME_HOURS]); };
|
||||
inline uint8_t day(void) { return BCDtoDEC(_time[TIME_DAY]); };
|
||||
inline const char dayChar(void) { return dayIntToChar[BCDtoDEC(_time[TIME_DAY]) - 1]; };
|
||||
inline const char * dayStr(void) { return dayIntToStr[BCDtoDEC(_time[TIME_DAY]) - 1]; };
|
||||
inline uint8_t date(void) { return BCDtoDEC(_time[TIME_DATE]); };
|
||||
inline uint8_t month(void) { return BCDtoDEC(_time[TIME_MONTH]); };
|
||||
inline uint8_t year(void) { return BCDtoDEC(_time[TIME_YEAR]); };
|
||||
|
||||
// To read a single value at a time, use the get___ functions:
|
||||
uint8_t getSecond(void);
|
||||
uint8_t getMinute(void);
|
||||
uint8_t getHour(void);
|
||||
uint8_t getDay(void);
|
||||
uint8_t getDate(void);
|
||||
uint8_t getMonth(void);
|
||||
uint8_t getYear(void);
|
||||
|
||||
// is12Hour -- check if the DS1307 is in 12-hour mode | returns true if 12-hour mode
|
||||
bool is12Hour(void);
|
||||
// pm -- Check if 12-hour state is AM or PM | returns true if PM
|
||||
bool pm(void);
|
||||
|
||||
///////////////////////////////
|
||||
// SQW Pin Control Functions //
|
||||
///////////////////////////////
|
||||
void writeSQW(uint8_t high); // Write SQW pin high or low
|
||||
void writeSQW(sqw_rate value); // Write SQW pin high, low, or to a set rate
|
||||
|
||||
/////////////////////////////
|
||||
// Misc. Control Functions //
|
||||
/////////////////////////////
|
||||
void enable(void); // Enable the oscillator
|
||||
void disable(void); // Disable the oscillator (no counting!)
|
||||
|
||||
bool set12Hour(bool enable12 = true); // Enable/disable 12-hour mode
|
||||
bool set24Hour(bool enable24 = true); // Enable/disable 24-hour mode
|
||||
|
||||
private:
|
||||
uint8_t _time[TIME_ARRAY_LENGTH];
|
||||
bool _pm;
|
||||
|
||||
uint8_t BCDtoDEC(uint8_t val);
|
||||
uint8_t DECtoBCD(uint8_t val);
|
||||
|
||||
bool i2cWriteBytes(uint8_t deviceAddress, ds1307_registers reg, uint8_t * values, uint8_t len);
|
||||
bool i2cWriteByte(uint8_t deviceAddress, ds1307_registers reg, uint8_t value);
|
||||
uint8_t i2cReadByte(uint8_t deviceAddress, ds1307_registers reg);
|
||||
bool i2cReadBytes(uint8_t deviceAddress, ds1307_registers reg, uint8_t * dest, uint8_t len);
|
||||
};
|
||||
|
||||
extern DS1307 rtc;
|
||||
|
||||
#endif // SPARKFUNDS1307RTC_H
|
|
@ -27,6 +27,10 @@
|
|||
// uncomment the line below if you want to use this sketch that writes to the EEPROM and accept the responsibility stated above! :-)
|
||||
//#define EEPROM_WRITES
|
||||
|
||||
// uncomment the line below if you have fitted the Sparkfun DS1307 RTC module to the expansion header
|
||||
// if no RTC module is fitted, this line should be commented out
|
||||
#define RTC_FITTED
|
||||
|
||||
// Include libraries
|
||||
#include <Arduino.h> // required for PlatformIO IDE (not required if you're using Arduino IDE)
|
||||
#include <SPI.h>
|
||||
|
@ -35,6 +39,8 @@
|
|||
#include <Adafruit_I2CDevice.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include <ft817.h>
|
||||
#include <SparkFunDS1307RTC.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// Declarations
|
||||
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); // pins for (CLK,DIN,D/C,CE,RST)
|
||||
|
@ -44,15 +50,16 @@ FT817 radio; // define “radio” so that we may pass CAT and EEPR
|
|||
SoftwareSerial expansion(12,11);
|
||||
|
||||
// Define PCB pins
|
||||
#define backlightPin 8 // backlight output pin (not the "LIGHT" input button!)
|
||||
#define buttonPin A0 // SW1-SW6 arrive as different levels on analog input A0
|
||||
#define sw7pin 2 // SW7 input is D2
|
||||
#define sw8pin 10 // SW8 input is D10
|
||||
#define sw9pin 9 // SW9 input is D9
|
||||
#define backlightPin 8 // backlight output pin (not the "LIGHT" input button!)
|
||||
#define buttonPin A0 // SW1-SW6 arrive as different levels on analog input A0
|
||||
#define sw7pin 11 // SW7 input is D11
|
||||
#define sw8pin 10 // SW8 input is D10
|
||||
#define sw9pin 9 // SW9 input is D9
|
||||
#define keyerSw1 15 // A1 (pin 15) is Keyer SW1
|
||||
#define keyerSw2 16 // A1 (pin 16) is Keyer SW2
|
||||
#define keyerSw3 17 // A1 (pin 17) is Keyer SW3
|
||||
#define keyerSw4 18 // A1 (pin 18) is Keyer SW4
|
||||
#define keyerSw2 16 // A2 (pin 16) is Keyer SW2
|
||||
#define keyerSw3 17 // A3 (pin 17) is Keyer SW3
|
||||
#define keyerSw4 13 // D13 is Keyer SW4
|
||||
#define SQW_INPUT_PIN 2 // Input pin to read RTC square wave
|
||||
|
||||
|
||||
// Global variables - g7uhn TO DO: Needs a big tidy up here
|
||||
|
@ -96,12 +103,13 @@ void pressKeyerSw2(); // no FT-817 interaction
|
|||
void pressKeyerSw3(); // no FT-817 interaction
|
||||
void pressKeyerSw4(); // no FT-817 interaction
|
||||
void longPressKeyerSw1(); // no FT-817 interaction
|
||||
void incrementSeconds(); // no FT-817 interaction
|
||||
|
||||
|
||||
///////////// SETUP YOUR SOFT-KEY PAGES HERE! ////////////////////////
|
||||
// Single functions from the ft817 library can be called straight from the pageXSoftKeyFunctionY function above
|
||||
// Single functions from the ft817 library can be called straight from the pageXSoftKeyFunctionY function
|
||||
// or, if the desired function is a combination of actions, define a function in the USER FUNCTIONS section below
|
||||
// to be called by the sketch. Don't forget to forward declare your user function above.
|
||||
// to be called by the sketch. If you make a new function, don't forget to forward declare your user function above.
|
||||
|
||||
// Page0 items
|
||||
// SOFT-KEY 1
|
||||
|
@ -129,10 +137,11 @@ String page0SoftkeyLabel6 = "NAR"; // 3 characters
|
|||
void page0SoftkeyFunction6() {radio.toggleNar();} // EEPROM write
|
||||
boolean page0SoftkeyStatus6() {return radio.getNar();} // EEPROM read
|
||||
|
||||
|
||||
// Page1 items
|
||||
// SOFT-KEY 1
|
||||
String page1SoftkeyLabel1 = "k13 "; // 6 characters
|
||||
void page1SoftkeyFunction1() {radio.setKeyerSpeed(13);} // EEPROM write
|
||||
String page1SoftkeyLabel1 = "getRTC"; // 6 characters
|
||||
void page1SoftkeyFunction1() {rtc.update(); ss = rtc.second(); mm = rtc.minute(); hh = rtc.hour();} // get update from RTC and update Arduino's hh/mm/ss
|
||||
boolean page1SoftkeyStatus1() {}
|
||||
// SOFT-KEY 2
|
||||
String page1SoftkeyLabel2 = "hh+"; // 3 characters
|
||||
|
@ -143,8 +152,8 @@ String page1SoftkeyLabel3 = "hh-"; // 3 characters
|
|||
void page1SoftkeyFunction3() {if(hh==0) {hh=23;} else{hh = (hh - 1) % 24;}; tickOver = millis(); delay(300);}
|
||||
boolean page1SoftkeyStatus3() {}
|
||||
// SOFT-KEY 4
|
||||
String page1SoftkeyLabel4 = " k17"; // 6 characters
|
||||
void page1SoftkeyFunction4() {radio.setKeyerSpeed(17);} // EEPROM write
|
||||
String page1SoftkeyLabel4 = "setRTC"; // 6 characters
|
||||
void page1SoftkeyFunction4() {rtc.setTime(0, mm, hh, 1, 1, 1, 21);} // set RTC to hh:mm on the 1/1/21 (we're not interested in dates here, only time)
|
||||
boolean page1SoftkeyStatus4() {}
|
||||
// SOFT-KEY 5
|
||||
String page1SoftkeyLabel5 = "mm+"; // 3 characters
|
||||
|
@ -183,6 +192,7 @@ void page2SoftkeyFunction6() {pressKeyerSw4();}
|
|||
boolean page2SoftkeyStatus6() {}
|
||||
|
||||
/////////////// END OF SOFT-KEY PAGE SETUP ////////////////////
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
void setup(void)
|
||||
|
@ -190,6 +200,13 @@ void setup(void)
|
|||
// Start serial
|
||||
radio.begin(38400); // start the serial port for the CAT library
|
||||
|
||||
#ifdef RTC_FITTED
|
||||
// Initialise RTC library
|
||||
rtc.begin();
|
||||
rtc.writeSQW(SQW_SQUARE_1); // Sets SQW output to 1Hz
|
||||
attachInterrupt(digitalPinToInterrupt(SQW_INPUT_PIN), incrementSeconds, RISING);
|
||||
#endif
|
||||
|
||||
// Start expansion software serial
|
||||
//expansion.begin(115200); // start the expansion serial port at 115200 baud for GPS module
|
||||
|
||||
|
@ -202,6 +219,7 @@ void setup(void)
|
|||
pinMode(keyerSw2, OUTPUT);
|
||||
pinMode(keyerSw3, OUTPUT);
|
||||
pinMode(keyerSw4, OUTPUT);
|
||||
pinMode(SQW_INPUT_PIN, INPUT_PULLUP);
|
||||
|
||||
digitalWrite(backlightPin, LOW);
|
||||
digitalWrite(keyerSw1, LOW);
|
||||
|
@ -211,7 +229,7 @@ void setup(void)
|
|||
|
||||
// Initialize Display
|
||||
display.begin();
|
||||
display.setContrast(57); // you can change the contrast around to adapt the display for the best viewing!
|
||||
display.setContrast(62); // you can change the contrast around to adapt the display for the best viewing! 57 is default and good for Sparkfun
|
||||
display.clearDisplay(); // Clear the buffer.
|
||||
|
||||
// Set up the timer interrupt to watch for button status at 50 Hz (needed to catch a quick button press)
|
||||
|
@ -231,6 +249,14 @@ void setup(void)
|
|||
drawMainDisplay();
|
||||
changePage();
|
||||
|
||||
#ifdef RTC_FITTED
|
||||
// Get RTC time (non-blocking and clock will start at 00:00 if no RTC available????)
|
||||
rtc.update();
|
||||
ss = rtc.second();
|
||||
mm = rtc.minute();
|
||||
hh = rtc.hour();
|
||||
#endif
|
||||
|
||||
} // end setup
|
||||
|
||||
|
||||
|
@ -369,15 +395,17 @@ void loop() // MAIN LOOP
|
|||
display.setCursor(27, 12);
|
||||
display.print(buffer);
|
||||
|
||||
// Calculate time value (either uptime or UTC if GPS expansion has updated the values)
|
||||
|
||||
#ifndef RTC_FITTED // if RTC not fitted, use this clunky way out counting up the time in the main loop (RTC method uses hardware interrupt)
|
||||
// Calculate time value (at boot this is uptime or user can set hh:mm values, install RTC module to avoid having to set this every boot)
|
||||
if ( millis() - tickOver > 59999) {
|
||||
if (mm == 59) {
|
||||
hh = (hh + 1) % 24; // If mm == 59, increment hh modulo 24
|
||||
}
|
||||
mm = (mm + 1) % 60; // Increment mm modulo 60
|
||||
// What do we do about incrementing the date???????? :-/
|
||||
tickOver = millis();
|
||||
}
|
||||
#endif
|
||||
|
||||
delay(200);
|
||||
|
||||
|
@ -454,6 +482,19 @@ ISR(TIMER1_COMPA_vect)
|
|||
}
|
||||
|
||||
|
||||
// Increment the clock driven by RTC 1Hz square wave interrupt
|
||||
void incrementSeconds()
|
||||
{
|
||||
if (ss == 59) {
|
||||
if (mm == 59) {
|
||||
hh = (hh + 1) % 24; // if second = 59 AND minute = 59, increment the hour, modulo 24
|
||||
}
|
||||
mm = (mm + 1) % 60; // if second = 59, increment the minute, modulo 60
|
||||
}
|
||||
ss = (ss + 1) % 60; // increment the second, modulo 60
|
||||
}
|
||||
|
||||
|
||||
// Draw main display
|
||||
void drawMainDisplay()
|
||||
{
|
||||
|
|
Ładowanie…
Reference in New Issue