#include <tiny2313.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <delay.h>
#include <stdio.h>

int freg,fr;

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
freg++;
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
fr=freg;
freg=0;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port A initialization
// Func2=In Func1=In Func0=In 
// State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// 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 D initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x04;
DDRD=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 43,200 kHz
// Mode: CTC top=OCR1A
// 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=0x0C;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0xA8;
OCR1AL=0xC0;
OCR1BH=0x00;
OCR1BL=0x00;


// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
GIMSK=0x40;
MCUCR=0x00;
EIFR=0x40;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;

#asm ("wdr")
/* Write logical one to WDTOE and WDE */
WDTCR |= 0x18;     
/* Turn off WDT */
WDTCR = 0x00;

// LCD module initialization
lcd_init(16);
lcd_gotoxy(4,0);
lcd_putsf("Hello:)");
delay_ms(1000);
lcd_clear();

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      lcd_gotoxy(0,0); 
      lcd_putsf("F=");
        if (fr>0 && fr<999)
        {                      
        lcd_putchar (fr/100+0x30);         //показати значення одиниць
        lcd_putchar ((fr/10)%10+0x30);     //показати значення десятих
        lcd_putchar ((fr/1)%10+0x30);     //показати значення десятих
        lcd_putsf ("Hz       ");
        }
        
        if (fr>999 && fr<9999)
        {
        lcd_putchar (fr/1000+0x30);         //показати значення одиниць
        lcd_putsf (".");
        lcd_putchar ((fr/100)%10+0x30);     //показати значення десятих
        lcd_putchar ((fr/10)%10+0x30);     //показати значення десятих
        lcd_putchar ((fr/1)%10+0x30);     //показати значення десятих
        lcd_putsf ("kHz     ");
        }
        
        if (fr>9999 && fr<99999)
        {
        lcd_putchar (fr/10000+0x30);         //показати значення одиниць
        lcd_putchar ((fr/1000)%10+0x30);         //показати значення одиниць
        lcd_putsf (".");
        lcd_putchar ((fr/100)%10+0x30);     //показати значення десятих
        lcd_putchar ((fr/10)%10+0x30);     //показати значення десятих
        lcd_putchar ((fr/1)%10+0x30);     //показати значення десятих
        lcd_putsf ("kHz     ");
        }
      };
}