// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Uses my Bit Bang I2C library. You can find it here:
// https://github.com/bitbank2/BitBang_I2C
#include <BitBang_I2C.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String daysW[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
LiquidCrystal lcd( 12, 11, 5, 4, 3, 2 );
LiquidCrystal_I2C lcd2(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
BBI2C bbi2c;
uint8_t rtc_adr = 0x68; // RTC address
uint8_t bcd2bin(uint8_t);
uint8_t bin2bcd(uint8_t);
void printDec( int, byte );
DateTime read_now(int);
uint8_t isRunning(void);
void adjust(const DateTime &dt);
void setup() {
Serial.begin(115200);
memset(&bbi2c, 0, sizeof(bbi2c));
lcd.begin( 16, 2 );
lcd.clear();
lcd2.init();
lcd2.backlight();
lcd2.clear();
// BBI2C bbi2c;
bbi2c.bWire =0; // use bit banging
bbi2c.iSDA = 8; // SDA on GPIO pin 8
bbi2c.iSCL = 9; // SCL on GPIO pin 9
I2CInit(&bbi2c, 100000L);
lcd.print( "Setting Time" );
delay(100); // allow devices to power up
adjust(DateTime(2023, 8, 14, 11, 12, 10));
delay(1000); // allow devices to power up
}
int loop_counter = 0;
void loop() {
// DateTime now;
loop_counter++;
Serial.println("Starting I2C Test");
lcd.clear();
lcd2.clear();
lcd2.print("loop_count:");
lcd2.print(loop_counter);
if(I2CTest(&bbi2c, rtc_adr))
{
Serial.println("RTC found");
if(!isRunning())
{
Serial.println("RTC is NOT running");
}
else
{
Serial.println("RTC is running");
}
DateTime now = read_now(0);
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
lcd.setCursor(2, 0);
lcd.print( daysW[now.dayOfTheWeek()] );
lcd.print( " " );
printDec( now.day(), 0 );
printDec( now.month(), 1 );
printDec( now.year()-2000, 1 );
lcd.setCursor( 4, 1 );
printDec( now.hour(), 0 );
printDec( now.minute(), 2 );
printDec( now.second(), 2 );
}
else
{
Serial.println("RTC not found");
}
delay(1000);
}
uint8_t bcd2bin(uint8_t val) { return val - 6 * (val >> 4); }
uint8_t bin2bcd(uint8_t val) { return val + 6 * (val / 10); }
/**************************************************************************/
/*!
@brief Getting current time from RTC
@return DateTime with time details
*/
/**************************************************************************/
DateTime read_now(int a){
uint8_t buffer[7];
buffer[0] = 0;
// i2c_dev->write_then_read(buffer, 1, buffer, 7);
I2CReadRegister(&bbi2c, rtc_adr, 0, buffer, 7);
return DateTime(bcd2bin(buffer[6]) + 2000U, bcd2bin(buffer[5]),
bcd2bin(buffer[4]), bcd2bin(buffer[2]), bcd2bin(buffer[1]),
bcd2bin(buffer[0] & 0x7F));
}
/**************************************************************************/
/*!
@brief Is the DS1307 running? Check the Clock Halt bit in register 0
@return 1 if the RTC is running, 0 if not
*/
/**************************************************************************/
uint8_t isRunning(void) {
uint8_t buffer[1];
buffer[0] = 0;
I2CReadRegister(&bbi2c, rtc_adr, 0, buffer, 1) >> 7;
return !(buffer[0] >> 7);
}
void adjust(const DateTime &dt) {
uint8_t buffer[8] = {0,
bin2bcd(dt.second()),
bin2bcd(dt.minute()),
bin2bcd(dt.hour()),
0,
bin2bcd(dt.day()),
bin2bcd(dt.month()),
bin2bcd(dt.year() - 2000U)};
// i2c_dev->write(buffer, 8);
I2CWrite(&bbi2c, rtc_adr, buffer, 8);
}
void printDec( int dec, byte pre )
{
if ( pre == 1 ) lcd.print( "/" );
else if( pre == 2 ) lcd.print( ":" );
if( dec < 10 ) lcd.print("0");
lcd.print( dec, DEC );
}