1580 |
kakl |
1 |
//******** Mrakomer2 - stepper motor control ***************** |
|
|
2 |
#define VERSION "3.0" // Special version for BART |
1586 |
kakl |
3 |
#define ID "$Id: irmrak.c 1590 2010-08-22 15:16:36Z kakl $" |
1580 |
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); |
1590 |
kakl |
108 |
delay_ms(400); |
1580 |
kakl |
109 |
printf("A"); // mereni teploty 45° nad obzorem |
1590 |
kakl |
110 |
delay_ms(50); |
|
|
111 |
krok(6); |
|
|
112 |
delay_ms(400); |
1580 |
kakl |
113 |
printf("B"); // mereni teploty v zenitu |
1590 |
kakl |
114 |
delay_ms(50); |
|
|
115 |
krok(6); |
|
|
116 |
delay_ms(400); |
1580 |
kakl |
117 |
printf("C"); // mereni teploty 45° nad obzorem na druhou stranu |
1590 |
kakl |
118 |
delay_ms(50); |
1580 |
kakl |
119 |
|
|
|
120 |
j++; // reverz |
|
|
121 |
dolu(); |
|
|
122 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
123 |
|
|
|
124 |
continue; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
if ('i'==uhel) {printf("I"); continue;} // Predani prikazu pro Info |
|
|
128 |
if ('h'==uhel) {printf("H"); continue;} // Predani prikazu pro Topeni |
1585 |
kakl |
129 |
if ('f'==uhel) {printf("F"); continue;} // Predani prikazu pro vypnuti topeni |
1580 |
kakl |
130 |
if ('x'==uhel) // Zjisteni verze FW |
|
|
131 |
{ |
|
|
132 |
printf("Mrakomer - Motor V%s (C) 2006 KAKL\n\r", VER); |
1585 |
kakl |
133 |
printf("%s\r\n", REV); |
1580 |
kakl |
134 |
} |
|
|
135 |
|
|
|
136 |
if ((uhel>='0') && (uhel<='@')) // mereni v pozadovanem uhlu [0..;]=(0..11) |
|
|
137 |
{ |
|
|
138 |
uhel-='0'; |
|
|
139 |
}; |
|
|
140 |
|
|
|
141 |
if(uhel>11) continue; // ochrana, abysme neukroutili draty |
|
|
142 |
|
|
|
143 |
nula(); |
|
|
144 |
j++; // reverz, nahoru |
|
|
145 |
|
|
|
146 |
krok(12); // odkrokuj do roviny |
|
|
147 |
for(i=0; i<uhel; i++) // dale odkrokuj podle pozadovaneho uhlu |
|
|
148 |
{ |
|
|
149 |
krok(2); |
|
|
150 |
}; |
1590 |
kakl |
151 |
delay_ms(400); |
1580 |
kakl |
152 |
printf("S"); |
1590 |
kakl |
153 |
delay_ms(50); |
1580 |
kakl |
154 |
|
|
|
155 |
j++; // reverz |
|
|
156 |
dolu(); |
|
|
157 |
printf("G"); // mereni teploty Zeme (<G>round) |
|
|
158 |
} |
|
|
159 |
} |
|
|
160 |
|