/*****************************************************
Chip type               : ATmega644
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*****************************************************/

#include <mega644.h>
#include <stdlib.h>



// Alphanumeric LCD Module functions
#include <alcd.h>
#include <delay.h>

interrupt [TIM0_OVF] void timer0_ovf_isr(void);
interrupt [14] void   timer1_compa_isr(void);
interrupt [EXT_INT0] void ext_int0_isr(void);
interrupt [3] void ext_int1_isr(void);

// External Interrupt 0 service routine
volatile unsigned char tlacitka1;
volatile int i = 0;
volatile unsigned int j = 0;
volatile int k = 0;
volatile unsigned int n = 1;
volatile bit status1 = 0;
volatile bit stop1 = 0, stop2 = 0;
volatile int pom = 0;


volatile int xy_f = 0;
volatile unsigned char events = 0;
volatile int Sequence[5] = {0,2,0,2,0};


 volatile unsigned  char highbyte = 0;
 volatile unsigned char lowbyte = 0; 
         
volatile int CountType = 0;



volatile float TF = 0, TF1 = 0, TF2 = 0;
volatile unsigned int Freq = 0;




flash unsigned char sgn1[]="EKG 1";
flash unsigned char sgn2[]="OBDELNIK";
flash unsigned char sgn3[]="Fib.sini";
flash unsigned char sgn4[]="SINUS";
flash unsigned char sgn5[]="PILA";




const flash unsigned char ecgwave1[96]={
0x2B,0x2B,0x2B,0x2B,0x2B,0x2A,0x2B,0x29,0x2A,0x2A,0x29,0x2A,0x28,0x2B,0x2E,0x30,0x35,
0x37,0x36,0x32,0x2F,0x2E,0x2D,0x28,0x27,0x26,0x26,0x26,0x25,0x26,0x27,0x28,0x2B,0x2A,
0x2B,0x32,0x53,0x91,0xC5,0x88,0x2D,0x2C,0x2C,0x2D,0x2A,0x28,0x29,0x2A,0x2B,0x2A,0x2C,
0x2C,0x2D,0x2D,0x2F,0x32,0x33,0x35,0x37,0x3A,0x3C,0x3F,0x44,0x49,0x4D,0x51,0x54,0x55,
0x57,0x55,0x50,0x48,0x41,0x3A,0x33,0x2E,0x2B,0x28,0x28,0x27,0x28,0x28,0x26,0x28,0x28,
0x28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0,0xA2,0x9B,0x86,0x67,0x4E,0x4F,0x5A,
0x53,0x38,0x1B,0x0A,0x01,0x01,0x0C,0x16,0x1C,0x1D,0x1E,0x1E,0x1C,0x1D,0x1D,0x20,0x20,
0x1E,0x1D,0x1D,0x1F,0x1E,0x1C,0x1F,0x1F,0x1F,0x1F,0x1D,0x1E,0x1E,0x1F,0x1E,0x1E,0x1F,
0x1C,0x1E,0x1C,0x1D,0x1C,0x1C,0x1B,0x1A,0x1A,0x1A,0x19,0x18,0x18,0x1B,0x19,0x19,0x18,
0x17,0x18,0x18,0x19,0x19,0x1A,0x1E,0x1D,0x1F,0x22,0x24,0x25,0x28,0x27,0x28,0x2C,0x2D,
0x2E,0x2D,0x2E,0x2E,0x32,0x30,0x2F,0x2E,0x30,0x2E,0x2E,0x2E,0x2E,0x2C,0x2D,0x2D,0x2E,
0x2E,0x2D,0x2D,0x2C,0x2C,0x2D,0x2C,0x2C,0x2D,0x2D,0x2C,0x2C,0x2C,0x2B,0x2B,0x2B,0x2C,
0x2B,0x2D,0x2C,0x2D,0x2D,0x2B,0x2E,0x2C,0x2B,0x2C,0x2C
};


const flash unsigned char Pila[256]={
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10
,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21
,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32
,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43
,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54
,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0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x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
, 0x58, 0x59, 0x5A, 0x5B, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64
, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72
, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F
};

