1580 |
kakl |
1 |
//******** Mrakomer2 - stepper motor control ***************** |
|
|
2 |
#define VERSION "3.0" // Special version for BART |
|
|
3 |
#define ID "$Id: irmrak.c 420 2006-12-29 21:43:11Z kakl $" |
|
|
4 |
//************************************************************ |
|
|
5 |
|
|
|
6 |
#include "irmrak.h" |
|
|
7 |
#include <string.h> |
|
|
8 |
|
|
|
9 |
char VER[4]=VERSION; |
|
|
10 |
char REV[50]=ID; |
|
|
11 |
|
|
|
12 |
#bit CREN = 0x18.4 // USART registers |
|
|
13 |
#bit SPEN = 0x18.7 |
|
|
14 |
#bit OERR = 0x18.1 |
|
|
15 |
#bit FERR = 0x18.2 |
|
|
16 |
|
|
|
17 |
#define HALL PIN_A4 // Hallova sonda pro zjisteni natoceni dolu |
|
|
18 |
// vykonovy FET je na RB3 (vystup PWM) |
|
|
19 |
|
|
|
20 |
int port; // stav brany B pro krokove motory |
|
|
21 |
int j; // pro synchronisaci fazi |
|
|
22 |
unsigned int8 uhel; // prijaty znak |
|
|
23 |
unsigned int8 i; // pro cyklus for |
|
|
24 |
|
|
|
25 |
// --- Kroky krokoveho motoru --- |
|
|
26 |
void krok(int n) |
|
|
27 |
{ |
|
|
28 |
while((n--)>0) |
|
|
29 |
{ |
|
|
30 |
if (1==(j&1)) {port^=0b11000000;} else {port^=0b00110000;}; |
|
|
31 |
output_B(port); |
|
|
32 |
delay_ms(20); // Nutno nastavit podle dynamiky systemu. |
|
|
33 |
j++; |
|
|
34 |
} |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
// --- Dojet dolu magnetem na cidlo --- |
|
|
38 |
void dolu() |
|
|
39 |
{ |
|
|
40 |
unsigned int8 err; // pocitadlo pro zjisteni zaseknuti otaceni |
|
|
41 |
|
|
|
42 |
err=0; |
|
|
43 |
while(!input(HALL)) // otoceni trubky dolu az na hall |
|
|
44 |
{ |
|
|
45 |
krok(1); |
|
|
46 |
err++; |
|
|
47 |
if(40==err) // do 40-ti kroku by se melo podarit otocit dolu |
|
|
48 |
{ |
|
|
49 |
output_B(0); // vypnuti motoru |
|
|
50 |
printf("E"); // Hlasime chybu |
|
|
51 |
err=0; |
|
|
52 |
} |
|
|
53 |
}; |
|
|
54 |
delay_ms(500); // cas na ustaleni trubky |
|
|
55 |
output_B(0); // vypnuti motoru |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
// --- Najeti na vychozi polohu dole --- |
|
|
59 |
void nula() |
|
|
60 |
{ |
|
|
61 |
port=0b10100000; // vychozi nastaveni fazi pro rizeni motoru |
|
|
62 |
output_B(port); |
|
|
63 |
j=0; // smer dolu |
|
|
64 |
delay_ms(500); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
//------------------------------------------------ |
|
|
69 |
void main() |
|
|
70 |
{ |
|
|
71 |
setup_oscillator(OSC_8MHZ|OSC_INTRC); // 8 MHz interni RC oscilator |
|
|
72 |
|
|
|
73 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
74 |
setup_adc(ADC_OFF); |
|
|
75 |
setup_spi(FALSE); |
|
|
76 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
77 |
setup_timer_1(T1_DISABLED); |
|
|
78 |
setup_ccp1(CCP_OFF); |
|
|
79 |
setup_comparator(NC_NC_NC_NC); |
|
|
80 |
setup_vref(FALSE); |
|
|
81 |
|
|
|
82 |
output_B(0); // vypnuti motoru a topeni |
|
|
83 |
set_tris_B(0b00000111); // faze a topeni jako vystupy |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
while(true) |
|
|
87 |
{ |
|
|
88 |
nula(); |
|
|
89 |
dolu(); // otoc trubku do vychozi pozice dolu |
|
|
90 |
|
|
|
91 |
CREN=0; CREN=1; // Reinitialise USART |
|
|
92 |
|
|
|
93 |
while(!kbhit()) |
|
|
94 |
{ |
|
|
95 |
if (!input(HALL)) // znovuotoceni trubky dolu, kdyby ji vitr otocil |
|
|
96 |
{ |
|
|
97 |
dolu(); |
|
|
98 |
} |
|
|
99 |
}; // pokracuj dal, kdyz prisel po RS232 znak |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
uhel=getc(); // prijmi znak |
|
|
103 |
if ('m'==uhel) // standardni mereni ve trech polohach |
|
|
104 |
{ |
|
|
105 |
nula(); |
|
|
106 |
j++; // reverz, nahoru |
|
|
107 |
|
|
|
108 |
krok(18); |
|
|
109 |
printf("A"); // mereni teploty 45° nad obzorem |
|
|
110 |
delay_ms(200); |
|
|
111 |
krok(7); |
|
|
112 |
printf("B"); // mereni teploty v zenitu |
|
|
113 |
delay_ms(200); |
|
|
114 |
krok(7); |
|
|
115 |
printf("C"); // mereni teploty 45° nad obzorem na druhou stranu |
|
|
116 |
delay_ms(200); |
|
|
117 |
|
|
|
118 |
j++; // reverz |
|
|
119 |
dolu(); |
|
|
120 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
121 |
|
|
|
122 |
continue; |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
if ('i'==uhel) {printf("I"); continue;} // Predani prikazu pro Info |
|
|
126 |
if ('h'==uhel) {printf("H"); continue;} // Predani prikazu pro Topeni |
|
|
127 |
if ('c'==uhel) {printf("C"); continue;} // Predani prikazu pro vypnuti topeni |
|
|
128 |
if ('x'==uhel) // Zjisteni verze FW |
|
|
129 |
{ |
|
|
130 |
printf("Mrakomer - Motor V%s (C) 2006 KAKL\n\r", VER); |
|
|
131 |
printf("%s\n\r", REV); |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
if ((uhel>='0') && (uhel<='@')) // mereni v pozadovanem uhlu [0..;]=(0..11) |
|
|
135 |
{ |
|
|
136 |
uhel-='0'; |
|
|
137 |
}; |
|
|
138 |
|
|
|
139 |
if(uhel>11) continue; // ochrana, abysme neukroutili draty |
|
|
140 |
|
|
|
141 |
nula(); |
|
|
142 |
j++; // reverz, nahoru |
|
|
143 |
|
|
|
144 |
krok(12); // odkrokuj do roviny |
|
|
145 |
for(i=0; i<uhel; i++) // dale odkrokuj podle pozadovaneho uhlu |
|
|
146 |
{ |
|
|
147 |
krok(2); |
|
|
148 |
}; |
|
|
149 |
printf("S"); |
|
|
150 |
delay_ms(200); |
|
|
151 |
|
|
|
152 |
j++; // reverz |
|
|
153 |
dolu(); |
|
|
154 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
|