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
8  
1746 kakl 9 #define S105A PIN_A0
1747 kakl 10 #define R110AU PIN_A1
11 #define R110AD PIN_A2
1746 kakl 12 #define S104A PIN_A3
13 #define R104AU PIN_A4
14 #define R104AD PIN_A5
15 #define R104BU PIN_A6
16 #define R104BD PIN_A7
17  
18 #define S103A PIN_B0
19 #define R103AU PIN_B1
20 #define R103AD PIN_B2
21 #define S106A PIN_B3
22 #define S107A PIN_B4
23 #define S109A PIN_B5
24  
25 //#define OS101 PIN_C0
26 //#define OS105B PIN_C1
27 //#define OS105C PIN_C2
28 //#define PIN_C3 31763
29 //#define PIN_C4 31764
30 //#define PIN_C5 31765
31  
32 //#define PIN_C6 31766 //TX
33 //#define PIN_C7 31767 //RX
34  
35 #define S101A PIN_D0
36 #define S105D PIN_D1
37 #define S102A PIN_D2
38 #define S110C PIN_D3
39 #define S108A PIN_D4
40 #define S201A PIN_D5
41 #define S110B PIN_D6
42  
43 /*
44 #define PIN_E0 31776
45 #define PIN_E1 31777
46 #define PIN_E2 31778
47 #define PIN_E3 31779
48 #define PIN_E4 31780
49 #define PIN_E5 31781
50 #define PIN_E6 31782
51 #define PIN_E7 31783
52  
53 #define PIN_F0 31784
54 #define PIN_F1 31785
55 #define PIN_F2 31786
56 #define PIN_F3 31787
57 #define PIN_F4 31788
58 #define PIN_F5 31789
59 #define PIN_F6 31790
60 #define PIN_F7 31791
61  
62 #define PIN_G0 31792
63 #define PIN_G1 31793
64 #define PIN_G2 31794
65 #define PIN_G3 31795
66 #define PIN_G4 31796
67 #define PIN_G5 31797
68  
69 #define PIN_H0 31800
70 #define PIN_H1 31801
71 #define PIN_H2 31802
72 #define PIN_H3 31803
73 #define PIN_H4 31804
74 #define PIN_H5 31805
75 #define PIN_H6 31806
76 #define PIN_H7 31807
77  
78 #define PIN_J0 31808
79 #define PIN_J1 31809
80 #define PIN_J2 31810
81 #define PIN_J3 31811
82 #define PIN_J4 31812
83 #define PIN_J5 31813
84 #define PIN_J6 31814
85 #define PIN_J7 31815
86 */
87  
1747 kakl 88 #bit CREN = 0xFAB.4 // USART enable register
89  
90 #define LED1 PIN_J7
1746 kakl 91 #define LED2 PIN_J6
92  
93 #define ON(port) output_low(port);
94 #define OFF(port) output_high(port);
95  
1747 kakl 96 typedef enum {GO_UP, GO_DOWN, REVERS_UP, REVERS_DOWN, STOP} state;
1746 kakl 97  
1747 kakl 98 state r103A, r110A, r104A, r104B; // Stavy rolet
99 int8 tr103A, tr110A, tr104A, tr104B; // Casovace rolet
100  
101 #int_TIMER0
102 void TIMER0_isr(void)
103 {
104 if(tr103A>0) tr103A--;
105 if(tr110A>0) tr110A--;
106 if(tr104A>0) tr104A--;
107 if(tr104B>0) tr104B--;
108 output_toggle(LED2);
109 }
110  
111 void up(state *status, int8 *timer) // Zmena stavu rolety smer nahoru
112 {
113 switch (*status)
114 {
115 case STOP:
116 case GO_DOWN:
117 *status=REVERS_UP;
118 *timer=REVERS;
119 break;
120 case REVERS_UP:
121 case GO_UP:
122 *status=STOP;
123 *timer=0;
124 break;
125 }
126 }
127  
128 void down(state *status, int8 *timer) // Zmena stavu rolety smer dolu
129 {
130 switch (*status)
131 {
132 case STOP:
133 case GO_UP:
134 *status=REVERS_DOWN;
135 *timer=REVERS;
136 break;
137 case REVERS_DOWN:
138 case GO_DOWN:
139 *status=STOP;
140 *timer=0;
141 break;
142 }
143 }
144  
145 #define run(x) { \
146 switch(r##x) \
147 { \
148 case STOP: \
149 OFF(R##x##U); \
150 OFF(R##x##D); \
151 break; \
152 case GO_UP: \
153 ON(R##x##U); \
154 OFF(R##x##D); \
155 if(tr##x==0) {r##x=STOP;} \
156 break; \
157 case GO_DOWN: \
158 OFF(R##x##U); \
159 ON(R##x##D); \
160 if(tr##x==0) {r##x=STOP;} \
161 break; \
162 case REVERS_UP: \
163 OFF(R##x##U); \
164 OFF(R##x##D); \
165 if(tr##x==0) {r##x=GO_UP; tr##x=GO;} \
166 break; \
167 case REVERS_DOWN: \
168 OFF(R##x##U); \
169 OFF(R##x##D); \
170 if(tr##x==0) {r##x=GO_DOWN; tr##x=GO;} \
171 break; \
172 } \
173 }
174  
1746 kakl 175 void main()
176 {
177 char c;
178  
179 if(restart_cause()!= WDT_TIMEOUT)
180 {
181 setup_adc_ports(NO_ANALOGS|VSS_VDD);
182 setup_adc(ADC_CLOCK_DIV_2);
183 setup_psp(PSP_DISABLED);
184 setup_spi(SPI_SS_DISABLED);
185 setup_wdt(WDT_ON);
1747 kakl 186 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
1746 kakl 187 setup_timer_1(T1_DISABLED);
188 setup_timer_2(T2_DISABLED,0,1);
189 setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
190 setup_ccp1(CCP_OFF);
191 setup_comparator(NC_NC_NC_NC);
192 setup_vref(FALSE);
193  
1747 kakl 194 OFF(S105A); // Vsechno zhasni a zastav
195 OFF(R110AU);
196 OFF(R110AD);
1746 kakl 197 OFF(S104A);
198 OFF(R104AU);
199 OFF(R104AD);
200 OFF(R104BU);
201 OFF(R104BD);
202 OFF(S103A);
203 OFF(R103AU);
204 OFF(R103AD);
205 OFF(S106A);
206 OFF(S107A);
207 OFF(S109A);
208 OFF(S101A);
209 OFF(S105D);
210 OFF(S102A);
211 OFF(S110C);
212 OFF(S108A);
213 OFF(S201A);
214 OFF(S110B);
1747 kakl 215  
216 r103A=STOP; // Rolety jsou zastaveny
217 r110A=STOP;
218 r104A=STOP;
219 r104B=STOP;
220  
221 enable_interrupts(INT_TIMER0);
222 enable_interrupts(GLOBAL);
1746 kakl 223 }
224  
1747 kakl 225 CREN=0; CREN=1; // Reinitialise USART
226  
1746 kakl 227 while(TRUE)
228 {
229 if(kbhit())
230 {
231 c=getc();
232 if(c=='S')
233 {
1747 kakl 234 if(getc()=='1')
1746 kakl 235 {
1747 kakl 236 switch(getc())
237 {
238 case '1':
239 output_toggle(S101A);
240 break;
241 case '2':
242 output_toggle(S102A);
243 break;
244 case '3':
245 output_toggle(S103A);
246 break;
247 case '4':
248 output_toggle(S104A);
249 break;
250 case '5':
251 c=getc();
252 if(c=='A') {output_toggle(S105A);}
253 if(c=='D') {output_toggle(S105D);}
254 break;
255 case '6':
256 output_toggle(S106A);
257 break;
258 case '7':
259 output_toggle(S107A);
260 break;
261 case '8':
262 output_toggle(S108A);
263 break;
264 case '9':
265 output_toggle(S109A);
266 break;
267 case 'a':
268 output_toggle(S110B);
269 output_toggle(S110C);
270 break;
271 }
1746 kakl 272 }
273 }
274  
275 if(c=='R')
276 {
1747 kakl 277 if(getc()=='1')
1746 kakl 278 {
1747 kakl 279 switch(getc())
280 {
281 case '3':
282 getc(); // A
283 if((getc()=='U')) {up(&r103A,&tr103A);} else {down(&r103A,&tr103A);}
284 break;
285 case '4':
286 c=getc();
287 if(c=='A') if((getc()=='U')) {up(&r104A,&tr104A);} else {down(&r104A,&tr104A);}
288 if(c=='B') if((getc()=='U')) {up(&r104B,&tr104B);} else {down(&r104B,&tr104B);}
289 break;
290 case 'a':
291 getc(); // A
292 if((getc()=='U')) {up(&r110A,&tr110A);} else {down(&r110A,&tr110A);}
293 break;
294 }
1746 kakl 295 }
296 }
1747 kakl 297  
298 output_toggle(LED1); // Prijmuli jsme cely prikaz
1746 kakl 299 }
300 else
301 {
1747 kakl 302 run(103A);
303 run(104A);
304 run(104B);
305  
306 switch(r110A)
307 {
308 case STOP:
309 OFF(R110AU);
310 OFF(R110AD);
311 break;
312 case GO_UP:
313 ON(R110AU);
314 OFF(R110AD);
315 if(tr110A==0) {r110A=STOP;}
316 break;
317 case GO_DOWN:
318 OFF(R110AU);
319 ON(R110AD);
320 if(tr110A==0) {r110A=STOP;}
321 break;
322 case REVERS_UP:
323 OFF(R110AU);
324 OFF(R110AD);
325 if(tr110A==0) {r110A=GO_UP; tr110A=GO_ZALUZIE;}
326 break;
327 case REVERS_DOWN:
328 OFF(R110AU);
329 OFF(R110AD);
330 if(tr110A==0) {r110A=GO_DOWN; tr110A=GO_ZALUZIE;}
331 break;
332 }
333 /*
334 switch(r110A)
335 {
336 case GO_UP:
337 printf("U");
338 break;
339 case GO_DOWN:
340 printf("D");
341 break;
342 case REVERS_UP:
343 printf("R");
344 break;
345 case REVERS_DOWN:
346 printf("E");
347 break;
348 case STOP:
349 printf("S");
350 break;
351 }
352 */
1746 kakl 353 restart_wdt();
354 }
355 }
356 }