1: void APP_DeviceCDCBasicDemoTasks()
2: {
3: /* If the USB device isn't configured yet, we can't really do anything
4: * else since we don't have a host to talk to. So jump back to the
5: * top of the while loop. */
6: if( USBGetDeviceState() < CONFIGURED_STATE )
7: {
8: return;
9: }
10:
11: /* If we are currently suspended, then we need to see if we need to
12: * issue a remote wakeup. In either case, we shouldn't process any
13: * keyboard commands since we aren't currently communicating to the host
14: * thus just continue back to the start of the while loop. */
15: if( USBIsDeviceSuspended()== true )
16: {
17: return;
18: }
19:
20: /* If the user has pressed the button associated with this demo, then we
21: * are going to send a "Button Pressed" message to the terminal.
22: */
23: if(BUTTON_IsPressed(BUTTON_DEVICE_CDC_BASIC_DEMO) == true)
24: {
25: /* Make sure that we only send the message once per button press and
26: * not continuously as the button is held.
27: */
28: if(buttonPressed == false)
29: {
30: /* Make sure that the CDC driver is ready for a transmission.
31: */
32: if(mUSBUSARTIsTxTrfReady() == true)
33: {
34: putrsUSBUSART(buttonMessage);
35: buttonPressed = true;
36: }
37: }
38: }
39: else
40: {
41: /* If the button is released, we can then allow a new message to be
42: * sent the next time the button is pressed.
43: */
44: buttonPressed = false;
45: }
46:
47: /* Check to see if there is a transmission in progress, if there isn't, then
48: * we can see about performing an echo response to data received.
49: */
50: if(USBUSARTIsTxTrfReady() == true)
51: {
52: Step = 3;
53: Captor = 8;
54: Seconds=16;
55:
56: uint8_t i;
57: uint8_t numBytesRead;
58:
59: numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));
60:
61: /* For every byte that was read... */
62: for(i=0; i<numBytesRead; i++)
63: {
64: switch(readBuffer[i])
65: {
66: /* If we receive new line or line feed commands, just echo
67: * them direct.
68: */
69: case 0x0A:
70: case 0x0D:
71: writeBuffer[i] = readBuffer[i];
72: break;
73:
74: /* If we receive something else, then echo it plus one
75: * so that if we receive 'a', we echo 'b' so that the
76: * user knows that it isn't the echo enabled on their
77: * terminal program.
78: */
79: default:
80:
81: Commander = readBuffer[i];
82: if(PORTBbits.RB7==1)
83: {
84: Forced_Capture();
85: }
86:
87: if(Commander == 'A')
88: {
89: Filter_Close();
90: }
91: elseif(Commander == 'B')
92: {
93: Filter_Open();
94: }
95: elseif(Commander == 'C')
96: {
97: Speed_Down_Capture(Step, Captor);
98: }
99: elseif(Commander == 'D')
100: {
101: Speed_Up_Capture(Step, Captor);
102: }
103: elseif(Commander == 'E')
104: {
105: Simple_Speed_Up();
106: }
107: elseif(Commander == 'F')
108: {
109: Speed_Up_to_Max(Step, Captor);
110: }
111: elseif(Commander == 'G')
112: {
113: Speed_Down_for_Diamond_Ring();
114: }
115: elseif(Commander == 'H')
116: {
117: Continues_Capture(Seconds);
118: }
119: elseif(Commander == 'I')
120: {
121: Capture();
122: }
123: elseif(Commander == 'X')
124: {
125: Forced_Capture();
126: }
127: }
128:
129: }
130: writeBuffer[i] = readBuffer[i] + 1;
131: }
132: elseif (USBUSARTIsTxTrfReady() == false && PORTBbits.RB7 == 0)
133: {
134: Step = 3;
135: Captor = 8;
136: Seconds=14;
137: Forced_Capture();
138: }
139: if(numBytesRead > 0)
140: {
141: /* After processing all of the received data, we need to send out
142: * the "echo" data now.
143: */
144: putUSBUSART(writeBuffer,numBytesRead);
145: }
146: CDCTxService();
147: }
148:
149:
150: int Forced_Capture(void)
151: {
152: Filter_Open();
153: Continues_Capture(Seconds);
154: Simple_Speed_Up();
155:
156: for(int r=1; r<=4; r++)
157: {
158: Speed_Down_Capture(Step, Captor);
159: Speed_Up_Capture(Step, Captor);
160: Simple_Speed_Up();
161: }
162: Speed_Down_for_Diamond_Ring();
163: Continues_Capture(Seconds);
164: Filter_Close();
165:
166: return;
167: }
168:
169: void Filter_Close(void)
170: {
171: TRISB = 0x00;
172: TRISC = 0b10000000;
173:
174: for(a=0; a<45; a++)
175: {
176: LATCbits.LATC0 = 1;
177: __delay_us(13);
178: LATCbits.LATC0 = 0;
179: for(b=1; b<=39; b++)
180: {
181: __delay_us(500);
182: }
183: }
184: LATCbits.LATC0 = 0;
185: __delay_ms(200);
186:
187:
188: for(a=0; a<45; a++)
189: {
190: LATCbits.LATC1 = 1;
191: __delay_us(13);
192: LATCbits.LATC1 = 0;
193: for(b=1; b<=39; b++)
194: {
195: __delay_us(500);
196: }
197: }
198: LATCbits.LATC1 = 0;
199: __delay_ms(200);
200:
201: for(a=0; a<45; a++)
202: {
203: LATCbits.LATC2 = 1;
204: __delay_us(13XX);
205: LATCbits.LATC2 = 0;
206: for(b=1; b<=39; b++)
207: {
208: __delay_us(500);
209: }
210: }
211: LATCbits.LATC2 = 0;
212: __delay_ms(200);
213:
214: return;
215: }
216:
217: void Filter_Open(void)
218: {
219: TRISB = 0x00;
220: TRISC = 0b10000000;
221:
222: for(k=0; k<45; k++)
223: {
224: LATCbits.LATC2 = 1;
225: for(b=1; b<=6; b++)
226: {
227: __delay_us(250);
228: }
229: LATCbits.LATC2 = 0;
230: for(b=1; b<=47; b++)
231: {
232: __delay_us(400);
233: }
234: }
235: LATCbits.LATC2 = 0;
236: __delay_ms(200);
237:
238: for(k=0; k<45; k++)
239: {
240: LATCbits.LATC1 = 1;
241: for(b=1; b<=6; b++)
242: {
243: __delay_us(250);
244: }
245: LATCbits.LATC1 = 0;
246: for(b=1; b<=47; b++)
247: {
248: __delay_us(400);
249: }
250: // LATCbits.LATC2 = 0;
251: }
252:
253: __delay_ms(200);
254:
255: for(k=0; k<45; k++)
256: {
257: LATCbits.LATC0 = 1;
258: for(b=1; b<=6; b++)
259: {
260: __delay_us(250);
261: }
262: LATCbits.LATC0 = 0;
263: for(b=1; b<=47; b++)
264: {
265: __delay_us(400);
266: }
267: }
268: //LATCbits.LATC2 = 0;
269: __delay_ms(200);
270: }
271:
272: int Speed_Up_Capture(Step, Captor) //C
273: {
274: int LongInterval;
275: int ShortInterval;
276: LATCbits.LATC6=0;
277: LATBbits.LATB5=0;
278: LATCbits.LATC5=0;
279: LATBbits.LATB4=0;
280:
281: LongInterval = Captor%3;
282: ShortInterval = Captor - LongInterval;
283:
284: for(l=1; l<LongInterval+1; l++)
285: {
286: __delay_ms(150);
287:
288: Capture();
289:
290: for(j=1; j<14; j++)
291: {
292: __delay_ms(100);
293: }
294:
295: LATCbits.LATC5=1;
296: LATBbits.LATB4=1;
297: __delay_ms(70);
298:
299: for(m=1;m<Step+1;m++)
300: {
301: LATCbits.LATC6=1;
302: LATBbits.LATB5=1;
303: __delay_ms(70);
304: LATCbits.LATC6=0;
305: LATBbits.LATB5=0;
306: __delay_ms(70);
307: }
308: LATCbits.LATC5=0;
309: LATBbits.LATB4=0;
310: __delay_ms(70);
311: }
312:
313: for(l=1; l<ShortInterval+1; l++)
314: {
315: __delay_ms(70);
316: Capture();
317: __delay_ms(70);
318:
319: LATCbits.LATC5=1;
320: LATBbits.LATB4=1;
321: __delay_ms(70);
322:
323: for(m=1;m<Step+1;m++)
324: {
325: LATCbits.LATC6=1;
326: LATBbits.LATB5=1;
327: __delay_ms(70);
328:
329: LATCbits.LATC6=0;
330: LATBbits.LATB5=0;
331: __delay_ms(70);
332: }
333:
334: LATCbits.LATC5=0;
335: LATBbits.LATB4=0;
336: for(j=1; j<6; j++)
337: {
338: __delay_ms(100);
339: }
340: }
341:
342: Capture();
343: for(j=1; j<6; j++)
344: {
345: __delay_ms(100);
346: }
347:
348: return(Step, Captor);
349: }
350:
351: int Speed_Down_Capture(Step, Captor) //D
352: {
353: int LongInterval;
354: int ShortInterval;
355:
356: LongInterval = Captor%3;
357: ShortInterval = Captor - LongInterval;
358: LATCbits.LATC6=0;
359: LATBbits.LATB5=0;
360: LATCbits.LATC5=0;
361: LATBbits.LATB4=0;
362:
363:
364: for(l=1; l<ShortInterval+1; l++)
365: {
366:
367: __delay_ms(50);
368: Capture();
369: __delay_ms(150);
370:
371: LATCbits.LATC6=1;
372: LATBbits.LATB5=1;
373: __delay_ms(70);
374:
375: for(m=1;m<Step+1;m++)
376: {
377: LATCbits.LATC5=1;
378: LATBbits.LATB4=1;
379: __delay_ms(70);
380:
381: LATCbits.LATC5=0;
382: LATBbits.LATB4=0;
383: __delay_ms(70);
384:
385: }
386: LATCbits.LATC6=0;
387: LATBbits.LATB5=0;
388: for(j=1; j<6; j++)
389: {
390: __delay_ms(100);
391: }
392: }
393:
394: for(l=1; l<LongInterval+1; l++)
395: {
396: __delay_ms(50);
397: Capture();
398: __delay_ms(100);
399:
400: LATCbits.LATC6=1;
401: LATBbits.LATB5=1;
402: __delay_ms(70);
403:
404: for(m=1;m<Step+1;m++)
405: {
406: LATCbits.LATC5=1;
407: LATBbits.LATB4=1;
408: __delay_ms(70);
409:
410: LATCbits.LATC5=0;
411: LATBbits.LATB4=0;
412: __delay_ms(70);
413: }
414:
415: LATCbits.LATC7=0;
416: LATBbits.LATB5=0;
417: __delay_ms(70);
418: for(j=1; j<13; j++)
419:
420: {
421: __delay_ms(100);
422: }
423: }
424:
425: __delay_ms(100);
426: Capture();
427: for(j=1; j<13; j++)
428: {
429: __delay_ms(100);
430: }
431:
432: return(Step, Captor);
433: }
434:
435: void Simple_Speed_Up(void) //E
436: {
437: TRISB = 0x00;
438: TRISC = 0b10000000;
439: LATCbits.LATC6=0;
440: LATBbits.LATB5=0;
441: LATCbits.LATC5=0;
442: LATBbits.LATB4=0;
443:
444: __delay_ms(50);
445:
446: LATCbits.LATC5=1;
447: LATBbits.LATB4=1;
448: __delay_ms(50);
449:
450: for(j=1;j<=4;j++)
451: {
452: LATCbits.LATC6=1;
453: LATBbits.LATB5=1;
454: __delay_ms(50);
455: LATCbits.LATC6=0;
456: LATBbits.LATB5=0;
457: __delay_ms(50);
458: }
459:
460: LATCbits.LATC5=0;
461: LATBbits.LATB4=0;
462: __delay_ms(50);
463: }
464:
465: int Speed_Up_to_Max(int Step, int Captor) //F
466: {
467: LATCbits.LATC4=0;
468: LATBbits.LATB5=0;
469: LATCbits.LATC3=0;
470: LATBbits.LATB4=0;
471:
472: __delay_ms(50);
473: LATCbits.LATC5=1;
474: LATBbits.LATB4=1;
475: __delay_ms(50);
476: for(j=1;j<(Step+1)*Captor;j++)
477: {
478: LATCbits.LATC6=1;
479: LATBbits.LATB5=1;
480: __delay_ms(50);
481: LATCbits.LATC6=0;
482: LATBbits.LATB5=0;
483: __delay_ms(50);
484: }
485: LATCbits.LATC5=0;
486: LATBbits.LATB4=0;
487: __delay_ms(50);
488: }
489:
490:
491: void Speed_Down_for_Diamond_Ring(void) //G
492: {
493: LATCbits.LATC6=0;
494: LATBbits.LATB5=0;
495: LATCbits.LATC5=0;
496: LATBbits.LATB4=0;
497:
498: LATCbits.LATC6=1;
499: LATBbits.LATB5=1;
500: __delay_ms(60);
501:
502: for(j=1;j<5;j++)
503: {
504: LATCbits.LATC5=1;
505: LATBbits.LATB4=1;
506:
507: __delay_ms(60);
508:
509: LATCbits.LATC5=0;
510: LATBbits.LATB4=0;
511:
512: __delay_ms(60);
513: }
514:
515: LATCbits.LATC6=0;
516: LATBbits.LATB5=0;
517: __delay_ms(60);
518:
519: return;
520: }
521:
522: int Continues_Capture(Seconds) //H
523: {
524: LATCbits.LATC3=0;
525: LATBbits.LATB6=0;
526: LATCbits.LATC4=0;
527: LATBbits.LATB7=0;
528:
529: __delay_ms(50);
530:
531: LATCbits.LATC3=1;
532: LATBbits.LATB6=1;
533: __delay_ms(50);
534:
535: LATCbits.LATC4=1;
536: LATBbits.LATB7=1;
537:
538: for(j=1; j<Seconds+1;j++)
539: {
540: for(k=1;k<=10; k++)
541: {
542: __delay_ms(100);
543: }
544: }
545: LATCbits.LATC4=0;
546: LATBbits.LATB7=0;
547: __delay_ms(50);
548:
549: LATCbits.LATC3=0;
550: LATBbits.LATB6=0;
551:
552: return(Seconds);
553: }
554:
555: void Capture(void) //K
556: {
557: LATCbits.LATC3=0;
558: LATBbits.LATB6=0;
559: LATCbits.LATC4=0;
560: LATBbits.LATB7=0;
561:
562: LATCbits.LATC3=1;
563: LATBbits.LATB6=1;
564:
565: __delay_ms(50);
566: LATCbits.LATC3=1;
567: LATBbits.LATB6=1;
568:
569: __delay_ms(50);
570:
571: LATCbits.LATC4=1;
572: LATBbits.LATB7=1;
573:
574: __delay_ms(50);
575:
576: LATCbits.LATC4=0;
577: LATBbits.LATB7=0;
578:
579: __delay_ms(50);
580:
581: LATCbits.LATC3=0;
582: LATBbits.LATB6=0;
583: return;
584: }