часы + будильник
#include <EEPROM.h>
#include <DS3231.h>
#include <Wire.h>
#include <STM32_TM1637.h>
DS3231 clock;
RTCDateTime DateTime;
STM32_TM1637 tm(PD2,PD3);// CLK, DIO
int timer;
byte hour,minut,hh,mm,sett;
bool w,ww,alarm;
// PD0, PD1(2, 3) = RXD TXD
// PC3(26) = SQW DS3231
// PC2(25) = HOUR+
// PC1(24) = MINUTE+
// PC0(23) = SETTING
// PC4(27) = SDA
// PC5(28) = SCL
// PD2, PD3(4, 5) = CLK DIO TM1637
// PD6(12) = LED ALARM
void setup(){
Wire.begin();
clock.begin();
tm.brig(7);
clock.setOutput(DS3231_1HZ);
PORTC |= (1 << 2)|(1 << 1)|(1 << 0); // INPUT_PULLUP PC0 PC1 PC2
DDRB |= (1 << 5);
hh = EEPROM.read(0);mm = EEPROM.read(1);alarm = EEPROM.read(2);
}
void loop(){
DateTime = clock.getDateTime();
hour = DateTime.hour;
minut = DateTime.minute;
timer = hour*100 + minut;
if(sett == 0) {tm.print_time(timer, ((PINC >> 3) & 1));}
if(sett == 1) {tm.print_time(timer, 1);}
if(sett == 2) {tm.print_time(hh*100+mm, 0);}
if(((PINC >> 2) & 1) == LOW && sett == 1){ /////// BUTTON PC2 = HOUR+
w=1; hour++; if(hour>23){hour=0;} delay(300);}
if(((PINC >> 1) & 1) == LOW && sett == 1){ /////// BUTTON PC1 = MINUTE+
w=1; minut++; if(minut>59){minut=1;} delay(300);}
if(((PINC >> 2) & 1) == LOW && sett == 2){ /////// BUTTON ALARM PC2 = HOUR+
ww=1; hh++; if(hh>23){hh=0;} delay(300);}
if(((PINC >> 1) & 1) == LOW && sett == 2){ /////// BUTTON ALARM PC1 = MINUTE+
ww=1; mm++; if(mm>59){mm=0;} delay(300);}
if(((PINC >> 2) & 1) == LOW && sett == 0){ /////// BUTTON PC2 = alarm on
alarm = 1; EEPROM.update(2,alarm);delay(300);}
if(((PINC >> 1) & 1) == LOW && sett == 0){ /////// BUTTON PC1 = alarm off
alarm = 0; EEPROM.update(2,alarm);delay(300);}
if(alarm == 1){PORTB |= (1 << 5);}else{PORTB &= ~(1 << 5);}
//// setting
if(((PINC >> 0) & 1) == LOW){sett++;if(sett > 2){sett=0;}delay(300);}
////////// SET
if(w == 1){w=0; clock.setDateTime(2020, 8, 20, hour, minut, 0);}
if(ww == 1){ww=0; EEPROM.update(0,hh);EEPROM.update(1,mm);}
if((hh*100+mm == hour*100+minut) && alarm==1){tone(6,1500,150);}
// delay(100);
}