Rev Author Line No. Line
1746 kakl 1 #include "main.h"
2  
3 #case
4  
1747 kakl 5 #define REVERS 2 // 2*256 ms = 0,5 s
6 #define GO 117 // 117*256 ms = 30 s
7 #define GO_ZALUZIE 5 // 5*256 ms = 1,25 s
1748 kakl 8 #define RECEIVE 12 // 3 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;
220  
221 output_high(LED1); // Indikace restartu
222 delay_ms(100);
223 output_low(LED1);
224 delay_ms(30);
225 output_high(LED1);
226 restart_wdt();
227 delay_ms(100);
228 output_low(LED1);
229 delay_ms(30);
230 output_high(LED1);
231 restart_wdt();
1746 kakl 232  
1748 kakl 233 enable_interrupts(INT_TIMER0);
234 enable_interrupts(GLOBAL);
235  
1747 kakl 236 CREN=0; CREN=1; // Reinitialise USART
237  
1746 kakl 238 while(TRUE)
239 {
1748 kakl 240  
241 if(kbhit()) // Chce po nas nekdo neco?
1746 kakl 242 {
1748 kakl 243 buf[0]=getc();
244 if((buf[0]=='S')||(buf[0]=='R')) // Jinym znakem nesmi zacinat prikaz
1746 kakl 245 {
1748 kakl 246 int8 n=0; // Ukazatel do prijimaciho bufferu
247 timer=RECEIVE; // Timeout pro prijem znaku
248  
249 do
1746 kakl 250 {
1748 kakl 251 if(kbhit()) // Je dalsi znak?
1747 kakl 252 {
1748 kakl 253 n++;
254 buf[n]=getc();
1747 kakl 255 }
1748 kakl 256 } while ((buf[n]!='\r')&&(timer>0)&&(n<7));
257  
258 if (timer==0) { CREN=0; CREN=1; } // Reinitialise USART
259  
260 output_toggle(LED1); // Ukonceni prijimani prikazu
261  
262 if((timer>0))
1746 kakl 263 {
1748 kakl 264  
265 if((buf[0]=='S')&&(n>=4)) // Zmena stavu svetla
1747 kakl 266 {
1748 kakl 267 if(buf[1]=='1')
268 {
269 switch(buf[2])
270 {
271 case '2':
272 output_toggle(S102A);
273 break;
274 case '3':
275 output_toggle(S103J);
276 break;
277 case '4':
278 output_toggle(S104A);
279 break;
280 case '5':
281 if(buf[3]=='A') {output_toggle(S105A);}
282 if(buf[3]=='Z') {output_toggle(S105Z);}
283 break;
284 case '6':
285 output_toggle(S106A);
286 break;
287 case '7':
288 output_toggle(S107A);
289 break;
290 case '8':
291 output_toggle(S108A);
292 break;
293 case '9':
294 output_toggle(S109A);
295 break;
296 case 'a':
297 output_toggle(S110J);
298 output_toggle(S110N);
299 break;
300 }
301 }
1747 kakl 302 }
1748 kakl 303  
304 if((buf[0]=='R')&&(n>=5)) // Zmena stavu rolety
305 {
306 if(buf[1]=='1')
307 {
308 switch(buf[2])
309 {
310 case '3':
311 if(buf[4]=='U') {up(&r103V,&tr103V);} else {down(&r103V,&tr103V);}
312 break;
313 case '4':
314 if(buf[3]=='V') if((buf[4]=='U')) {up(&r104V,&tr104V);} else {down(&r104V,&tr104V);}
315 if(buf[3]=='J') if((buf[4]=='U')) {up(&r104J,&tr104J);} else {down(&r104J,&tr104J);}
316 break;
317 case 'a':
318 if((buf[4]=='U')) {up(&r110N,&tr110N);} else {down(&r110N,&tr110N);}
319 break;
320 }
321 }
322 }
323  
1746 kakl 324 }
325 }
326 }
1748 kakl 327  
328 run(103V); // Rizeni rolet za behu
329 run(104V);
330 run(104J);
1747 kakl 331  
1748 kakl 332 switch(r110N) // Zaluzie ma specielni rizeni
333 {
334 case STOP:
335 OFF(R110NU);
336 OFF(R110ND);
337 break;
338 case GO_UP:
339 ON(R110NU);
340 OFF(R110ND);
341 if(tr110N==0) {r110N=STOP;}
342 break;
343 case GO_DOWN:
344 OFF(R110NU);
345 ON(R110ND);
346 if(tr110N==0) {r110N=STOP;}
347 break;
348 case REVERS_UP:
349 OFF(R110NU);
350 OFF(R110ND);
351 if(tr110N==0) {r110N=GO_UP; tr110N=GO_ZALUZIE;}
352 break;
353 case REVERS_DOWN:
354 OFF(R110NU);
355 OFF(R110ND);
356 if(tr110N==0) {r110N=GO_DOWN; tr110N=GO_ZALUZIE;}
357 break;
358 }
1747 kakl 359 /*
360 switch(r110A)
361 {
362 case GO_UP:
363 printf("U");
364 break;
365 case GO_DOWN:
366 printf("D");
367 break;
368 case REVERS_UP:
369 printf("R");
370 break;
371 case REVERS_DOWN:
372 printf("E");
373 break;
374 case STOP:
375 printf("S");
376 break;
377 }
378 */
1748 kakl 379 restart_wdt();
1746 kakl 380 }
381 }