Тема: Часы на ИН-12А (ИН-15А) (LGT8F328)
// LGT8F328
#define AN1 2
#define AN2 3
#define AN3 4
#define AN4 5
#define AN5 6
#define ID1 11
#define ID2 10
#define ID4 7
#define ID8 8
#define SET A0 // BUTTON SET
#define UP A2 // BUTTON +++
#define DW A1 // BUTTON ---
#define BR 300 // 400-3000 яркость
#include <Wire.h>
#include <DS3231.h> //http://rcl-radio.ru/wp-content/uploads/2022/10/DS3231.zip
DS3231 clock;
RTCDateTime DateTime;
int an, segm,i;
byte a[5];
int dp,h,m,set,tic;
unsigned long times0;
bool x;
void setup() {
Wire.begin();clock.begin();
Serial.begin(9600);
Serial.println("ok");
pinMode(9,OUTPUT);// PWM TIMER_1
pinMode(AN1,OUTPUT);pinMode(AN2,OUTPUT);pinMode(AN3,OUTPUT);pinMode(AN4,OUTPUT);pinMode(AN5,OUTPUT);
pinMode(ID1,OUTPUT);pinMode(ID2,OUTPUT);pinMode(ID4,OUTPUT);pinMode(ID8,OUTPUT);
pinMode(SET,INPUT_PULLUP);pinMode(UP,INPUT_PULLUP);pinMode(DW,INPUT_PULLUP);
// clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
cli();
//TIMER_1 PWM +170V
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1A |= (1<<COM1A1);
TCCR1B |= (1<<WGM12);
TCCR1A |= (1<<WGM11);
TCCR1B |= (1 << CS10);
ICR1 = 511;
OCR1A = 350;
//TIMER_2
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 120;
TCCR2A |= (1 << WGM21);
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
sei();
}
void loop() {
DateTime=clock.getDateTime();// опрос времени
h = DateTime.hour;
m = DateTime.minute;
if(digitalRead(SET)==LOW){set++;if(set>2){set=0;}delay(200);}
if(set==1){
if(digitalRead(UP)==LOW){x=1;tic=0;h++;if(h>23){h=23;}delay(200);}
if(digitalRead(DW)==LOW){x=1;tic=0;h--;if(h<0){h=0;}delay(200);}
if(tic==0){a[0]=h/10%10;}else{a[0]=10;}
if(tic==0){a[1]=h%10;}else{a[1]=10;}
a[2]=7;
a[3]=m/10%10;
a[4]=m%10;
}
if(set==2){
if(digitalRead(UP)==LOW){x=1;tic=0;m++;if(m>59){m=59;}delay(200);}
if(digitalRead(DW)==LOW){x=1;tic=0;m--;if(m<0){m=0;}delay(200);}
a[0]=h/10%10;
a[1]=h%10;
a[2]=7;
if(tic==0){a[3]=m/10%10;}else{a[3]=10;}
if(tic==0){a[4]=m%10;}else{a[4]=10;}
}
if(x==1){x=0;clock.setDateTime(2024, 1, 1, h, m, 0);}
if(set==0){
if(millis()-times0<500){dp=8;}
if(millis()-times0>=500){dp=10;}
if(millis()-times0>1000){times0=millis();}
a[0]=h/10;
a[1]=h%10;
a[2]=dp;
a[3]=m/10%10;
a[4]=m%10;
}
tic++;
if(tic>1){tic=0;}
delay(100);
}
void segment(){
switch(segm){
case 0: digitalWrite(ID1,LOW);digitalWrite(ID2,LOW);digitalWrite(ID4,LOW);digitalWrite(ID8,LOW);break; // DEC 0 = 0b0000
case 1: digitalWrite(ID1,LOW);digitalWrite(ID2,LOW);digitalWrite(ID4,LOW);digitalWrite(ID8,HIGH);break; // DEC 1 = 0b0001
case 2: digitalWrite(ID1,LOW);digitalWrite(ID2,LOW);digitalWrite(ID4,HIGH);digitalWrite(ID8,LOW);break; // DEC 2 = 0b0010
case 3: digitalWrite(ID1,LOW);digitalWrite(ID2,LOW);digitalWrite(ID4,HIGH);digitalWrite(ID8,HIGH);break; // DEC 3 = 0b0011
case 4: digitalWrite(ID1,LOW);digitalWrite(ID2,HIGH);digitalWrite(ID4,LOW);digitalWrite(ID8,LOW);break; // DEC 4 = 0b0100
case 5: digitalWrite(ID1,LOW);digitalWrite(ID2,HIGH);digitalWrite(ID4,LOW);digitalWrite(ID8,HIGH);break; // DEC 5 = 0b0101
case 6: digitalWrite(ID1,LOW);digitalWrite(ID2,HIGH);digitalWrite(ID4,HIGH);digitalWrite(ID8,LOW);break; // DEC 6 = 0b0110
case 7: digitalWrite(ID1,LOW);digitalWrite(ID2,HIGH);digitalWrite(ID4,HIGH);digitalWrite(ID8,HIGH);break; // DEC 7 = 0b0111
case 8: digitalWrite(ID1,HIGH);digitalWrite(ID2,LOW);digitalWrite(ID4,LOW);digitalWrite(ID8,LOW);break; // DEC 8 = 0b1000
case 9: digitalWrite(ID1,HIGH);digitalWrite(ID2,LOW);digitalWrite(ID4,LOW);digitalWrite(ID8,HIGH);break; // DEC 9 = 0b0110
case 10: digitalWrite(ID1,HIGH);digitalWrite(ID2,HIGH);digitalWrite(ID4,HIGH);digitalWrite(ID8,HIGH);break;
}}
void anod(){
switch(an){
case 0: digitalWrite(AN1,HIGH);digitalWrite(AN2,LOW);digitalWrite(AN3,LOW);digitalWrite(AN4,LOW);digitalWrite(AN5,LOW);break;
case 1: digitalWrite(AN1,LOW);digitalWrite(AN2,HIGH);digitalWrite(AN3,LOW);digitalWrite(AN4,LOW);digitalWrite(AN5,LOW);break;
case 2: digitalWrite(AN1,LOW);digitalWrite(AN2,LOW);digitalWrite(AN3,HIGH);digitalWrite(AN4,LOW);digitalWrite(AN5,LOW);break;
case 3: digitalWrite(AN1,LOW);digitalWrite(AN2,LOW);digitalWrite(AN3,LOW);digitalWrite(AN4,HIGH);digitalWrite(AN5,LOW);break;
case 4: digitalWrite(AN1,LOW);digitalWrite(AN2,LOW);digitalWrite(AN3,LOW);digitalWrite(AN4,LOW);digitalWrite(AN5,HIGH);break;
}}
ISR(TIMER2_COMPA_vect) {
switch(i){
case 0: segm=a[0]; an=0;cl();delayMicroseconds(BR); anod(); segment();break;
case 1: segm=a[1]; an=1;cl();delayMicroseconds(BR); anod(); segment();break;
case 2: segm=a[2]; an=2;cl();delayMicroseconds(BR); anod(); segment();break;
case 3: segm=a[3]; an=3;cl();delayMicroseconds(BR); anod(); segment();break;
case 4: segm=a[4]; an=4;cl();delayMicroseconds(BR); anod(); segment();break;
}
i++;if(i>4){i=0;}}
void cl(){digitalWrite(AN1,LOW);digitalWrite(AN2,LOW);digitalWrite(AN3,LOW);digitalWrite(AN4,LOW);digitalWrite(AN5,LOW);
digitalWrite(ID1,HIGH);digitalWrite(ID2,HIGH);digitalWrite(ID4,HIGH);digitalWrite(ID8,HIGH);
}