#include <avr/io.h>
#include <avr/interrupt.h>

#define NUMARRAY 20
unsigned char digit[] = {0x18, 0xBB, 0x2C, 0x29, 0x8B, 0x49, 0x48, 0x3B, 0x08, 0x09};
volatile long timer0=0, adc_count=0;
volatile unsigned char LED_val = 254;
volatile int adc_val[NUMARRAY];

void on_7LED(int i)
{
 if(i==0){
  PORTC = (PORTC | 0x30) & ~(1<<PC5);
  PORTA = digit[ (LED_val % 100)/10 ];
 }else{
  PORTC = (PORTC | 0x30) & ~(1<<PC4);
  PORTA = digit[ LED_val % 10 ];
 }
}

SIGNAL(SIG_OVERFLOW0)
{
 timer0++;
 
 on_7LED(timer0 % 2);
}

SIGNAL(SIG_ADC)
{ long imsi, sum;
 int  i;
 
 adc_count++;
 adc_val[adc_count%NUMARRAY] = ADC;
 
 for(i=0, sum=0; i<NUMARRAY; i++)
  sum += adc_val[i];
 
 imsi = sum / NUMARRAY;
 
 LED_val = imsi * 100 / 1024;
}

int main(void)
{
 DDRA = 0xFF;
 DDRC = 1<<PC4 | 1<<PC5;
 
 TIMSK |= 1<<TOIE0;
 TCCR0 = (TCCR0 & 0xF8 ) | 0x5;
 
 ADCSRA = 1<<ADEN | 1<<ADFR | 1<<ADIE | 0x7;
 ADMUX = 0x0;
 sei();
 
 ADCSRA |= 1<<ADSC;
 while(1){
 
 }
}

