bool LED_STATE = true;
volatile int timer = 0;
void setup() {
pinMode(13, OUTPUT); //Set the pin to be OUTPUT
DDRD = B11111111;
DDRB = B00000011;
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000100; //Set CS12 to 1 so we get prescalar 256
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 31250*/
OCR1A = 62500; //Finally we set compare register A to this value
sei(); //Enable back the interrupts
}
//With the settings above, this IRS will trigger each 500ms.
ISR(TIMER1_COMPA_vect){
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
//LED_STATE = !LED_STATE; //Invert LED state
//digitalWrite(13,LED_STATE); //Write new state to the LED on pin D5
timer += 1;
}
void loop() {
// put your main code here, to run repeatedly:
display(timer, 0, 0);
if(PINB & (1 << 2)){
digitalWrite(13,LED_STATE);
timer = 0;
}
}
void display(int value, char base, char blanking){
//blank();
int dig10 = 0;
int dig1 = 0;
if((base==0)&&(blanking==0)){
dig10 = value/10;
dig1 = value%10;
PORTB = B00000010;
//delay(100);
segment(dig1);
//delay(100);
//blank();
//PORTB = B00000001;
//delay(100);
//segment(dig10);
//delay(100);
}
else if((base==0)&&(blanking==1)){
dig10 = value/10;
dig1 = value%10;
if(dig10<1){
PORTB = B00000010;
segment(dig1);
delay(1);
}
else{
PORTB = B00000010;
segment(dig1);
delay(1);
blank();
PORTB = B00000001;
segment(dig10);
delay(1);
}
}
if((base==1)&&(blanking==0)){
dig10 = 0;
dig1 = value;
PORTB = B00000010;
segment(dig1);
delay(1);
blank();
PORTB = B00000001;
segment(dig10);
delay(1);
}
else if((base==1)&&(blanking==1)){
dig1 = value;
PORTB = B00000010;
segment(dig1);
delay(1);
//blank();
//PORTB = B00000001;
//segment(dig10);
//delay(1);
}
}
void segment(int digit){
switch(digit){
case 0:
PORTD = B00111111;
break;
case 1:
PORTD = B00000110;
break;
case 2:
PORTD = B01011011;
break;
case 3:
PORTD = B01001111;
break;
case 4:
PORTD = B01100110;
break;
case 5:
PORTD = B01101101;
break;
case 6:
PORTD = B01111101;
break;
case 7:
PORTD = B00000111;
break;
case 8:
PORTD = B01111111;
break;
case 9:
PORTD = B01101111;
break;
case 10:
PORTD = B01110111;
break;
case 11:
PORTD = B01111100;
break;
case 12:
PORTD = B00111001;
break;
case 13:
PORTD = B01011110;
break;
case 14:
PORTD = B01111001;
break;
case 15:
PORTD = B01110001;
}
}
void blank(){
PORTB = B00000000;
PORTD = B00000000;
}