//============================================================================
// File: Robot.C
// Func: Ovládání robota pomocí čidel a 
//		 informování o stavu přes softwarovou I2C
// Copy: (C)2005 by Michal Hrouda
//       
// Hard: Robot v1.1 (HC908QB8)
//       
// Rem.: Compile with ImageCraft ICC08
//       http://imagecraft.com/software/
//============================================================================

#include	<hidef.h> /* for EnableInterrupts macro */
#include	"derivative.h" /* include peripheral declarations */

#define	MT_LEVO	   		  		0x40
// Zapnutí levého motoru      - PTB6		
#define	MT_PRAVO   		  		0x10
// Zapnutí pravého motoru     - PTB4	
#define	SMER_MT_LEVO	  		0x80
// Zpetný chod levého motoru  - PTB7	
#define	SMER_MT_PRAVO			0x20
// Zpetný chod pravého motoru - PTB5 	

#define	PIEZO					0x08
// Ovládání pieza - PTA3
#define	TLACITKO				0x04
// Tlacitko	- PTA2
#define	PREK					0x02
// Zapnutí detektoru prekazek - PTA1
#define	CARA					0x10
// Zapnutí detektoru cáry - PTA4
#define	SDA						0x20
// I2C (Data) - PTA5
#define	SCL						0x01
// I2C (Hodiny) - PTA0

#define	PREK_LEVO				0x01
// Dektekce prekázky v levo - PTB0
#define	PREK_PRAVO				0x02
// Dektekce prekázky v pravo - PTB1
#define	PREK_PREDLEVO			0x04
// Dektekce prekázky ve predu v levo  - PTB2
#define	PREK_PREDPRAVO		0x08
// Dektekce prekázky ve predu v pravo - PTB3
#define	CARA_LEVO				0x01
// Detektor cary v levo - PTB0
#define	CARA_PRAVO				0x02
// Detektor cary v pravo - PTB1

#define	DisplejAdresa		0x4E 
//I2C adresa obvodu, ktery obsluhuje displej 
#define	LEDAdresa		0x4C 
//I2C adresa obvodu, ktery obsluhuje LEDky 

#define	Displej_1		0x06
#define	Displej_2		0x5B
#define	Displej_3		0x4F
#define	Displej_4		0x66
#define	Displej_5		0x6D
#define	Displej_6		0x7D
#define	Displej_7		0x07
#define	Displej_8		0x7F
#define	Displej_9		0x6F
//Pro prevod BCD na 7 segmentovy displej

//============================================================================
// Promenne
//============================================================================ 

unsigned char 
	i,j,k,				      	//Cykl
	program,			      	//Jaky je zvolen program
	ext_sig,ext_sig_temp,	//Ukladani stavu cidel
	nacteno,				      //Zda byla nactena data
	udalost,			      	//K jake doslo udalosti 
	pipni,pipni2,			    //Povoleni Pieza
	BitCounter,				    //Pomocna promenna pro I2C
	hodnota,hodnota2,	  	//Pro kompletaci dat pro I2C
	cas;					        //Kolikrat doslo k preteceni casovace


//============================================================================
// Podprogramy
//============================================================================ 

//==========================Pomocne podprogramky==============================

void	Cekej (void)	{           //Cekani nez se zapnou vsechny periferie
  for (j=0;j<85;j++)	{               
    for (i=0;i<255;i++);
  }  
}

