Subversion Repositories svnkaklik

Rev

Go to most recent revision | Blame | Last modification | View Log | Download

#include "C:\MIHO_DAT\KROKAC\krokac.h"


unsigned int   StepState;     // Vnitrni promenna kroku
signed int     StepDirection; // Smer +1/0/-1

byte const StepTab[8]={       // Tabulka pro krokovy motor
         0b0001,     // 0         
         0b0101,     // 1
         0b0100,     // 2
         0b0110,     // 3
         0b0010,     // 4
         0b1010,     // 5
         0b1000      // 6
         0b1001,     // 7
         };
         
#INT_TIMER2
void StepNext()
{
   unsigned int i;
   
   StepState+=StepDirection;     // posun se na dalsi krok
   i=StepState&0b111;            // vezmi spodni 3 bity stavu
   i=StepTab[i];                 // prekoduj pomoci tabulky
   output_bit(MOTORA,i&0b0001);  // aktivuj jednotlive vystupy
   output_bit(MOTORB,i&0b0010);
   output_bit(MOTORC,i&0b0100);
   output_bit(MOTORD,i&0b1000);
}


void StepSetDirection(signed int Direction)
{
   if(Direction> 1) Direction= 1;
   if(Direction<-1) Direction=-1;
   StepDirection=Direction;
}


void StepSetTime(unsigned int16 Time)
{
   
}

void Delay(unsigned int16 Time)
{
   while(Time>255) {Time-=256;delay_us(256);};
   if(Time!=0) delay_us(Time);
}


void main()
{
unsigned int16 T;
unsigned long int i;

   // Nastav pracovni frekvenci procesoru
   setup_oscillator(OSC_4MHZ);

   // Vypni motorovy vystup
   output_low(MOTORA);
   output_low(MOTORB);
   output_low(MOTORC);
   output_low(MOTORD);

   // Vstupy s pull-up odporem
   port_b_pullups(TRUE);

   // Inicializace A/D prevodniku
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   // Dalsi inicializace
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   // Inicializace stavovych promennych
   StepState=0;

   // Nastaveni casovace
   setup_timer_2(T2_DIV_BY_16,0xFF,1);

   // Nastveni preruseni od casovace
   enable_interrupts(global);
   enable_interrupts(int_timer2);
   
for(;;)
{

   T=read_adc(ADC_READ_ONLY);    // Prectu hodnotu
   read_adc(ADC_START_ONLY);     // Ostartuji dalsi prevod (na pozadi)

   //setup_timer_2(T2_DIV_BY_16,T,1);
   if(T>0) *0x92=T;              // Preload register casovace

   // Podle prepinacu se hybu
   if(input(PIN_B5))
      {
         if(input(PIN_B4)) StepSetDirection(1); else StepSetDirection(-1);
      }
   else
      {  int1 x;   // pomocna promenna aby se volalo spozdeni jen 1x
         if (StepState>230) {
                              if (!input(PIN_B4) && (x))
                              {
                                 StepSetDirection(0);
                                 delay_ms(500);
                                 x=0;
                              };
                              StepSetDirection(-1);
                            }
         if (StepState<30)  {
                              StepSetDirection( 1);
                              x=1;
                            }  
      }
//StepDirection=2;
//for(i=0;i<800;i++)
//{
//   StepNext();
//   Delay(T);
//}
//delay_ms(10);
//StepDirection=-1;
//for(i=0;i<800;i++)
//{
//   StepNext();
//   Delay(T);
//}
//delay_ms(10);

}  // Cyklus for
  
}