Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
6 kaklik 1
#include "C:\MIHO_DAT\KROKAC\krokac.h"
2
 
3
 
4
unsigned int   StepState;     // Vnitrni promenna kroku
5
signed int     StepDirection; // Smer +1/0/-1
6
 
7
byte const StepTab[8]={       // Tabulka pro krokovy motor
8
         0b0001,     // 0         
9
         0b0101,     // 1
10
         0b0100,     // 2
11
         0b0110,     // 3
12
         0b0010,     // 4
13
         0b1010,     // 5
14
         0b1000      // 6
15
         0b1001,     // 7
16
         };
17
 
18
#INT_TIMER2
19
void StepNext()
20
{
21
   unsigned int i;
22
 
23
   StepState+=StepDirection;     // posun se na dalsi krok
24
   i=StepState&0b111;            // vezmi spodni 3 bity stavu
25
   i=StepTab[i];                 // prekoduj pomoci tabulky
26
   output_bit(MOTORA,i&0b0001);  // aktivuj jednotlive vystupy
27
   output_bit(MOTORB,i&0b0010);
28
   output_bit(MOTORC,i&0b0100);
29
   output_bit(MOTORD,i&0b1000);
30
}
31
 
32
 
33
void StepSetDirection(signed int Direction)
34
{
35
   if(Direction> 1) Direction= 1;
36
   if(Direction<-1) Direction=-1;
37
   StepDirection=Direction;
38
}
39
 
40
 
41
void StepSetTime(unsigned int16 Time)
42
{
43
 
44
}
45
 
46
void Delay(unsigned int16 Time)
47
{
48
   while(Time>255) {Time-=256;delay_us(256);};
49
   if(Time!=0) delay_us(Time);
50
}
51
 
52
 
53
void main()
54
{
55
unsigned int16 T;
56
unsigned long int i;
57
 
58
   // Nastav pracovni frekvenci procesoru
59
   setup_oscillator(OSC_4MHZ);
60
 
61
   // Vypni motorovy vystup
62
   output_low(MOTORA);
63
   output_low(MOTORB);
64
   output_low(MOTORC);
65
   output_low(MOTORD);
66
 
67
   // Vstupy s pull-up odporem
68
   port_b_pullups(TRUE);
69
 
70
   // Inicializace A/D prevodniku
71
   setup_adc_ports(sAN0|VSS_VDD);
72
   setup_adc(ADC_CLOCK_INTERNAL);
73
   set_adc_channel(0);
74
 
75
   // Dalsi inicializace
76
   setup_spi(FALSE);
77
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
78
   setup_timer_1(T1_DISABLED);
79
   setup_comparator(NC_NC_NC_NC);
80
   setup_vref(FALSE);
81
 
82
   // Inicializace stavovych promennych
83
   StepState=0;
84
 
85
   // Nastaveni casovace
86
   setup_timer_2(T2_DIV_BY_16,0xFF,1);
87
 
88
   // Nastveni preruseni od casovace
89
   enable_interrupts(global);
90
   enable_interrupts(int_timer2);
91
 
92
for(;;)
93
{
94
 
95
   T=read_adc(ADC_READ_ONLY);    // Prectu hodnotu
96
   read_adc(ADC_START_ONLY);     // Ostartuji dalsi prevod (na pozadi)
97
 
98
   //setup_timer_2(T2_DIV_BY_16,T,1);
99
   if(T>0) *0x92=T;              // Preload register casovace
100
 
101
   // Podle prepinacu se hybu
102
   if(input(PIN_B5))
103
      {
104
         if(input(PIN_B4)) StepSetDirection(1); else StepSetDirection(-1);
105
      }
106
   else
107
      {  int1 x;   // pomocna promenna aby se volalo spozdeni jen 1x
108
         if (StepState>230) {
109
                              if (!input(PIN_B4) && (x))
110
                              {
111
                                 StepSetDirection(0);
112
                                 delay_ms(500);
113
                                 x=0;
114
                              };
115
                              StepSetDirection(-1);
116
                            }
117
         if (StepState<30)  {
118
                              StepSetDirection( 1);
119
                              x=1;
120
                            }  
121
      }
122
//StepDirection=2;
123
//for(i=0;i<800;i++)
124
//{
125
//   StepNext();
126
//   Delay(T);
127
//}
128
//delay_ms(10);
129
//StepDirection=-1;
130
//for(i=0;i<800;i++)
131
//{
132
//   StepNext();
133
//   Delay(T);
134
//}
135
//delay_ms(10);
136
 
137
}  // Cyklus for
138
 
139
}