//============================Procedury pro I2C===============================

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Provide some data setup time to allow
// SDA to stabilize in slave device
// Completely arbitrary delay (10 cycles)
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void I2CBitDelay (void) {
  asm("NOP");
  asm("NOP");
  asm("NOP");
  asm("NOP");
  asm("NOP");  

}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// A START condition is defined as a falling edge
// on SDA while SCL is high
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-
void I2CStartBit (void) {
  PTA &= ~SDA;
  I2CBitDelay ();
  PTA &= ~SCL;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// A STOP condition is defined as a rising edge
// on SDA while SCL is high
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void I2CStopBit (void) {
  PTA &= ~SDA;    
  PTA |= SCL;
  PTA |= SDA;
  I2CBitDelay ();
}


//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Bit delay to provide (approximately) the desired
// SCL frequency
// Again, this is arbitrary (16 cycles)
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void I2CSetupDelay (void) {
  asm("NOP");
  asm("NOP");
}
 
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// I2CTxByte
// Transmit the byte in Acc to the SDA pin
// (Acc will not be restored on return)
// Must be careful to change SDA values only while SCL is low,
// otherwise a STOP or START could be implied
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//No acknowDisplejgment received from slave device
//Some error action can be performed here
//For now, just restore the bus

void I2CNoAck (void) {
  PTA &= ~SCL;
  PTA |= SDA;
}
//----

void I2CAckPoll (void) {
  
  PTA |= SDA;
  DDRA &= ~SDA;              // Set SDA as input
  I2CSetupDelay();         
  PTA |= SCL;                // Clock the line to get ACK
  I2CBitDelay();
  
  if ((PTA & SDA) != 0) { I2CNoAck(); }  // Look for ACK from slave device
  
  PTA &= ~SCL;             // Restore clock line
  DDRA |= SDA;             // SDA back as output
}
//----

void I2CTxCont (void) {
  PTA &= ~SCL;             // Restore clock to low state     
  BitCounter--;            // Decrement the bit counter
  if (BitCounter == 0) { I2CAckPoll(); }
}

//----

void SendHigh (void) { 
  PTA |= SDA;              // Set the data bit value
  I2CSetupDelay();         // Give some time for data setup
  PTA |= SCL;              // Clock it in
  I2CBitDelay();           // Wait a bit
  I2CTxCont();             // Continue
}

//----

void SendLow (void) {
  PTA &= ~SDA;
  I2CSetupDelay();
  PTA |= SCL;
  I2CBitDelay();
  I2CTxCont();
}

//----

void I2CTxByte (unsigned char I2CData) {   
  BitCounter = 8;        
  if ((I2CData & 0x80) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x40) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x20) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x10) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x08) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x04) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x02) == 0) { SendLow(); } else { SendHigh(); }
  if ((I2CData & 0x01) == 0) { SendLow(); } else { SendHigh(); }
}
      


             
//-------------------------------------------------------------
// Send the I2C transmission, including START, address,
// data, and STOP
// -------------------------------------------------------------

void I2CSendIt (unsigned char adresa,unsigned char data) {

   I2CStartBit();                  // Give START condition		                   
                                
   I2CTxByte(adresa);              // Send the eight bits DATA bytes
                                   
   I2CTxByte(data);                // Send the eight bits DATA bytes
	 
   I2CStopBit();                   // Give STOP condition

   I2CBitDelay();                  // Wait a bit

}


//======================Procedury pro ovladani motoru=========================

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Vynulovani ovladacich promennych
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
void robot_motor_konec (void) { 
  PTB |= MT_PRAVO;
	PTB |= MT_LEVO;
	PTB &= ~SMER_MT_PRAVO;
	PTB &= ~SMER_MT_LEVO;    
  pipni = 2;  
  nacteno = 0;
  cas = 0;
  ext_sig_temp = 0;
}  



