Rev 1439 Rev 1446
1 #include "main.h" 1 #include "main.h"
2   2  
3 unsigned int position; -  
4 unsigned int32 section[4]; 3 unsigned int32 section[4];
5 unsigned int32 time; // pocitadlo preteceni casovace 4 unsigned int32 time; // pocitadlo preteceni casovace
6   5  
7 #Define INORDER 1 6 #Define INORDER 1
8   7  
9 #int_RTCC 8 #int_RTCC
10 void RTCC_isr(void) // preruseni od pretekleho casovace 9 void RTCC_isr(void) // preruseni od pretekleho casovace
11 { 10 {
12 time++; 11 time++;
13 } 12 }
14   13  
15 #define LCD_ENABLE_PIN PIN_E0 14 #define LCD_ENABLE_PIN PIN_E0
16 #define LCD_RS_PIN PIN_E1 15 #define LCD_RS_PIN PIN_E1
17 #define LCD_RW_PIN PIN_E2 16 #define LCD_RW_PIN PIN_E2
18 #define LCD_DATA4 PIN_D4 17 #define LCD_DATA4 PIN_D4
19 #define LCD_DATA5 PIN_D5 18 #define LCD_DATA5 PIN_D5
20 #define LCD_DATA6 PIN_D6 19 #define LCD_DATA6 PIN_D6
21 #define LCD_DATA7 PIN_D7 20 #define LCD_DATA7 PIN_D7
22   21  
23 #include <lcd.c> 22 #include <lcd.c>
24   23  
25 #define BUTTON1 PIN_D0 24 #define BUTTON1 PIN_D0
26 #define BUTTON2 PIN_D1 25 #define BUTTON2 PIN_D1
27 #define BUTTON3 PIN_D2 26 #define BUTTON3 PIN_D2
28 #define BUTTON4 PIN_D3 27 #define BUTTON4 PIN_D3
29   28  
-   29 #define START1 PIN_B0
-   30 #define STOP1 PIN_B1
-   31 #define START2 PIN_B2
-   32 #define STOP2 PIN_B3
-   33 #define START3 PIN_B4
-   34 #define STOP3 PIN_B5
-   35 #define START4 PIN_B6
-   36 #define STOP4 PIN_B7
-   37  
30 #define BEEPER PIN_C0 38 #define BEEPER PIN_C0
31   39  
32 unsigned int32 run; 40 unsigned int32 run;
33   41  
34 // Includes all USB code and interrupts, as well as the CDC API 42 // Includes all USB code and interrupts, as well as the CDC API
35 #include <usb_cdc.h> 43 #include <usb_cdc.h>
36   44  
37 void beep(int16 period) 45 void beep(int16 period)
38 { 46 {
39 while(period){ 47 while(period){
40 output_toggle(BEEPER); 48 output_toggle(BEEPER);
41 delay_us(800); 49 delay_us(800);
42 period--; 50 period--;
43 } 51 }
44 } 52 }
45   53  
46 void test_mode() //Testovaci mod stopek 54 void test_mode() //Testovaci mod stopek
47 { 55 {
48 printf(lcd_putc,"\f TEST MODE"); 56 printf(lcd_putc,"\f TEST MODE");
49 lcd_gotoxy(1,2); 57 lcd_gotoxy(1,2);
50 printf(lcd_putc,"ESC"); 58 printf(lcd_putc,"ESC");
51 while(input(BUTTON1)) // testuje dokud nekdo nezmackne tlacitlo 59 while(input(BUTTON1)) // testuje dokud nekdo nezmackne tlacitlo
52 { 60 {
53 lcd_gotoxy(5,2); 61 lcd_gotoxy(5,2);
54 Output_toggle(PIN_A0); 62 Output_toggle(PIN_A0);
55 delay_ms(100); 63 delay_ms(100);
56 printf(lcd_putc,"%d %d %d %d",bit_test(input_b(),0), bit_test(input_b(),1), bit_test(input_b(),2), bit_test(input_b(),3)); 64 printf(lcd_putc,"%x",input_b());
57 } 65 }
58 } 66 }
59 void send_measuring() 67 void send_measuring()
60 { 68 {
61 printf(usb_cdc_putc, "\n\r %5u %5u %5u %5u %5u", run, section[0], section[1], section[2], section[3]); 69 printf(usb_cdc_putc, "\n\r %5u %5u %5u %5u %5u", run, section[0], section[1], section[2], section[3]);
62 } //*0.000010667 70 } //*0.000010667
63   71  
64 void main() 72 void main()
65 { 73 {
66 unsigned int8 last, input; -  
67 unsigned int16 timer; -  
68 int exception; -  
69   -  
70 setup_adc_ports(NO_ANALOGS|VSS_VDD); 74 setup_adc_ports(NO_ANALOGS|VSS_VDD);
71 port_b_pullups(TRUE); 75 port_b_pullups(TRUE);
72 setup_adc(ADC_OFF); 76 setup_adc(ADC_OFF);
73 setup_psp(PSP_DISABLED); 77 setup_psp(PSP_DISABLED);
74 setup_spi(SPI_SS_DISABLED); 78 setup_spi(SPI_SS_DISABLED);
75 setup_wdt(WDT_OFF); 79 setup_wdt(WDT_OFF);
76 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); 80 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
77 setup_timer_1(T1_DISABLED); 81 setup_timer_1(T1_DISABLED);
78 setup_timer_2(T2_DISABLED,0,1); 82 setup_timer_2(T2_DISABLED,0,1);
79 setup_timer_3(T3_DISABLED|T3_DIV_BY_1); 83 setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
80 setup_comparator(NC_NC_NC_NC); 84 setup_comparator(NC_NC_NC_NC);
81 setup_vref(FALSE); 85 setup_vref(FALSE);
82 enable_interrupts(INT_TIMER0); 86 enable_interrupts(INT_TIMER0);
83 enable_interrupts(GLOBAL); 87 enable_interrupts(GLOBAL);
84   88  
85 lcd_init(); 89 lcd_init();
86 usb_init(); 90 usb_init();
87   91  
88 lcd_gotoxy(1,1); 92 lcd_gotoxy(1,1);
89 printf(lcd_putc,"\f STOPWATCH01A"); 93 printf(lcd_putc,"\f STOPWATCH01A");
90 lcd_gotoxy(1,2); 94 lcd_gotoxy(1,2);
91 printf(lcd_putc," Kaklik 2010"); 95 printf(lcd_putc," Kaklik 2010");
92 delay_ms(1000); 96 delay_ms(1000);
93 lcd_gotoxy(1,1); 97 lcd_gotoxy(1,1);
94 printf(lcd_putc,"\f Waiting for PC... "); 98 printf(lcd_putc,"\f Waiting for PC... ");
95 lcd_gotoxy(1,2); 99 lcd_gotoxy(1,2);
96 printf(lcd_putc,"TEST"); 100 printf(lcd_putc,"TEST");
97   101  
98 while(!usb_cdc_connected()) if(!input(BUTTON1)) test_mode(); 102 while(!usb_cdc_connected()) if(!input(BUTTON1)) test_mode(); // pockej nez se pripoji seriovy port PC
99 103
100 lcd_gotoxy(1,1); 104 lcd_gotoxy(1,1);
101 printf(lcd_putc,"\f PC connected..."); 105 printf(lcd_putc,"\f PC connected...");
102 beep(10); 106 beep(10);
103 107
104 run=0; 108 run=0;
105 while (TRUE) 109 while (TRUE)
106 { 110 {
107 run++; 111 run++;
108 printf(lcd_putc,"\f\nSTART"); 112 printf(lcd_putc,"\f\nSTART");
109 section[0]=0; -  
110 section[1]=0; -  
111 section[2]=0; 113
112 section[3]=0; 114 while(input(BUTTON1)); // ceka na zmacknuti startovaciho tlacitka
113 last!=input_b(); 115 printf(lcd_putc,"\f\n STOP"); //
114   116  
115 while(input(BUTTON1)); 117 while((!input(START1)) && (input(BUTTON2))); // cekame na tlacitko stop nebo na preruseni paprsku
116 printf(lcd_putc,"\f\n STOP"); -  
117 time = 0; 118 time = 0;
118 set_timer0(0); 119 set_timer0(0);
119 position = 0; -  
120 exception = 0; -  
121   -  
122 while((position < 4) && (input(BUTTON2))) // cekame na tlacitko stop nebo na preruseni vsech paprsku -  
123 { -  
124 if(last!=(input=input_b())) // pri preruseni nektereho z paprsku zjistime ktery to byl -  
125 { -  
126 timer=get_timer0(); -  
127 if(bit_test(last ^ input, position))section[position] = (time << 16) + timer; // kontroluje, jestli jsou paprsky preruseny ve spravnem poradi. -  
128 else exception=INORDER; -  
129 time=0; -  
130 set_timer0(0); -  
131 position++; -  
132 } -  
133 last=input_b(); -  
134 } -  
135 if (exception == INORDER) -  
136 { -  
137 printf(lcd_putc,"\fInorder! Send?"); -  
138 lcd_gotoxy(1,2); -  
139 printf(lcd_putc,"YES NO"); -  
140 While(input(BUTTON1) && input(BUTTON4)); 120 while((input(STOP1)) && (input(BUTTON2)))
141 if(!input(BUTTON1)) send_measuring(); 121 section[0] = (time << 16) + get_timer0();
142 if(!input(BUTTON4)) continue; -  
143 } 122
144 else send_measuring(); 123 send_measuring();
145 } 124 }
146 } 125 }