#include "C:\RS\Meric_casu\main.h"
#define mic input(PIN_A4)
#define POCET 10
#define E1 3034

void main()
{
   int16 perioda;
   int8 i;
   int32 cas;
   signed int16 vysl;
  
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   output_B(0xFF);

   while(TRUE)
   {
      cas=0;
      for(i=0;i<POCET;i++)
      {
         while(mic);
         while(!mic);
         while(mic)
         {
            set_timer1(0);
            //LED_ON;
            //delay_us(1400);
            while(mic);
         }
         while(!mic)
         {
            //LED_OFF;
            //delay_us(1400);
            while(!mic);
         }
         perioda=get_timer1();
         //printf("Perioda %5Lu us.\n\r",perioda);
         cas+=perioda;
      }
      cas/=i;
      printf("Perioda %5Lu us.\n\r",cas);
      vysl=cas-E1;
      if(vysl<-53)output_B(0b10111111);
      if((vysl>-53)&&(vysl<-35))output_B(0b11011111);
      if((vysl>-35)&&(vysl<-17))output_B(0b11101111);
      if((vysl>-17)&&(vysl<17))output_B(0b11110111);
      if((vysl>17)&&(vysl<35))output_B(0b11111011);
      if((vysl>35)&&(vysl<52))output_B(0b111111101);
      if(vysl>52)output_B(0b111111110);
   }
}