//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Procedura pro ovladani motoru
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void robot_prek_motor (unsigned char udalost_proc,unsigned int cas_proc) {
 
  switch (udalost_proc) {
    case 6: {
      switch (cas_proc) {
        case 1:  { PTB |= MT_PRAVO;       PTB &= ~MT_LEVO;      PTB &= ~SMER_MT_PRAVO;  PTB |= SMER_MT_LEVO;  break; }
        case 8:  { PTB &= ~MT_PRAVO;      PTB |= MT_LEVO;       PTB |= SMER_MT_PRAVO;   PTB &= ~SMER_MT_LEVO; break; }
        case 16: { PTB |= MT_PRAVO;       PTB &= ~MT_LEVO;      PTB &= ~SMER_MT_PRAVO;  PTB &= ~SMER_MT_LEVO; break; }
        case 24: { PTB &= ~MT_PRAVO;      PTB |= MT_LEVO;       PTB &= ~SMER_MT_PRAVO;  PTB &= ~SMER_MT_LEVO; break; }
        case 32: { PTB |= MT_PRAVO;       PTB |= MT_LEVO;       PTB &= ~SMER_MT_PRAVO;  PTB &= ~SMER_MT_LEVO; break; }
        case 36: { PTB &= ~MT_PRAVO;      PTB &= ~MT_LEVO;      PTB |= SMER_MT_PRAVO;   PTB |= SMER_MT_LEVO;  break; }
        case 40: { PTB |= MT_PRAVO;       PTB |= MT_LEVO;       PTB &= ~SMER_MT_PRAVO;  PTB &= ~SMER_MT_LEVO; break; }
        case 44: { PTB &= ~MT_PRAVO;      PTB &= ~MT_LEVO;      PTB |= SMER_MT_PRAVO;   PTB |= SMER_MT_LEVO;  break; }
        case 48: { PTB |= MT_PRAVO;       PTB |= MT_LEVO;       PTB &= ~SMER_MT_PRAVO;  PTB &= ~SMER_MT_LEVO; break; }
        case 52: { PTB &= ~MT_PRAVO;      PTB &= ~MT_LEVO;      PTB |= SMER_MT_PRAVO;   PTB |= SMER_MT_LEVO;  break; }
        case 56: { robot_motor_konec();   break; }
      } break;
    }
    case 1: {
      switch (cas_proc) {       
        case 1:  { PTB &= ~MT_PRAVO;      PTB |= MT_LEVO;     PTB &= ~SMER_MT_PRAVO;      PTB &= ~SMER_MT_LEVO;     break; }
        case 5:  { robot_motor_konec();   break; }
      } break;
    }

    case 2: {
      switch (cas_proc) {
        case 1:  { PTB |= MT_PRAVO;       PTB &= ~MT_LEVO;    PTB &= ~SMER_MT_PRAVO;      PTB &= ~SMER_MT_LEVO;     break; }
		    case 5:  { robot_motor_konec();   break; }
      } break; 
    }

    case 3: {
      switch (cas_proc) {
        case 1:  { PTB &= ~MT_PRAVO;      PTB &= ~MT_LEVO;    PTB |= SMER_MT_PRAVO;       PTB |= SMER_MT_LEVO;      break; }
        case 9:  { PTB &= ~MT_PRAVO;      PTB |= MT_LEVO;     PTB |= SMER_MT_PRAVO;       PTB &= ~SMER_MT_LEVO;     break; }
        case 15: { robot_motor_konec();   break; }
      } break;
    }
    
    case 4: {
      switch (cas_proc) {
        case 1:  { PTB |= MT_PRAVO;       PTB &= ~MT_LEVO;    PTB &= ~SMER_MT_PRAVO;      PTB |= SMER_MT_LEVO;      break; }
        case 8:  { robot_motor_konec();   break; }
      } break;
    }
    
    case 5: {
      switch (cas_proc) {
        case 1:  { PTB &= ~MT_PRAVO;      PTB |= MT_LEVO;     PTB |= SMER_MT_PRAVO;       PTB &= ~SMER_MT_LEVO;     break; }
        case 8:  { robot_motor_konec();   break; }
      } break;
    }
    
    case 7: {
      switch (cas_proc) {
        case 1: { PTB &= ~MT_PRAVO;     PTB |= SMER_MT_PRAVO; break; }
        case 3: { robot_motor_konec();  break; }
      } break;
    }
    
    case 8: {
      switch (cas_proc) {
        case 1: { PTB &= ~MT_LEVO;      PTB |= SMER_MT_LEVO; break; }
		    case 3: { robot_motor_konec();  break; };
      } break;
    }
  }

  
}

//============================================================================ 

