//Defines all pins on the Arduino Uno board in order of connection.
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
static unsigned char counter10msec=0;
static unsigned char counter40msec=0;
static unsigned char counter100msec=0;
unsigned char counter=0;
static unsigned char num=0;
unsigned char display_array[]={~0x3F,~0x6,~0x5B,~0x4F ,~0x66, ~0x6D,~0x5F,~0x7,~0x7F,~0x6F,0x8,0x00,0x46,~0x3F,0x06,0x0E};
void display_on(byte num) // This function turns on the correct pins to display numbers passed to it // through the variable “num”.
{
int result = bitRead(num, 0); // Read the first binary entry in num and stores it in result.
if (result == 1) // Check to see if this segment should be on.
{
digitalWrite(A, HIGH);
} // Turns on the segment.
else // Otherwise, it turns it off.
{
digitalWrite(A, LOW);
} // Turns segment off.
result = bitRead( num, 1); // Same thing for the 6 remaining segments.
if (result == 1)
{
digitalWrite(B, HIGH);
}
else
{
digitalWrite(B, LOW);
}
result = bitRead( num, 2);
if (result == 1)
{digitalWrite(C, HIGH);}
else
{digitalWrite(C, LOW);}
result = bitRead( num, 3);
if (result == 1)
{digitalWrite(D, HIGH);}
else
{digitalWrite(D, LOW);}
result = bitRead( num, 4);
if (result == 1)
{digitalWrite(E, HIGH);}
else
{digitalWrite(E, LOW);}
result = bitRead( num, 5);
if (result == 1)
{digitalWrite(F, HIGH);}
else
{digitalWrite(F, LOW);}
result = bitRead( num, 6);
if (result == 1)
{digitalWrite(G, HIGH);}
else
{digitalWrite(G, LOW);}
}
void Digitalpin_init(void)
{
pinMode(A, OUTPUT); // Making all pins outputs
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(10,OUTPUT);
}
void Timer_Init(void)
{
cli(); /*stop interrupts for till we make the settings*/
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR0A = 0; /* Reset entire TCCR1A to 0 */
TCCR0B = 0; /* Reset entire TCCR1B to 0 */
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR0B |= B00000011; /* Set CS10 and CS11 to 1 so we get prescalar 64 */
/*3. We enable compare match mode on register A*/
TIMSK0 |= B00000010; /*Set OCIE1A to 1 so we enable compare match A */
/*4. Set the value of register A to 31250*/
OCR0A = 250; /*Finally we set compare register A to this value*/
sei(); /*Enable back the interrupts*/
}
void Task_100ms()
{
display(num);
count();
}
void count(void)
{
if(counter>=10 )
{
counter=0;
num=num+1;
if(num>15)
{
num=0;
}
}
else{
counter++;
}
}
void display(int a)
{
display_on(display_array[a]);
}
/*****************************************************************/
void setup() {
Serial.begin(115200);
Digitalpin_init();
Timer_Init();
}
void loop()
{
if(counter10msec>=(10))
{
counter10msec=0;
//Task_10ms();
}
else{
/*Do Nothing*/
}
if(counter40msec>=(40))
{
counter40msec=0;
//Task_40ms();
}
else{
/*Do Nothing*/
}
if(counter100msec>=(100))
{
counter100msec=0;
Task_100ms();
}
else{
/*Do Nothing*/
}
}
/********************do not change******/
/*With the settings above, this IRS will trigger each count */
ISR(TIMER0_COMPA_vect){
TCNT0 = 0; /*First, set the timer back to 0 so it resets for next interrupt*/
counter10msec++;
counter40msec++;
counter100msec++;
}