TCCR0=0;   
TCNT0=9;    0,   , 
;      INT0   0
TIFR=0 ; 
GIFR=0 ; 
-    
-      
if(TCCR0==0x00)
{ // 
TCCR0=0x01;
}
else
{
//     i -    
TCCR0=0x00;
Count = TCNT0;
frequency = 8000000UL/(i*255+Count);   -  i*256
i=0;
}






/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
 Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 17.06.2013
Author  : 
Company : 
Comments: 


Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
int start = 0;
unsigned int i = 0;

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
   start = 1;
   PORTC = 0b11111111;
}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0xF8;
    //Place your code here
    if(start == 1)
    {   
//        PORTC = 0b11111111;
        start = 0;
    }  
    //if(PIND.2 == 1)
    {
        i += 1;
        if(i == 1000)
        {
            PORTC = 0b00000000;
            i = 0;
        }
    }

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTC=0x00;
DDRC=0x7F;

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

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
TCCR0=0x01;
TCNT0=0xF8;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

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

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

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

while (1)
    {

    }
}



/////////////////////////////////////////////////////////


/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
 Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 17.06.2013
Author  : 
Company : 
Comments: 


Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#include <lcd.h>
#include <stdio.h>
#include <delay.h>

#asm //   
    .equ __lcd_port=0x18 // LCD  
#endasm

unsigned long i = 0, Count = 0, imp = 0, imp2 = 0, Count2 = 0;
unsigned long int freq = 0;
int zamer = 0;
unsigned long j = 0, q = 0, opimp = 0;
char lcd_buffer[33];
eeprom unsigned int tab_itog[157];
unsigned int tab_sred[10];
unsigned int need_freq[157] = {600};
// Timer1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
//   if (zamer==0) 
//   {
//       ms++;
//       if(ms == 1000)
//       {
//            sec++;
//            ms=0;
//       } 
//   }
}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
    //    ( )
//    TCCR0=0; //  
//    TCNT0=9; //   0,   , 
    //;      INT0   0
    //TIFR=0; 
    //GIFR=0; 
    //-    
    //-      
    if(TCCR0==0x00) 
    
    { //  
        //TCCR0=0; //  
        TCNT0=9; //   0,   , 
    
        TCCR0=0x01;
    }
    else  
    //if(TCCR0==0x01)
    { //     
        //TCCR0=0x00;
        Count = TCNT0;
        //if (Count2 == 0)
        //{
        //    Count2 = 255-Count;
            freq = 8000000UL/(i*255+TCNT0); //
        //}
        //else
        //{
        //    freq = 8000000UL/(i*255+Count2);
        //}
        i = 0;
        //Count = 0; 
        zamer = 1;
    }
    //zamer = 0;
//    ms=0;
//    sec=0;
}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
    if(TCCR0==0x01)
    //{ //     
    //if ((i+TCNT0)>0)
    {
        imp2 = i;
        imp = TCNT0;
    }
//    //     
////        TCCR0=0x00;
////        Count = TCNT0;
////        freq = 8000000UL/(i*255+Count); 
////        i=0; 
//////        zamer = 1;
    //}
}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
   i++;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
//unsigned int j = 0, q = 0, opimp = 0;
unsigned long temp = 0;
// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0xFF;

//// Port C initialization
//// Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
//// State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
//PORTC=0x00;
//DDRC=0x7F;

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

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: On
// INT1 Mode: Rising Edge
GICR|=0xC0;
MCUCR=0x0F;
GIFR=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

//   16x2
lcd_init(16);

//     
sprintf(lcd_buffer,"      Hello,       ini system  ");
lcd_clear();
lcd_puts(lcd_buffer);
for (j=1; j<157; j++)
{
   need_freq[j]=need_freq[j-1]+60;
}
for (j=0; j<157; j++)
{
    sprintf(lcd_buffer,"%u", need_freq[j]);
    lcd_gotoxy(0,0);
    lcd_puts(lcd_buffer);
    //delay_ms(100);
}
j=0;
delay_ms(500);
lcd_clear();

//lcd_gotoxy(0,0);
//sprintf(lcd_buffer,"test");

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

while (1)
      {
      // Place your code here 
          if(zamer == 1)
          {
              lcd_gotoxy(0,0);
              sprintf(lcd_buffer,"F:%u ", freq * 60);
              lcd_puts(lcd_buffer);
              lcd_gotoxy(8,0);     
              opimp = imp+255*imp2; 
              sprintf(lcd_buffer,"N:%u ",i );//need_freq[q]
              //sprintf(lcd_buffer,"op.=%u  ", opimp);
              lcd_puts(lcd_buffer);
              lcd_gotoxy(0,1);
              sprintf(lcd_buffer,"op.=%u  ", Count); //opimp
              lcd_puts(lcd_buffer);
              zamer = 0; 
              //lcd_clear();
              //delay_ms(500);  
              //j++; 
              if(j == 10)
              { 
//                for(j=0; j<10; j++)
//                //  
//                { 
//                    lcd_clear();
//                    delay_ms(300);
//                  lcd_gotoxy(0,0);
//                  sprintf(lcd_buffer,"zamer:%u ", j+1);
//                  lcd_puts(lcd_buffer);
//                  lcd_gotoxy(0,1);
//                  sprintf(lcd_buffer,"znach:%u ", tab_sred[j]);
//                  lcd_puts(lcd_buffer);
//                  delay_ms(400);
//                  lcd_clear();
//                  temp += tab_sred[j]/5;
//                 }
//                lcd_clear();
//                sprintf(lcd_buffer, "srednee:%u", temp);
//                lcd_puts(lcd_buffer);
//                delay_ms(600);
                j=0; 
                //tab_itog[q] = opimp;
                q++;
                if(q==156) q=0;
              }
              else 
              {
                //tab_sred[j] = opimp;
                j++; 
              }
              
              
          }
      }
}