void zvol_program (void) {
  
  if ((nacteno == 0) && (program == 0)) {
    program = 1;                          //Nastaveni prvniho programu
  	while (nacteno == 0) {
      if ((PTA & 0x04) != 0) {
        Cekej();                            //Jednoduche osetreni zakmitu tlacitka
  	    program++;
  	    if ((PTA & 0x04) != 0) {
  	      Cekej();                        //Prodleva pro zjisteni toho, zda je tlacitko pridrzeno dele
  	      if ((PTA & 0x04) != 0) {
  	        Cekej();                      //Prodleva pro zjisteni toho, zda je tlacitko pridrzeno dele
  	        if ((PTA & 0x04) != 0) {
  	          program--; 
  	          nacteno=1;                  //Kdyz bylo podryrno dele spust zvoleny program
  	        }
  	      }
  	    }
             
  	    switch(program) {
  	      case 1: { hodnota = ~Displej_1;   hodnota2 = 0x02;    break;  }
  	      case 2: { hodnota = ~Displej_2;   hodnota2 = 0x22;    break;  }
  	      case 3: { hodnota = ~Displej_3;   hodnota2 = 0x06;    break;  }
  	      case 4: { hodnota = ~Displej_4;   hodnota2 = 0x26;    break;  }
  	      case 5: { hodnota = ~Displej_5;   hodnota2 = 0x10;    break;  }
  	      case 6: { hodnota = ~Displej_6;   hodnota2 = 0x30;    break;  }
          case 7: { hodnota = ~Displej_7;   hodnota2 = 0x14;    break;  }
          case 8: { hodnota = ~Displej_8;   hodnota2 = 0x34;    break;  }
          case 9: { hodnota = ~Displej_9;   hodnota2 = 0xF6;    break;  }
  	      case 10:{ hodnota = ~Displej_1;   hodnota2 = 0x02;    program=1;   break;  }
  	    }
   
        if (nacteno == 1) { hodnota |= 0x80; hodnota2 |= 0x08;}
  	      
  	    I2CSendIt (DisplejAdresa,hodnota);
  	    I2CSendIt (LEDAdresa,~hodnota2);
      }
  	}
  	nacteno =0;
     
      // Nastaveni cidel a promennych podle zvoleneho programu  
    if ((program == 1) || (program == 2)) {
      PTA &= ~PREK;		                   //Spusteni cidla prekazek 
    }
       
    if ((program == 3) || (program == 4)) {
      PTA &= ~PREK;		                   //Spusteni cidla prekazek
      hodnota2 |= 0x01;
      I2CSendIt (LEDAdresa,~hodnota2);       //Spusteni svetel 
    }
       
    if ((program == 5) || (program == 6)) {
      PTA |= CARA;		                   //Spusteni cidla cary
    }
       
    if ((program == 7) || (program == 8)) {
      PTA |= CARA;		                   //Spusteni cidla cary
      hodnota2 |= 0x01;
      I2CSendIt (LEDAdresa,~hodnota2);       //Spusteni svetel 
    }
      
    if (program != 9) {
      DDRA &= ~SDA;
      DDRA &= ~SCL;
    }
  }
}
 

//============================================================================
// Hlavni program
//============================================================================

