Тема: ИВЛ1–7/5 + PT6312
Основная статья - http://rcl-radio.ru/?p=131652
#define STB 4
#define DIN 2
#define CLK 3
#define SET 5
#define UP 6
#define DW 7
#include <Wire.h>
#include <DS3231.h> // http://rcl-radio.ru/wp-content/uploads/2022/10/DS3231.zip
DS3231 clock;RTCDateTime DateTime;
int a[5];
int hour,minut,set;
long times,times0;
bool d,mig;
void setup(){
Serial.begin(9600);clock.begin();
///clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
pinMode(STB,OUTPUT);
pinMode(DIN,OUTPUT);
pinMode(CLK,OUTPUT);
pinMode(SET,INPUT_PULLUP);
pinMode(UP,INPUT_PULLUP);
pinMode(DW,INPUT_PULLUP);
delay(100);
digitalWrite(STB,HIGH);delay(200);
// PWM OUTPUT D9 TIMER 1
TCCR1A = 0;TCCR1B = 0;
TCCR1A = (1 << COM1A1)|(1 << WGM11);
TCCR1B = (1 << WGM13)|(1 << WGM12)|(1 << CS10);
DDRB = 1 << DDB1; // OUTPUT PWM (OCR1A)
OCR1A = 15;
ICR1 = 40;
write_byte1_PT6312(0b01000100);// command 2
write_byte1_PT6312(0b00000100);// command 1 8dig/14seg
write_byte1_PT6312(0b10001111);// command 4 display on
}
void loop(){
DateTime = clock.getDateTime();
hour = DateTime.hour;
minut = DateTime.minute;
if(digitalRead(SET)==LOW){set++;if(set>2){set=0;} delay(300);}
if(digitalRead(UP)==LOW&&set==1){hour++;if(hour>23){hour=23;} clock.setDateTime(2024, 2, 22, hour, minut, 0);delay(100);}
if(digitalRead(DW)==LOW&&set==1){hour--;if(hour<0){hour=0;} clock.setDateTime(2024, 2, 22, hour, minut, 0);delay(100);}
if(digitalRead(UP)==LOW&&set==2){minut++;if(minut>59){minut=59;} clock.setDateTime(2024, 2, 22, hour, minut, 0);delay(100);}
if(digitalRead(DW)==LOW&&set==2){minut--;if(minut<0){minut=0;} clock.setDateTime(2024, 2, 22, hour, minut, 0);delay(100);}
if(millis()-times0<200){mig=1;}
if(millis()-times0>200){mig=0;}
if(millis()-times0>400){times0=millis();}
if(millis()-times<500){d=1;}
if(millis()-times>500){d=0;}
if(millis()-times>1000){times=millis();}
if(set==0){
a[0]=minut%10;
a[1]=minut/10%10;
a[2]=hour%10;
a[3]=hour/10%10;}
if(set!=2){a[0]=minut%10;}
if(set!=2){a[1]=minut/10%10;}
if(set==2&&mig==0){a[0]=minut%10;}
if(set==2&&mig==0){a[1]=minut/10%10;}
if(set==2&&mig==1){a[0]=10;}
if(set==2&&mig==1){a[1]=10;}
if(set!=1){a[2]=hour%10;}
if(set!=1){a[3]=hour/10%10;}
if(set==1&&mig==0){a[2]=hour%10;}
if(set==1&&mig==0){a[3]=hour/10%10;}
if(set==1&&mig==1){a[2]=10;}
if(set==1&&mig==1){a[3]=10;}
//dp g f e d c b a
write_byte2_PT6312(0b11000000,seg(a[0],0));//GR1
write_byte2_PT6312(0b11000010,seg(a[1],0));//GR2
write_byte2_PT6312(0b11000100,seg(a[2],0));//GR3
write_byte2_PT6312(0b11000110,seg(a[3],0));//GR4
write_byte2_PT6312(0b11001000,seg(0,d));//GR5
delay(100);
}
byte seg(int a, bool dp){
byte segm;
switch(a){
case 0: segm=0b00111111+(dp<<7);break;
case 1: segm=0b00000110+(dp<<7);break;
case 2: segm=0b01011011+(dp<<7);break;
case 3: segm=0b01001111+(dp<<7);break;
case 4: segm=0b01100110+(dp<<7);break;
case 5: segm=0b01101101+(dp<<7);break;
case 6: segm=0b01111101+(dp<<7);break;
case 7: segm=0b00000111+(dp<<7);break;
case 8: segm=0b01111111+(dp<<7);break;
case 9: segm=0b01101111+(dp<<7);break;
case 10:segm=0b00000000;break;// пусто
case 11:segm=0b01000000;break;// -
case 12:segm=0b01100011;break;// градус
case 13:segm=0b00111001;break;// С
}
return segm;
}
void write_byte1_PT6312(byte data){
digitalWrite(STB,LOW);
for(int i = 0; i <= 7; i++){
digitalWrite(CLK,LOW);
digitalWrite(DIN, (data >> i) & 1);
digitalWrite(CLK,HIGH);
}
digitalWrite(STB,HIGH);delay(1);
}
void write_byte2_PT6312(byte reg, byte data){
digitalWrite(STB,LOW);
for(int i = 0; i <= 7; i++){
digitalWrite(CLK,LOW);
digitalWrite(DIN, (reg >> i) & 1);
digitalWrite(CLK,HIGH);
}
delay(1);
for(int i = 0; i <= 7; i++){
digitalWrite(CLK,LOW);
digitalWrite(DIN, (data >> i) & 1);
digitalWrite(CLK,HIGH);
}
digitalWrite(STB,HIGH);delay(1);
}