Тема: Часы Attiny2313
Основная статья - http://rcl-radio.ru/?p=94090
Затемнение индикатора с 23:00 до 6:00
Не тестировалось !!!
// tm1637 PB0 === CLK
// tm1637 PB1 === DIO
int i,i1,hh,mm,bb,brig;
void setup() {
DDRB = 0b00000000;
PORTB |= (1 << 2) | (1 << 3);
cli();
TCCR1A = 0;
TCCR1B = 0;
OCR1A = 18750; // 0.1 s
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS11) | (1 << CS10); // 64
TIMSK |= (1 << OCIE1A);
sei();
}
void loop() {
if(hh * 100 + mm > 2300 || hh * 100 + mm < 600){brig = 1;}else{brig = 7;}
print_time(hh * 100 + mm, bb, brig);// int число выводимое на индикатор, двоеточие, яркость от 0 до 7
if(i1 <= 5){bb = 1;}else{bb = 0;}
if(((PINB >> 2) & 1) == 0){mm++; if(mm>59){mm = 0;} delay(200);}
if(((PINB >> 3) & 1) == 0){hh++; if(hh>23){hh = 0;} delay(200);}
}
void tm_dec(byte dig){
for(int i = 0; i < 8; i++) {
DDRB |= (1 << 0);del();
if (dig & 0x01)
DDRB &= ~(1 << 1);
else
DDRB |= (1 << 1);del();
DDRB &= ~(1 << 0);del();
dig = dig >> 1;
}
DDRB |= (1 << 0);
DDRB &= ~(1 << 1);del();
DDRB &= ~(1 << 0);del();
if (((PINB >> 1) & 1) == 0)
DDRB |= (1 << 1);del();
DDRB |= (1 << 0);del();
}
void tm_stop(){
DDRB |= (1 << 1);del();
DDRB &= ~(1 << 0);del();
DDRB &= ~(1 << 1);del();
}
void tm_start(){
DDRB |= (1 << 1);del();
}
void print_time(int t, bool pd_t, int br){
tm_start();tm_dec(0b10001000 + br);//tm_stop();tm_start();
tm_dec(0x40);tm_stop();tm_start();
int data0 = t / 1000;
int data1 = t / 100 % 10;
int data2 = t / 10 % 10;
int data3 = t % 10;
for(byte n = 0; n < 4; n++){
int data;
switch(n){
case 0: data = data0;break;
case 1: data = data1;break;
case 2: data = data2;break;
case 3: data = data3;break;
}
switch(data){ // XGFEDCBA
case 0: data = 0b00111111;break; // 0
case 1: data = 0b00000110;break; // 1
case 2: data = 0b01011011;break; // 2
case 3: data = 0b01001111;break; // 3
case 4: data = 0b01100110;break; // 4
case 5: data = 0b01101101;break; // 5
case 6: data = 0b01111101;break; // 6
case 7: data = 0b00000111;break; // 7
case 8: data = 0b01111111;break; // 8
case 9: data = 0b01101111;break; // 9
}
if(n == 0){data0 = data;}
if(n == 1){data1 = data;}
if(n == 2){data2 = data;}
if(n == 3){data3 = data;}
}
if(pd_t == 1){data1+= 0b10000000;}
tm_dec(0xC0);tm_dec(data0);tm_dec(data1);tm_dec(data2);tm_dec(data3);tm_stop();
}
void del(){delayMicroseconds(100);}
ISR(TIMER1_COMPA_vect){
i++;i1++;
if(i1 > 9){i1 = 0;}
if(i > 599){mm++;i = 0;}
if(mm > 59){hh++;mm = 0;}
if(hh > 23){hh = 0;}
}