void main(void)
	 {
	   CONFIG1 = 0x33;	//00110001 = 0x31
					            //||||||||	   
					            //|||||||+-	COPD	   
					            //||||||+--	STOP
        					    //|||||+---	SSREC
        					    //||||+----	LVI5OR3    
        					    //|||+-----	LVIPWRD           
        					    //||+------	LVIRSTD       
        					    //|+-------	LVISTOP                           
        					    //+--------	COPRS 											  

     CONFIG2 = 0x00;	
           
	   TSC = 0x66;	    //01110110 = 0x76
	   	                //||||||||	   
	                    //|||||||+- PS0    = Prescaler Select Bit	   
	                    //||||||+--	PS1    = Prescaler Select Bit
	                    //|||||+---	PS2    = Prescaler Select Bit
	                    //||||+----	N/A   
	                    //|||+-----	TRST   = Reset Bit                      (0=No effect           1=Prescaler and Counter Cleared)   
	                   	//||+------	TSTOP  = Stop Bit                       (0=Counter actived     1=Counter stopped)   
	                    //|+------- TOIE   = Overflow Interrupt Enable Bit  (0=Disabled            1=Enaled)
	                    //+--------	TOF    = Overflow Flag Bit              (0=Not Reached         1=Reached)  
	  
	   TSC0 = 0x00;		  //00000000 = 0x00
	                    //||||||||	   
	                    //|||||||+- CH0MAX = Channel 0  Max Duty Cycle Bit  (See to the datasheet)	   
	                    //||||||+--	TOV0   = Toggle On Overflow Bit         (0=Not Toggle on OVFL  1=Toggle on OVFL)
	                    //|||||+---	ELS0A  = Edge/Level Bit                 (See to the datasheet)
	                    //||||+----	ELS0B  = Edge/Level Bit                 (See to the datasheet)
	                    //|||+-----	MS0A   = Mode Select Bit A              (See to the datasheet)  
	                    //||+------	MS0B   = Mode Select Bit A              (See to the datasheet)
	                    //|+------- CH0IE  = Channel 0 Interrupt Enable Bit (0=Disabled            1=Enabled)
	                    //+--------	CH0F   = Channel 0 Flag Bit             (0=No IO Compare       1=IO Compare)   
	   
	   
	   TSC1 = 0x00;		//00000000 = 0x00
	                    //||||||||	   
	                    //|||||||+- CH1MAX = Channel 1  Max Duty Cycle Bit  (See to the datasheet)	   
	                    //||||||+--	TOV1   = Toggle On Overflow Bit         (0=Not Toggle on OVFL  1=Toggle on OVFL)
	                    //|||||+---	ELS1A  = Edge/Level Bit                 (See to the datasheet)
	                    //||||+----	ELS1B  = Edge/Level Bit                 (See to the datasheet)
	                    //|||+-----	MS1A   = Mode Select Bit A              (See to the datasheet)  
	                    //||+------	N/A   
	                    //|+------- CH1IE  = Channel 1 Interrupt Enable Bit (0=Disabled            1=Enabled)
	                    //+--------	CH1F   = Channel 1 Flag Bit             (0=No IO Compare       1=IO Compare) 
	   
	   INTSCR = 0x00;

	   TMODH = 0x07;		    //Modulo count = 3906 (0x07A2) protoze 1MHz/64 = 15625 chci 4Hz t.j. vydelit  jeste 3906
	   TMODL = 0xA2;	      //Citac cita od nuly proto TMOD = 0x07A2
	   
	   DDRA = ~0x04;	 	    //PTA 0,1,3,4,5,6,7 - výstupy | PTA 2 - Vstupy   
	   DDRB = 0xF0;			    //PTB 4,5,6,7 - výstupy | PTB 0,1,2,3 - Vstupy             
                        
	   PTA = 0x23;			    //Vynulování PTA
	   PTB = 0x00;			    //Vynulování PTB
	   	     
	   nacteno = 0;
	   hodnota2 = 0x00;
	   
	   Cekej();                               //Cekani nez se zapnou vsechny periferie
	   
	   I2CSendIt (LEDAdresa,hodnota2);        //Rosviceni vsech led
	       
     Cekej();                                 //Cekani nez se zapnou vsechny periferie
     
     hodnota2 = 0x02;
	   I2CSendIt (LEDAdresa,~hodnota2);
	   I2CSendIt(DisplejAdresa,~Displej_1);   //Odeslani jednicky na displje
	   
     zvol_program();     
                              
	   PTB |= MT_PRAVO;       //Robot pojede dopredu
	   PTB |= MT_LEVO;
	   PTB &= ~SMER_MT_PRAVO;
	   PTB &= ~SMER_MT_LEVO;
	   
	   Cekej();               //Cekani nez se zapnou vsechny periferie
	   Cekej();               //Cekani nez se zapnou vsechny periferie
  	    	   	   
	   EnableInterrupts;    	//Povoleni preruseni
	   TSC &= ~0x20;		    //Spusteni casovace
	   
	   for (;;) {           	//Nekonecna smycka 
		   ext_sig = PTB;       //Ukladani stavu cidel
		             
		   if ((((program %2) == 0) || (program == 9)) && (pipni != 0)) {
		     PTA ^= PIEZO;
		     for (j=0;j<(pipni*11);j++);
		     PTA ^= PIEZO;
		     for (j=0;j<(pipni*11);j++);     
		   }

	   }
	            
	 }
	 
      
	