struct signal {
volatile int order;        //poradie vzorku
flash unsigned char *name; //nazov vzokru
const flash unsigned char *type; //typ vzorku
volatile  int number;      //pocet vzorkov
volatile unsigned int type_frequency;         //zakladna frekvencia vzorku bez deformacie{rozsirenia alebo zuzenia}
volatile unsigned  int wait_loop; // pocet cakania cyklov zavisiacichod frakvencie
};


struct signal Data[6];

interrupt [EXT_INT0] void ext_int0_isr(void)
{

    tlacitka1 = PIND;
    EIMSK = 0x02;
    EIFR=0x03;
    events = (~tlacitka1 )& 0x77; 
    
    if(CountType == 2)
    {
    TCNT0=0x00;
    TCCR0B=0x04;
    TIMSK0=0x01;
    }
    else
    {
    TCNT0=0x00;
    TCCR0B=0x05;
    TIMSK0=0x01;
    }


// Place your code here

}
interrupt [3] void ext_int1_isr(void)
{
 //inicializaci Timeru_1 


//highbyte = Freq>>8; 
//lowbyte = Freq; 

TIFR1 = 0x02;
TIMSK1 = 0x02; 

TCCR1A = 0; 
TCCR1B |= (1 << WGM12)  |(1 << CS12); //(1 << CS11)|(0 << CS10);  // (1 << CS12); 
OCR1AH =Data[Sequence[0]].type_frequency>>8;//highbyte;
OCR1AL =Data[Sequence[0]].type_frequency;//lowbyte;   
n = 0;
i=0;
j=0;
}



interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
    
    
    TCNT0=0x00;
    
   if (n>6)
   {
   TCCR0B=0x00; 
   EIMSK = 0x03; 
   n=0;
   }            
   else
   n++;
  

         
     
 } 
   
 interrupt [14] void   timer1_compa_isr(void)
 {  
   TCNT1H=0x00;
  TCNT1L=0x00; 


  if((Data[Sequence[i]].type[j]) && (stop1 == 0))  
  {
      PORTC = Data[Sequence[i]].type[j];//Data[Sequence[i]].type[j];
      j++; 
      
  }
  else
  {     
   if((stop1 == 0))
    { 
      j=0; 
      n++;  
      stop1 = 1; 
      } 
      
      }


   
  if(n > Data[i].number)
       { 
       i++;
       n = 0; 

       if(i ==5) i = 0;
       } 
       
    if(stop2 == 1)
  {
  stop1 = 0; 
  stop2 = 0;
  OCR1AH =Data[Sequence[i]].type_frequency>>8;//highbyte;
  OCR1AL =Data[Sequence[i]].type_frequency;//lowbyte; 
  TCNT1H=0x00;
  TCNT1L=0x00; 
 
  }

   
    if((stop1 == 1))
  {
   OCR1AH =Data[Sequence[i]].wait_loop>>8;  
   OCR1AL =Data[Sequence[i]].wait_loop; 
   TCNT1H=0x00;
   TCNT1L=0x00;
   
   stop2 = 1;

  } 
  
   
}

  

 
void main(void)
{
 int x = 0;
 int y = 0; 
   
 float jm = 0.000064;
 

 char *Cycle = (char*)malloc(sizeof( char)*5);   
char *Frequency = (char*)malloc(sizeof(char)*10);



 Data[0].order = 0;
 Data[0].name = sgn1;         //EKG 1
 Data[0].type = ecgwave1; 
 Data[0].number = 4; 
 Data[0].type_frequency = 122;  
 Data[0].wait_loop = 0 ;
 
 Data[1].order = 1;
 Data[1].name = sgn2; 
 Data[1].type = obdelnik;          //OBDELNIK
 Data[1].number = 4;
 Data[1].type_frequency = 16;
 Data[1].wait_loop = 0 ;
 
 Data[2].order = 2;
 Data[2].name = sgn3; 
 Data[2].type = ecgwave2;          //EKG 2
 Data[2].number =4; 
 Data[2].type_frequency = 63;    //nastavenie  OCR1AH,OCR1AL  
 Data[2].wait_loop = 0 ;
 
 Data[3].order = 3;
 Data[3].name = sgn4;
 Data[3].type = Sine;// Sine; 
 Data[3].number = 4;
 Data[3].type_frequency = 28;  
 Data[3].wait_loop = 0 ;
 
 Data[4].order = 4;
 Data[4].name = sgn5; 
 Data[4].type = Pila;          //pila
 Data[4].number = 4;
 Data[4].type_frequency = 30; 
 Data[4].wait_loop = 0 ;
 
 Data[5].number = 50;   //prednastavena tepova frekvencia

#pragma optsize-
CLKPR=0x80;
CLKPR=0x01;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Reset Source checking
if (MCUSR & 1)
   {
   // Power-on Reset
   MCUSR&=0xE0;
   // Place your code here

   }
else if (MCUSR & 2)
   {
   // External Reset
   MCUSR&=0xE0;
   // Place your code here

   }
else if (MCUSR & 4)
   {
   // Brown-Out Reset
   MCUSR&=0xE0;
   // Place your code here

   }
else if (MCUSR & 8)
   {
   // Watchdog Reset
   MCUSR&=0xE0;
   // Place your code here

   }
else if (MCUSR & 0x10)
   {
   // JTAG Reset
   MCUSR&=0xE0;
   // Place your code here

   }

// Input/Output Ports initialization
// Port A 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 
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 C 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 
PORTC=0x00;
DDRC=0xFF;

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

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=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


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
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-15: Off
// Interrupt on any change on pins PCINT16-23: Off
// Interrupt on any change on pins PCINT24-31: Off
EICRA=0x00;
EIMSK=0x03;
EIFR=0x03;
PCICR=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00;

// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;

// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;

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

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

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

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

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

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 3
// D5 - PORTB Bit 4
// D6 - PORTB Bit 5
// D7 - PORTB Bit 6
// Characters/line: 16
lcd_init(16);

// Global enable interrupts
#asm("sei");
 
while (1)
      { 
      

         
      //sipka hore PD0 
     
      if(events == 0x05)
      {     events &= ~0x05; 
          if(( CountType == 0) || ( CountType == 1) )
          { 
          lcd_clear(); 
          y++;
          if(y >5 ){ y = 4;}
           
          }    
          
         if(CountType == 2)
          { 
              
              xy_f++;
              if(xy_f >= 999)
              { 
              xy_f = 999;
              }
             
          } 

           
      
      }
      //sipka dole  PD1
      if(events == 0x06)
      {   events &= ~0x06;
          if(( CountType == 0) || ( CountType == 1))
          {
            lcd_clear();
            y--;
            if(y < 0){y = 0;}
             
          }    
          
          if(CountType == 2)
          {
             
            xy_f--;
            if(xy_f < 0)
            {
             xy_f = 0;
            }   

          }
      }
      //sipka doprava  PD4
      if(events == 0x24)
      {   
       events &= ~0x24; 
      
        lcd_clear();
       if(CountType == 0)
       {     
        x++;
        if(x > 5)
        {
        
        x =5;
        } 
         
        }
      }
      //sipka dolava  PD5
     if(events == 0x14) 
      {   
       events &= ~0x14;
       if(CountType == 0)
       {
        lcd_clear();
                 
         x--;
         if(x <0) 
         {x = 0;
         
         }  
  
        }  
    
       } 
       
        // potvrd
      if(events == 0x44) 
      { 
      events &= ~0x44; 
       lcd_clear();
      CountType++;
      if(CountType == 3)
      {
      CountType = 0;
      }
          
        
      }
      switch (x) { 
      
    case 0 :
     if((CountType==0)) 
              {
            lcd_gotoxy(0, 0);
            lcd_putsf("Vyber signal 1");
            
            lcd_gotoxy(0,1);
            lcd_putsf(Data[Sequence[x]].name); 
           // Sequence[x] = Data[y].order; 
             lcd_gotoxy(10, 1);
            lcd_putsf(","); //6,7,8 misto na displeji v 2 radku     0123456 
            itoa(Data[x].number,Cycle);  
           
             
            lcd_puts(Cycle);
             
             }
              
             if((CountType == 1))
             {   
             lcd_gotoxy(0, 0);
             lcd_putsf("Vzorek:");
             lcd_gotoxy(0,1);
             lcd_putsf(Data[Sequence[x]].name);
             Sequence[x] = Data[y].order; 
             }  
              
               
            if((CountType == 2))
            {  
             lcd_gotoxy(0, 0);
            lcd_putsf("Pocet vzorku:");
             Data[x].number = xy_f;
             if(xy_f <100)
             {
             lcd_gotoxy(0,1);
             lcd_putsf("   ");
             }
            lcd_gotoxy(0, 1);
             itoa(Data[x].number,Cycle);
            lcd_puts(Cycle);

             }
           
    break; 
    
    case 1 :   
    if((CountType==0)) 
              {
            lcd_gotoxy(0, 0);
            lcd_putsf("Vyber signal 2");
            
            lcd_gotoxy(0,1);
            lcd_putsf(Data[Sequence[x]].name); 
           // Sequence[x] = Data[y].order; 
             lcd_gotoxy(10, 1);
            lcd_putsf(","); //6,7,8 misto na displeji v 2 radku     0123456 
            itoa(Data[x].number,Cycle);  
           
             
            lcd_puts(Cycle);
             
             }
              
             if((CountType == 1))
             {   
             lcd_gotoxy(0, 0);
             lcd_putsf("Vzorek:");
             lcd_gotoxy(0,1);
             lcd_putsf(Data[Sequence[x]].name);
             Sequence[x] = Data[y].order; 
             }  
              
               
            if((CountType == 2))
            {  
             lcd_gotoxy(0, 0);
            lcd_putsf("Pocet vzorku:");
             Data[x].number = xy_f;
             if(xy_f <100)
             {
             lcd_gotoxy(0,1);
             lcd_putsf("   ");
             }
            lcd_gotoxy(0, 1);
             itoa(Data[x].number,Cycle);
            lcd_puts(Cycle);

             }
           
     
             
    break;  
    
    case 2 :  
    if((CountType==0)) 
              {
            lcd_gotoxy(0, 0);
            lcd_putsf("Vyber signal 3");
            
            lcd_gotoxy(0,1);
            lcd_putsf(Data[Sequence[x]].name); 
           // Sequence[x] = Data[y].order; 
             lcd_gotoxy(10, 1);
            lcd_putsf(","); //6,7,8 misto na displeji v 2 radku     0123456 
            itoa(Data[x].number,Cycle);  
           
             
            lcd_puts(Cycle);
             
             }
              
             if((CountType == 1))
             {   
             lcd_gotoxy(0, 0);
             lcd_putsf("Vzorek:");
             lcd_gotoxy(0,1);
             lcd_putsf(Data[Sequence[x]].name);
             Sequence[x] = Data[y].order; 
             }  
              
               
            if((CountType == 2))
            {  
             lcd_gotoxy(0, 0);
            lcd_putsf("Pocet vzorku:");
             Data[x].number = xy_f;
             if(xy_f <100)
             {
             lcd_gotoxy(0,1);
             lcd_putsf("   ");
             }
            lcd_gotoxy(0, 1);
             itoa(Data[x].number,Cycle);
            lcd_puts(Cycle);

             }
           
     
           
    break;   
    
    case 3 :  
    if((CountType==0)) 
              {
            lcd_gotoxy(0, 0);
            lcd_putsf("Vyber signal 4");
            
            lcd_gotoxy(0,1);
            lcd_putsf(Data[Sequence[x]].name); 
           // Sequence[x] = Data[y].order; 
             lcd_gotoxy(10, 1);
            lcd_putsf(","); //6,7,8 misto na displeji v 2 radku     0123456 
            itoa(Data[x].number,Cycle);  
           
             
            lcd_puts(Cycle);
             
             }
              
             if((CountType == 1))
             {   
             lcd_gotoxy(0, 0);
             lcd_putsf("Vzorek:");
             lcd_gotoxy(0,1);
             lcd_putsf(Data[Sequence[x]].name);
             Sequence[x] = Data[y].order; 
             }  
              
               
            if((CountType == 2))
            {  
             lcd_gotoxy(0, 0);
            lcd_putsf("Pocet vzorku:");
             Data[x].number = xy_f;
             if(xy_f <100)
             {
             lcd_gotoxy(0,1);
             lcd_putsf("   ");
             }
            lcd_gotoxy(0, 1);
             itoa(Data[x].number,Cycle);
            lcd_puts(Cycle);

             }
           
     
    break; 
    
    case 4 : 
    if((CountType==0)) 
              {
            lcd_gotoxy(0, 0);
            lcd_putsf("Vyber signal 5");
            
            lcd_gotoxy(0,1);
            lcd_putsf(Data[Sequence[x]].name); 
           // Sequence[x] = Data[y].order; 
             lcd_gotoxy(10, 1);
            lcd_putsf(","); //6,7,8 misto na displeji v 2 radku     0123456 
            itoa(Data[x].number,Cycle);  
           
             
            lcd_puts(Cycle);
             
             }
              
             if((CountType == 1))
             {   
             lcd_gotoxy(0, 0);
             lcd_putsf("Vzorek:");
             lcd_gotoxy(0,1);
             lcd_putsf(Data[Sequence[x]].name);
             Sequence[x] = Data[y].order; 
             }  
              
               
            if((CountType == 2))
            {  
             lcd_gotoxy(0, 0);
            lcd_putsf("Pocet vzorku:");
             Data[x].number = xy_f;
             if(xy_f <100)
             {
             lcd_gotoxy(0,1);
             lcd_putsf("   ");
             }
            lcd_gotoxy(0, 1);
             itoa(Data[x].number,Cycle);
            lcd_puts(Cycle);

             }
           
    
    break;  
   
    
    case 5 :  
            if(((CountType == 0)||(CountType == 1)))
            { 
             
            lcd_gotoxy(0,0);
            lcd_putsf("Tepova frekvence"); 
           
             lcd_gotoxy(0,1); 
             itoa(Data[x].number,Frequency);  
             
             lcd_puts(Frequency);
              
             TF =(float)Data[x].number; 
             TF1 = TF / 60;
             TF2 = 1/TF1;
             TF1 = (TF2 / jm); 

             Freq =(unsigned int)TF1;  
             //highbyte = Freq>>8; 
             //lowbyte = Freq;  
           if(status1 == 0)
            {
            for(k = 0; k <= 4;k++)    //for(k = 0; k <=4;k++)horna hranica je pocet vzorkov
                {  i=0; 
                  while(Data[Sequence[k]].type[i])
                  {
                  i++;
                  
                  } 
                  

                 
                 Data[Sequence[k]].wait_loop = Freq -  Data[Sequence[k]].type_frequency*i; 
                 pom =(long int) (Freq -  Data[Sequence[k]].type_frequency*i);
                   if(pom<0)
                    { 
                    lcd_clear();
                    lcd_gotoxy(0,0);
                    lcd_putsf("Vysoka f. pro");
                    lcd_gotoxy(0,1);
                    lcd_putsf(Data[Sequence[k]].name);
                    delay_ms(500); 
                    CountType = 2;
                    status1=1;
                    }
                    i=0; 
                    pom = 0;
                } 
                if(status1==0)
                {
                
                  lcd_clear();
                  lcd_gotoxy(0,0);
                  lcd_putsf("Frekvence OK"); 
                  delay_ms(500);
                  lcd_clear(); 
                  status1= 1; 
                } 
                
                
               
             }   
               
          
            }
//            if((CountType == 1) && (status == 0)) 
//            {   
//                   
//               
//            }

              if((CountType == 2)) 
              
              { 
                status1=0;        
              lcd_gotoxy(0,0);
              lcd_putsf("Tepu za minutu");
               
             Data[x].number = xy_f;      
              
               if(xy_f <100)
                 {
                 lcd_gotoxy(0,1);
                lcd_putsf("   ");
                 } 
                
             lcd_gotoxy(0, 1);
             itoa(Data[x].number,Frequency);
             lcd_puts(Frequency);     
             

             }    
             
             
             
           
            
    break; 

   }
   
      //koniec if(status == 0)
       



  }
    
  }