#include "main.h"
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2

#define H1 PIN_A1
#define L1 PIN_A2
#define H2 PIN_A3
#define L2 PIN_A4

signed int8 command;  // rozsah +-127

#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;

   output_a(0);         // vypnuti vsech budicu

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      command = i2c_read();
   }

   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(command);
   }
}


void main()
{
   int8 speed;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_8MHZ|OSC_INTRC);

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

   command=-128;  // zastaveni po resetu

   while(true)
   {

      if (command==-128) // prikaz na odpojeni mustku nebo chybna hodnota
      {
         output_a(0);   // volnobeh
         continue;
      };

      speed=command+127;   // posunuti 0 pro zaporna cisla

      output_a(0b10010);   // vpred
      delay_us(speed);
      output_a(0);         // vypnuti vsech budicu
      delay_us(1);
      restart_wdt();
      output_a(0b01100);   // vzad
      delay_us(254-speed);
      output_a(0);         // vypnuti vsech budicu
      delay_us(1);
   }
}