//============================================================================
// Obsluhy HW preruseni
//============================================================================


//=========================Obsluha preruseni casovace=========================
		 					  				
void interrupt 6 overflow_isr (void) {
   DisableInterrupts;
   
	 TSC &= ~0x88;		  //Potvrzerni ze doslo k preteceni casovace
	 pipni = 0;
	 
	 if (nacteno == 0) {
	 
  	 if ((program == 5) || (program == 6) || (program == 7) || (program == 8)){
  	   ext_sig = ext_sig & 0x03;      //Zamaskovani carovych cidel
  	   if (ext_sig == 0x03) {
  		   udalost = 0;               //Zadna udalost
  		   nacteno = 0;               //Data nenactena
  		   cas = 0;                   //Nevyuzite preruseni
  	   } else {
  	     ext_sig_temp = ext_sig;      //Ulozeni stavu cidel
  	     
  	     switch (ext_sig_temp) {
  	      case 0x01: {  udalost = 7;  break;}
  	      case 0x02: {  udalost = 8;  break;}
  	      case 0x00: {  udalost = 8;  break;}
  	     }
  		   
         nacteno = 1;               //Doslo k nacteni dat
  	   }
  	 }
  	 	   
  	 if ((program == 1) || (program == 2) || (program == 3) || (program == 4)) {
  	   ext_sig = ext_sig & 0x0F;      //Zamaskovani prekaykovych cidel
  	   
  	   if (ext_sig == 0x00) {
  		   udalost = 0;               //Zadna udalost
  		   nacteno = 0;               //Data nenactena
  		   cas = 0;                   //Nevyuzite preruseni
  	   } else {
  	     ext_sig_temp = ext_sig;      //Ulozeni stavu cidel
  	     
  		   switch (ext_sig_temp) {
  		    case 0x01: {  udalost = 1;  break;} 
  		    case 0x02: {  udalost = 2;  break;}
  		    case 0x03: {  udalost = 3;  break;}
  		    case 0x0C: {  udalost = 3;  break;}
  		    case 0x0D: {  udalost = 3;  break;}
  		    case 0x0E: {  udalost = 3;  break;}
  		    case 0x0F: {  udalost = 3;  break;} 
  		    case 0x08: {  udalost = 4;  break;} 
  		    case 0x09: {  udalost = 4;  break;}
  		    case 0x0A: {  udalost = 4;  break;}
  		    case 0x0B: {  udalost = 4;  break;}
  		    case 0x05: {  udalost = 5;  break;}
  		    case 0x06: {  udalost = 5;  break;}
  		    case 0x07: {  udalost = 5;  break;}
  		    case 0x04: {  udalost = 5;  break;}
  		   }
  		     		 	 
  		 nacteno = 1;                    //Doslo k nacteni dat
  	   }
  	 }
  	 
	 }
	 
	 if (program == 9) { 
	   udalost = 6; 
	   nacteno = 1;
	   if ((cas % 8) == 0) {               //Kazde 2s
	     hodnota2 ^= 0x01;
	     I2CSendIt (LEDAdresa,~hodnota2);  //Blikani svetel
	   }
	   if ((cas % 4) == 0) { pipni2++; pipni = pipni2;}      //Kazdou 1s pipni 
	   if (pipni2 > 3) { pipni2 = 0; }
	 }
	 
	 if (nacteno == 1) { cas++; }         //Kdyz byla nactena data pricti cas
	 	 
	 	 
	 if ((nacteno == 1) && (udalost != 0)) {  robot_prek_motor (udalost,cas); }    
	                                      //Nastaveni motoru podle udalosti a kroku
	 
	 EnableInterrupts;

}
	 



                 




