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 |
1585 |
kakl |
51 |
reset_cpu(); |
1580 |
kakl |
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 |
{ |
1585 |
kakl |
61 |
port=0b10010000; // vychozi nastaveni fazi pro rizeni motoru |
1580 |
kakl |
62 |
output_B(port); |
1585 |
kakl |
63 |
j=1; // smer dolu |
1580 |
kakl |
64 |
delay_ms(500); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
//------------------------------------------------ |
|
|
69 |
void main() |
|
|
70 |
{ |
|
|
71 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
72 |
setup_adc(ADC_OFF); |
1585 |
kakl |
73 |
setup_spi(SPI_SS_DISABLED); |
|
|
74 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
1580 |
kakl |
75 |
setup_timer_1(T1_DISABLED); |
1585 |
kakl |
76 |
setup_timer_2(T2_DISABLED,0,1); |
1580 |
kakl |
77 |
setup_ccp1(CCP_OFF); |
1585 |
kakl |
78 |
setup_comparator(NC_NC_NC_NC); |
|
|
79 |
setup_vref(FALSE); |
|
|
80 |
setup_oscillator(OSC_8MHZ|OSC_INTRC); |
1580 |
kakl |
81 |
|
|
|
82 |
output_B(0); // vypnuti motoru a topeni |
|
|
83 |
set_tris_B(0b00000111); // faze a topeni jako vystupy |
|
|
84 |
|
1585 |
kakl |
85 |
nula(); |
|
|
86 |
dolu(); // otoc trubku do vychozi pozice dolu |
|
|
87 |
|
1580 |
kakl |
88 |
while(true) |
|
|
89 |
{ |
|
|
90 |
CREN=0; CREN=1; // Reinitialise USART |
|
|
91 |
|
|
|
92 |
while(!kbhit()) |
|
|
93 |
{ |
|
|
94 |
if (!input(HALL)) // znovuotoceni trubky dolu, kdyby ji vitr otocil |
|
|
95 |
{ |
|
|
96 |
dolu(); |
|
|
97 |
} |
|
|
98 |
}; // pokracuj dal, kdyz prisel po RS232 znak |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
uhel=getc(); // prijmi znak |
|
|
102 |
if ('m'==uhel) // standardni mereni ve trech polohach |
|
|
103 |
{ |
|
|
104 |
nula(); |
|
|
105 |
j++; // reverz, nahoru |
|
|
106 |
|
|
|
107 |
krok(18); |
|
|
108 |
printf("A"); // mereni teploty 45° nad obzorem |
1585 |
kakl |
109 |
delay_ms(300); |
1580 |
kakl |
110 |
krok(7); |
|
|
111 |
printf("B"); // mereni teploty v zenitu |
1585 |
kakl |
112 |
delay_ms(300); |
1580 |
kakl |
113 |
krok(7); |
|
|
114 |
printf("C"); // mereni teploty 45° nad obzorem na druhou stranu |
1585 |
kakl |
115 |
delay_ms(300); |
1580 |
kakl |
116 |
|
|
|
117 |
j++; // reverz |
|
|
118 |
dolu(); |
|
|
119 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
120 |
|
|
|
121 |
continue; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
if ('i'==uhel) {printf("I"); continue;} // Predani prikazu pro Info |
|
|
125 |
if ('h'==uhel) {printf("H"); continue;} // Predani prikazu pro Topeni |
1585 |
kakl |
126 |
if ('f'==uhel) {printf("F"); continue;} // Predani prikazu pro vypnuti topeni |
1580 |
kakl |
127 |
if ('x'==uhel) // Zjisteni verze FW |
|
|
128 |
{ |
|
|
129 |
printf("Mrakomer - Motor V%s (C) 2006 KAKL\n\r", VER); |
1585 |
kakl |
130 |
printf("%s\r\n", REV); |
1580 |
kakl |
131 |
} |
|
|
132 |
|
|
|
133 |
if ((uhel>='0') && (uhel<='@')) // mereni v pozadovanem uhlu [0..;]=(0..11) |
|
|
134 |
{ |
|
|
135 |
uhel-='0'; |
|
|
136 |
}; |
|
|
137 |
|
|
|
138 |
if(uhel>11) continue; // ochrana, abysme neukroutili draty |
|
|
139 |
|
|
|
140 |
nula(); |
|
|
141 |
j++; // reverz, nahoru |
|
|
142 |
|
|
|
143 |
krok(12); // odkrokuj do roviny |
|
|
144 |
for(i=0; i<uhel; i++) // dale odkrokuj podle pozadovaneho uhlu |
|
|
145 |
{ |
|
|
146 |
krok(2); |
|
|
147 |
}; |
|
|
148 |
printf("S"); |
1585 |
kakl |
149 |
delay_ms(300); |
1580 |
kakl |
150 |
|
|
|
151 |
j++; // reverz |
|
|
152 |
dolu(); |
|
|
153 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
154 |
} |
|
|
155 |
} |
|
|
156 |
|