Rev Author Line No. Line
1746 kakl 1 #include "main.h"
2  
3 #case
4  
1749 kakl 5 #define REVERS 3 // 2*256 ms = 0,75 s
1747 kakl 6 #define GO 117 // 117*256 ms = 30 s
1749 kakl 7 #define GO_ZALUZIE 6 // cca 1,5 s
8 #define RECEIVE 10 // cca 2,6 s
1747 kakl 9  
1746 kakl 10 #define S105A PIN_A0
1748 kakl 11 #define R110NU PIN_A1
12 #define R110ND PIN_A2
1746 kakl 13 #define S104A PIN_A3
1748 kakl 14 #define R104VU PIN_A4
15 #define R104VD PIN_A5
16 #define R104JU PIN_A6
17 #define R104JD PIN_A7
1746 kakl 18  
1748 kakl 19 #define S103J PIN_B0
20 #define R103VU PIN_B1
21 #define R103VD PIN_B2
1746 kakl 22 #define S106A PIN_B3
23 #define S107A PIN_B4
24 #define S109A PIN_B5
25  
26 //#define OS101 PIN_C0
27 //#define OS105B PIN_C1
28 //#define OS105C PIN_C2
29 //#define PIN_C3 31763
30 //#define PIN_C4 31764
31 //#define PIN_C5 31765
32  
33 //#define PIN_C6 31766 //TX
34 //#define PIN_C7 31767 //RX
35  
1748 kakl 36 //#define S101A PIN_F0
37 #define S105Z PIN_F1
38 #define S102A PIN_F2
39 #define S110N PIN_F3
40 #define S108A PIN_F4
41 #define S201A PIN_F5
42 #define S110J PIN_F6
1746 kakl 43  
44 /*
45 #define PIN_E0 31776
46 #define PIN_E1 31777
47 #define PIN_E2 31778
48 #define PIN_E3 31779
49 #define PIN_E4 31780
50 #define PIN_E5 31781
51 #define PIN_E6 31782
52 #define PIN_E7 31783
53  
54 #define PIN_F0 31784
55 #define PIN_F1 31785
56 #define PIN_F2 31786
57 #define PIN_F3 31787
58 #define PIN_F4 31788
59 #define PIN_F5 31789
60 #define PIN_F6 31790
61 #define PIN_F7 31791
62  
63 #define PIN_G0 31792
64 #define PIN_G1 31793
65 #define PIN_G2 31794
66 #define PIN_G3 31795
67 #define PIN_G4 31796
68 #define PIN_G5 31797
69  
70 #define PIN_H0 31800
71 #define PIN_H1 31801
72 #define PIN_H2 31802
73 #define PIN_H3 31803
74 #define PIN_H4 31804
75 #define PIN_H5 31805
76 #define PIN_H6 31806
77 #define PIN_H7 31807
78  
79 #define PIN_J0 31808
80 #define PIN_J1 31809
81 #define PIN_J2 31810
82 #define PIN_J3 31811
83 #define PIN_J4 31812
84 #define PIN_J5 31813
85 #define PIN_J6 31814
86 #define PIN_J7 31815
87 */
88  
1747 kakl 89 #bit CREN = 0xFAB.4 // USART enable register
90  
91 #define LED1 PIN_J7
1746 kakl 92 #define LED2 PIN_J6
93  
94 #define ON(port) output_low(port);
95 #define OFF(port) output_high(port);
96  
1747 kakl 97 typedef enum {GO_UP, GO_DOWN, REVERS_UP, REVERS_DOWN, STOP} state;
1746 kakl 98  
1748 kakl 99 state r103V, r110N, r104V, r104J; // Stavy rolet
100 int8 tr103V, tr110N, tr104V, tr104J; // Casovace rolet
101 int8 timer; // Casovac komunikace
1747 kakl 102  
103 #int_TIMER0
104 void TIMER0_isr(void)
105 {
1748 kakl 106 if(tr103V>0) tr103V--;
107 if(tr110N>0) tr110N--;
108 if(tr104V>0) tr104V--;
109 if(tr104J>0) tr104J--;
110 if(timer>0) timer--;
1747 kakl 111 output_toggle(LED2);
112 }
113  
114 void up(state *status, int8 *timer) // Zmena stavu rolety smer nahoru
115 {
116 switch (*status)
117 {
118 case STOP:
119 case GO_DOWN:
120 *status=REVERS_UP;
121 *timer=REVERS;
122 break;
123 case REVERS_UP:
124 case GO_UP:
125 *status=STOP;
126 *timer=0;
127 break;
128 }
129 }
130  
131 void down(state *status, int8 *timer) // Zmena stavu rolety smer dolu
132 {
133 switch (*status)
134 {
135 case STOP:
136 case GO_UP:
137 *status=REVERS_DOWN;
138 *timer=REVERS;
139 break;
140 case REVERS_DOWN:
141 case GO_DOWN:
142 *status=STOP;
143 *timer=0;
144 break;
145 }
146 }
147  
148 #define run(x) { \
149 switch(r##x) \
150 { \
151 case STOP: \
152 OFF(R##x##U); \
153 OFF(R##x##D); \
154 break; \
155 case GO_UP: \
156 ON(R##x##U); \
157 OFF(R##x##D); \
158 if(tr##x==0) {r##x=STOP;} \
159 break; \
160 case GO_DOWN: \
161 OFF(R##x##U); \
162 ON(R##x##D); \
163 if(tr##x==0) {r##x=STOP;} \
164 break; \
165 case REVERS_UP: \
166 OFF(R##x##U); \
167 OFF(R##x##D); \
168 if(tr##x==0) {r##x=GO_UP; tr##x=GO;} \
169 break; \
170 case REVERS_DOWN: \
171 OFF(R##x##U); \
172 OFF(R##x##D); \
173 if(tr##x==0) {r##x=GO_DOWN; tr##x=GO;} \
174 break; \
175 } \
176 }
177  
1746 kakl 178 void main()
179 {
1748 kakl 180 char buf[10];
1746 kakl 181  
1748 kakl 182 setup_adc_ports(NO_ANALOGS|VSS_VDD);
183 setup_adc(ADC_CLOCK_DIV_2);
184 setup_psp(PSP_DISABLED);
185 setup_spi(SPI_SS_DISABLED);
186 setup_wdt(WDT_OFF);
187 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
188 setup_timer_1(T1_DISABLED);
189 setup_timer_2(T2_DISABLED,0,1);
190 setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
191 setup_ccp1(CCP_OFF);
192 setup_comparator(NC_NC_NC_NC);
193 setup_vref(FALSE);
1747 kakl 194  
1748 kakl 195 OFF(S105A); // Vsechno zhasni a zastav
196 OFF(R110NU);
197 OFF(R110ND);
198 OFF(S104A);
199 OFF(R104VU);
200 OFF(R104VD);
201 OFF(R104JU);
202 OFF(R104JD);
203 OFF(S103J);
204 OFF(R103VU);
205 OFF(R103VD);
206 OFF(S106A);
207 OFF(S107A);
208 OFF(S109A);
209 OFF(S105Z);
210 OFF(S102A);
211 OFF(S110N);
212 OFF(S108A);
213 OFF(S201A);
214 OFF(S110J);
1747 kakl 215  
1748 kakl 216 r103V=STOP; // Rolety jsou zastaveny
217 r110N=STOP;
218 r104V=STOP;
219 r104J=STOP;
1749 kakl 220 tr103V=0;
221 tr110N=0;
222 tr104V=0;
223 tr104J=0;
224  
1748 kakl 225 output_high(LED1); // Indikace restartu
226 delay_ms(100);
227 output_low(LED1);
228 delay_ms(30);
229 output_high(LED1);
230 restart_wdt();
231 delay_ms(100);
232 output_low(LED1);
233 delay_ms(30);
234 output_high(LED1);
235 restart_wdt();
1746 kakl 236  
1748 kakl 237 enable_interrupts(INT_TIMER0);
238 enable_interrupts(GLOBAL);
239  
1747 kakl 240 CREN=0; CREN=1; // Reinitialise USART
241  
1746 kakl 242 while(TRUE)
243 {
1748 kakl 244  
245 if(kbhit()) // Chce po nas nekdo neco?
1746 kakl 246 {
1748 kakl 247 buf[0]=getc();
248 if((buf[0]=='S')||(buf[0]=='R')) // Jinym znakem nesmi zacinat prikaz
1746 kakl 249 {
1748 kakl 250 int8 n=0; // Ukazatel do prijimaciho bufferu
251 timer=RECEIVE; // Timeout pro prijem znaku
252  
253 do
1746 kakl 254 {
1748 kakl 255 if(kbhit()) // Je dalsi znak?
1747 kakl 256 {
1748 kakl 257 n++;
258 buf[n]=getc();
1747 kakl 259 }
1748 kakl 260 } while ((buf[n]!='\r')&&(timer>0)&&(n<7));
261  
262 if (timer==0) { CREN=0; CREN=1; } // Reinitialise USART
263  
264 output_toggle(LED1); // Ukonceni prijimani prikazu
265  
266 if((timer>0))
1746 kakl 267 {
1748 kakl 268  
269 if((buf[0]=='S')&&(n>=4)) // Zmena stavu svetla
1747 kakl 270 {
1748 kakl 271 if(buf[1]=='1')
272 {
273 switch(buf[2])
274 {
275 case '2':
276 output_toggle(S102A);
277 break;
278 case '3':
279 output_toggle(S103J);
280 break;
281 case '4':
282 output_toggle(S104A);
283 break;
284 case '5':
285 if(buf[3]=='A') {output_toggle(S105A);}
286 if(buf[3]=='Z') {output_toggle(S105Z);}
287 break;
288 case '6':
289 output_toggle(S106A);
290 break;
291 case '7':
292 output_toggle(S107A);
293 break;
294 case '8':
295 output_toggle(S108A);
296 break;
297 case '9':
298 output_toggle(S109A);
299 break;
300 case 'a':
301 output_toggle(S110J);
302 output_toggle(S110N);
303 break;
304 }
305 }
1747 kakl 306 }
1748 kakl 307  
308 if((buf[0]=='R')&&(n>=5)) // Zmena stavu rolety
309 {
310 if(buf[1]=='1')
311 {
312 switch(buf[2])
313 {
314 case '3':
315 if(buf[4]=='U') {up(&r103V,&tr103V);} else {down(&r103V,&tr103V);}
316 break;
317 case '4':
318 if(buf[3]=='V') if((buf[4]=='U')) {up(&r104V,&tr104V);} else {down(&r104V,&tr104V);}
319 if(buf[3]=='J') if((buf[4]=='U')) {up(&r104J,&tr104J);} else {down(&r104J,&tr104J);}
320 break;
321 case 'a':
322 if((buf[4]=='U')) {up(&r110N,&tr110N);} else {down(&r110N,&tr110N);}
323 break;
324 }
325 }
326 }
327  
1746 kakl 328 }
329 }
330 }
1748 kakl 331  
332 run(103V); // Rizeni rolet za behu
333 run(104V);
334 run(104J);
1747 kakl 335  
1748 kakl 336 switch(r110N) // Zaluzie ma specielni rizeni
337 {
338 case STOP:
339 OFF(R110NU);
340 OFF(R110ND);
341 break;
342 case GO_UP:
343 ON(R110NU);
344 OFF(R110ND);
345 if(tr110N==0) {r110N=STOP;}
346 break;
347 case GO_DOWN:
348 OFF(R110NU);
349 ON(R110ND);
350 if(tr110N==0) {r110N=STOP;}
351 break;
352 case REVERS_UP:
353 OFF(R110NU);
354 OFF(R110ND);
1749 kakl 355 if(tr110N==0) {r110N=GO_UP; tr110N=GO_ZALUZIE;} // Nahoru pouze kratce na otevreni zaluzie
1748 kakl 356 break;
357 case REVERS_DOWN:
358 OFF(R110NU);
359 OFF(R110ND);
1749 kakl 360 if(tr110N==0) {r110N=GO_DOWN; tr110N=2*GO_ZALUZIE;} // Dolu jede dele, aby se najelo na koncak a zatemnilo.
1748 kakl 361 break;
362 }
1747 kakl 363 /*
364 switch(r110A)
365 {
366 case GO_UP:
367 printf("U");
368 break;
369 case GO_DOWN:
370 printf("D");
371 break;
372 case REVERS_UP:
373 printf("R");
374 break;
375 case REVERS_DOWN:
376 printf("E");
377 break;
378 case STOP:
379 printf("S");
380 break;
381 }
382 */
1748 kakl 383 restart_wdt();
1746 kakl 384 }
385 }