Тема: Ретро часы на ИН-16 (Arduino)
Основная статья - http://rcl-radio.ru/?p=67087
#include <MsTimer2.h>
#include <Wire.h>
#include <DS3231.h>
#include <EEPROM.h>
DS3231 clock;RTCDateTime DateTime;
int an, segm,times,i;
byte a[4];
void setup(){ Wire.begin();clock.begin();MsTimer2::set(4, to_Timer);MsTimer2::start();
// clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
pinMode(2,OUTPUT); // D2 === H1
pinMode(3,OUTPUT); // D3 === H2
pinMode(4,OUTPUT); // D4 === H3
pinMode(5,OUTPUT); // D5 === H4
pinMode(6,OUTPUT); // D6 === A0 3 ид1
pinMode(7,OUTPUT); // D7 === A1 6 ид1
pinMode(8,OUTPUT); // D8 === A2 7 ид1
pinMode(10,OUTPUT); // D10 === A3 4 ид1
pinMode(9,OUTPUT); // ВЫХОД СИГНАЛА ГЕНЕРАТОРА 16-30 кГц для высоковольтного источника питания
TCCR1A = 0x40;TCCR1B = 0x09;OCR1A = 400;//270=30кГц, 300=26кГц, 400=20кГц, 500=16кГц - изминение яркости индикаторов
}
void loop(){
DateTime=clock.getDateTime();// опрос времени
times = DateTime.hour*100+DateTime.minute;
a[0]=times/1000;
a[1]=times/100%10;
a[2]=times/10%10;
a[3]=times%10%10;
}
void to_Timer(){
switch(i){
case 0: segm=a[0]; an=0; anod(); segment();delay(1);break;
case 1: segm=a[1]; an=1; anod(); segment();delay(1);break;
case 2: segm=a[2]; an=2; anod(); segment();delay(1);break;
case 3: segm=a[3]; an=3; anod(); segment();delay(1);break;}i++;if(i>3){i=0;}}
void segment(){
switch(segm){
case 0: digitalWrite(10,LOW);digitalWrite(8,LOW);digitalWrite(7,LOW);digitalWrite(6,LOW);break; // DEC 0 = 0b0000
case 1: digitalWrite(10,LOW);digitalWrite(8,LOW);digitalWrite(7,LOW);digitalWrite(6,HIGH);break; // DEC 1 = 0b0001
case 2: digitalWrite(10,LOW);digitalWrite(8,LOW);digitalWrite(7,HIGH);digitalWrite(6,LOW);break; // DEC 2 = 0b0010
case 3: digitalWrite(10,LOW);digitalWrite(8,LOW);digitalWrite(7,HIGH);digitalWrite(6,HIGH);break; // DEC 3 = 0b0011
case 4: digitalWrite(10,LOW);digitalWrite(8,HIGH);digitalWrite(7,LOW);digitalWrite(6,LOW);break; // DEC 4 = 0b0100
case 5: digitalWrite(10,LOW);digitalWrite(8,HIGH);digitalWrite(7,LOW);digitalWrite(6,HIGH);break; // DEC 5 = 0b0101
case 6: digitalWrite(10,LOW);digitalWrite(8,HIGH);digitalWrite(7,HIGH);digitalWrite(6,LOW);break; // DEC 6 = 0b0110
case 7: digitalWrite(10,LOW);digitalWrite(8,HIGH);digitalWrite(7,HIGH);digitalWrite(6,HIGH);break; // DEC 7 = 0b0111
case 8: digitalWrite(10,HIGH);digitalWrite(8,LOW);digitalWrite(7,LOW);digitalWrite(6,LOW);break; // DEC 8 = 0b1000
case 9: digitalWrite(10,HIGH);digitalWrite(8,LOW);digitalWrite(7,LOW);digitalWrite(6,HIGH);break; // DEC 9 = 0b0110
}}
void anod(){
switch(an){
case 0: digitalWrite(2,HIGH);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,LOW);break;
case 1: digitalWrite(2,LOW);digitalWrite(3,HIGH);digitalWrite(4,LOW);digitalWrite(5,LOW);break;
case 2: digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,HIGH);digitalWrite(5,LOW);break;
case 3: digitalWrite(2,LOW);digitalWrite(3,LOW);digitalWrite(4,LOW);digitalWrite(5,HIGH);break;
}}