// ATtiny85 - MAX7219 Dot Matrix
// https://akuzechie.blogspot.com/2022/01/attiny85-max7219-dot-matrix.html
// https://www.youtube.com/watch?v=0jKfozjTYIs
// ATtiny85 - DS1307 Real-Time Clock
// https://akuzechie.blogspot.com/2022/02/attiny85-ds1307-real-time-clock.html
// https://www.youtube.com/watch?v=o9HGWWoQkCo
//---------------------------------------
//ATtiny85 - DS1307 RTC Read Time & Date
//---------------------------------------
#include <TinyWireM.h>
//----------------------
extern "C" {void delay_ms(); void delay_us();}
//---------------------------------------------
byte read_DS1307[6];
byte numeral[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned long millisCapture = millis(), myDelay = 30000;
boolean colon_ONOFF = false;
//===================================================================
void setup()
{
pinMode(1, OUTPUT);
TinyWireM.begin();
TM1637_init();
}
//===================================================================
void loop()
{
//write to set pointer register to seconds
//-----------------------------------------
TinyWireM.beginTransmission(0x68);
TinyWireM.send(0x00); //point to Seconds register
TinyWireM.endTransmission();
//----------
//read time
//----------
TinyWireM.requestFrom(0x68, 3);
if(TinyWireM.available())
{
read_DS1307[0] = TinyWireM.receive(); //read seconds
read_DS1307[1] = TinyWireM.receive(); //read minutes
read_DS1307[2] = TinyWireM.receive(); //read hours
}
delay_ms();
//--------------------------------------
//write to set pointer register to date
//--------------------------------------
TinyWireM.beginTransmission(0x68);
TinyWireM.send(0x04); //point to Date register
TinyWireM.endTransmission();
//----------
//read date
//----------
TinyWireM.requestFrom(0x68, 3);
if(TinyWireM.available())
{
read_DS1307[3] = TinyWireM.receive(); //read day
read_DS1307[4] = TinyWireM.receive(); //read month
read_DS1307[5] = TinyWireM.receive(); //read year
}
delay_ms();
//----------------------------------------------------------------
if(millis() - millisCapture > myDelay) disp_date();
//----------------------------------------------------------------
if((read_DS1307[2] & 0x10) >> 4 == 0)
{
send_byte(0xC0); send_byte(0x00); //turn OFF digit 0
}
else disp_digit(0xC0, ((read_DS1307[2] & 0x10) >> 4));
//----------------------------------------------------------------
if(colon_ONOFF == true) disp_colon(0xC1, (read_DS1307[2] & 0x0F));
else disp_digit(0xC1, (read_DS1307[2] & 0x0F));
//----------------------------------------------------------------
disp_digit(0xC2, ((read_DS1307[1] & 0xF0) >> 4));
disp_digit(0xC3, (read_DS1307[1] & 0x0F));
//----------------------------------------------------------------
byte AM_PM = read_DS1307[2] & 0x20;
if(AM_PM == 0x20) digitalWrite(1, HIGH);
else digitalWrite(1, LOW);
//----------------------------------------------------------------
delay(1000);
}
//===================================================================
void disp_date()
{
clr_disp(); delay(500);
//---------------------------------------------------
disp_digit(0xC1, (read_DS1307[3] & 0x0F));
disp_digit(0xC0, ((read_DS1307[3] & 0xF0) >> 4));
//---------------------------------------------------
disp_digit(0xC3, (read_DS1307[4] & 0x0F));
disp_digit(0xC2, ((read_DS1307[4] & 0x10) >> 4));
//---------------------------------------------------
delay(2000);
clr_disp(); delay(500);
//---------------------------------------------------
disp_digit(0xC0, 2);
disp_digit(0xC1, 0);
disp_digit(0xC3, (read_DS1307[5] & 0x0F));
disp_digit(0xC2, ((read_DS1307[5] & 0xF0) >> 4));
//---------------------------------------------------
delay(2000);
clr_disp(); delay(500);
millisCapture = millis();
}
//===================================================================
void disp_digit(byte address, byte digit)
{
send_byte(address); send_byte(numeral[digit]);
// switch(digit)
// {
// case 0: send_byte(address); send_byte(numeral[0]); break;
// case 1: send_byte(address); send_byte(numeral[1]); break;
// case 2: send_byte(address); send_byte(numeral[2]); break;
// case 3: send_byte(address); send_byte(numeral[3]); break;
// case 4: send_byte(address); send_byte(numeral[4]); break;
// case 5: send_byte(address); send_byte(numeral[5]); break;
// case 6: send_byte(address); send_byte(numeral[6]); break;
// case 7: send_byte(address); send_byte(numeral[7]); break;
// case 8: send_byte(address); send_byte(numeral[8]); break;
// case 9: send_byte(address); send_byte(numeral[9]);
// }
colon_ONOFF = !colon_ONOFF;
}
//===================================================================
void disp_colon(byte address1, byte digit1)
{
send_byte(address1); send_byte(numeral[digit1]|0x80);
// switch(digit1)
// {
// case 0: send_byte(address1); send_byte(numeral[0]|0x80); break;
// case 1: send_byte(address1); send_byte(numeral[1]|0x80); break;
// case 2: send_byte(address1); send_byte(numeral[2]|0x80); break;
// case 3: send_byte(address1); send_byte(numeral[3]|0x80); break;
// case 4: send_byte(address1); send_byte(numeral[4]|0x80); break;
// case 5: send_byte(address1); send_byte(numeral[5]|0x80); break;
// case 6: send_byte(address1); send_byte(numeral[6]|0x80); break;
// case 7: send_byte(address1); send_byte(numeral[7]|0x80); break;
// case 8: send_byte(address1); send_byte(numeral[8]|0x80); break;
// case 9: send_byte(address1); send_byte(numeral[9]|0x80);
// }
send_byte(0xC1); send_byte(0x00);
colon_ONOFF = !colon_ONOFF;
}
//===================================================================
void TM1637_init()
{
send_byte(0x8F); //command for display brightness level (max)
send_byte(0x44); //command for single address mode
clr_disp(); //clear display
}
//===================================================================
void clr_disp()
{
for(byte i=0; i<=4; i++)
{
send_byte(0xC0 + i);
send_byte(0);
}
}
//===================================================================
void send_byte(byte Rx_byte)
{
DDRB |= (1 << PB4); //PB4 o/p: DIO line
DDRB |= (1 << PB3); //PB3 o/p: CLK line
//----------------------------------------------------------
PORTB &= ~(1 << PB4); //START condition
delay_us();
PORTB &= ~(1 << PB3);
delay_us();
//----------------------------------------------------------
shiftOut(4, 3, LSBFIRST, Rx_byte); //shift out byte LSB 1st
//----------------------------------------------------------
DDRB &= ~(1 << PB4); //MCU releases DIO line
delay_us();
PORTB |= (1 << PB3); //9th CLK for ACK
delay_us();
PORTB &= ~(1 << PB3);
delay_us();
DDRB |= (1 << PB4); //MCU reclaims DIO line
delay_us();
//----------------------------------------------------------
PORTB |= (1 << PB3); //END condition
delay_us();
PORTB |= (1 << PB4);
delay_us();
}