/*****************************************************
Chip type : ATmega8
Program type : Application
Clock frequency : 1,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256

*****************************************************/
#include <mega8.h>

unsigned int imp,kmh;
unsigned char current_dig,ind_speed[3];

void init(void);
void display(unsigned char number);


interrupt [EXT_INT1] void ext_int1_isr(void)
{
imp++;
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
kmh=imp*6/5;       //         
imp=0;
TCNT1H=0x00;
TCNT1L=0x00;
}

interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{    PORTD.0=0;PORTD.1=0;PORTD.2=0;                                          // 
     PORTB.0=1;PORTB.1=1;PORTB.2=1;PORTB.3=1;PORTB.4=1;PORTB.5=1;PORTB.6=1;  //
     
if (current_dig <3)current_dig++;else current_dig=0;      // 
display(ind_speed[current_dig]);                        //  
    switch (current_dig)                                //   
    {
        case 0: {PORTD.0=1;PORTD.1=0;PORTD.2=0;break;}
        case 1: {PORTD.0=0;PORTD.1=1;PORTD.2=0;break;}
        case 2: {PORTD.0=0;PORTD.1=0;PORTD.2=1;break;}
    }
}

void init(void)
{
PORTB=0x00;
DDRB=0xff;

PORTC=0x00;
DDRC=0xff;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=P State2=0 State1=0 State0=0 
PORTD=0x08;
DDRD=0xF7;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 15,625 kHz
//TCCR0=0x03;

// Clock value: 125,000 kHz
TCCR0=0x02;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 3,906 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x04;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x07;
OCR1AL=0xA1;
OCR1BH=0x00;
OCR1BL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: On
// INT1 Mode: Falling Edge
GICR|=0x80;
MCUCR=0x08;
GIFR=0x80;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x51;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

}

void display(unsigned char number)
{
switch (number)
{case 0: {PORTB.1=0;PORTB.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTD.4=1;break;}
case 1: {PORTB.1=1;PORTB.2=0;PORTB.0=1;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=1;break;}
case 2: {PORTB.1=0;PORTB.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=0;PORTD.4=0;break;}
case 3: {PORTB.1=0;PORTB.2=0;PORTD.7=0;PORTD.6=0;PORTD.5=1;PORTB.0=1;PORTD.4=0;break;}
case 4: {PORTB.1=1;PORTB.2=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTB.0=0;PORTD.4=0;break;}
case 5: {PORTB.1=0;PORTB.2=1;PORTD.7=0;PORTD.6=0;PORTD.5=1;PORTB.0=0;PORTD.4=0;break;}
case 6: {PORTB.1=0;PORTB.2=1;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTB.0=0;PORTD.4=0;break;}
case 7: {PORTB.1=0;PORTB.2=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTB.0=1;PORTD.4=1;break;}
case 8: {PORTB.1=0;PORTB.2=0;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTB.0=0;PORTD.4=0;break;}
case 9: {PORTB.1=0;PORTB.2=0;PORTD.7=0;PORTD.6=0;PORTD.5=1;PORTB.0=0;PORTD.4=0;break;}
case 10: {PORTB.1=0;PORTB.2=1;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTB.0=1;PORTD.4=1;break;}
case 11: {PORTB.0=1;PORTB.1=1;PORTB.2=1;PORTB.3=1;PORTB.4=1;PORTB.5=1;PORTB.6=1;break;}
default:break;
}


}
void main(void)
{init();


while (1)
{ 
   if (kmh>99){ind_speed[2]=((kmh/100)%10);}//else ind_speed[2]=11;    // : 3
   if (kmh>9){ind_speed[1]=((kmh/10)%10);}//else ind_speed[1]=11;      // 2
              ind_speed[0]=(kmh%10);                                 // 1
} //End of while(1)
}//End of main