1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
//---------------------------------------------
//ATtiny85 - DS3231 RTC Read Time, Date & Temp
//---------------------------------------------
#include <TinyWireM.h>
//----------------------
extern "C" {void delay_ms(); void delay_us();} // <=================================
//---------------------------------------------
byte numeral[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
byte read_DS3231[7];
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_DS3231[0] = TinyWireM.receive(); //read seconds
read_DS3231[1] = TinyWireM.receive(); //read minutes
read_DS3231[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_DS3231[3] = TinyWireM.receive(); //read day
read_DS3231[4] = TinyWireM.receive(); //read month
read_DS3231[5] = TinyWireM.receive(); //read year
}
delay_ms();
//--------------------------------------
//write to set pointer register to temp
//--------------------------------------
TinyWireM.beginTransmission(0x68);
TinyWireM.send(0x11); //point to Temp register
TinyWireM.endTransmission();
//----------
//read temp
//----------
TinyWireM.requestFrom(0x68, 1);
if(TinyWireM.available())
{
read_DS3231[6] = TinyWireM.receive(); //read integer part of temp
}
delay_ms();
//----------------------------------------------------------------
if(millis() - millisCapture > myDelay) disp_date_temp();
//----------------------------------------------------------------
if((read_DS3231[2] & 0x10) >> 4 == 0)
{
send_byte(0xC0); send_byte(0x00); //turn OFF digit 0
}
else disp_digit(0xC0, ((read_DS3231[2] & 0x10) >> 4));
//----------------------------------------------------------------
if(colon_ONOFF == true) disp_colon(0xC1, (read_DS3231[2] & 0x0F));
else disp_digit(0xC1, (read_DS3231[2] & 0x0F));
//----------------------------------------------------------------
disp_digit(0xC2, ((read_DS3231[1] & 0xF0) >> 4));
disp_digit(0xC3, (read_DS3231[1] & 0x0F));
//----------------------------------------------------------------
byte AM_PM = read_DS3231[2] & 0x20;
if(AM_PM == 0x20) digitalWrite(1, HIGH);
else digitalWrite(1, LOW);
//----------------------------------------------------------------
delay(2000);
}
//===================================================================
void disp_date_temp()
{
clr_disp(); delay(500);
//---------------------------------------------------
disp_digit(0xC1, (read_DS3231[3] & 0x0F));
disp_digit(0xC0, ((read_DS3231[3] & 0xF0) >> 4));
//---------------------------------------------------
disp_digit(0xC3, (read_DS3231[4] & 0x0F));
disp_digit(0xC2, ((read_DS3231[4] & 0x10) >> 4));
//---------------------------------------------------
delay(2000);
clr_disp(); delay(500);
//---------------------------------------------------
disp_digit(0xC0, 2);
disp_digit(0xC1, 0);
disp_digit(0xC3, (read_DS3231[5] & 0x0F));
disp_digit(0xC2, ((read_DS3231[5] & 0xF0) >> 4));
//---------------------------------------------------
delay(2000);
clr_disp(); delay(500);
//---------------------------------------------------
disp_digit(0xC0, (read_DS3231[6]/10));
disp_digit(0xC1, (read_DS3231[6]%10));
send_byte(0xC2); send_byte(0x63);
send_byte(0xC3); send_byte(0x39);
//---------------------------------------------------
delay(2000);
clr_disp(); delay(500);
//---------------------------------------------------
millisCapture = millis();
}
//===================================================================
void disp_digit(byte address, byte 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)
{
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();
} |
Partager