Rev 1423 Rev 1424
1 #include "main.h" 1 #include "main.h"
2   2  
3 unsigned int position; 3 unsigned int position;
4 unsigned int32 section[4]; 4 unsigned int32 section[4];
5 unsigned int32 time; // pocitadlo preteceni casovace 5 unsigned int32 time; // pocitadlo preteceni casovace
6 unsigned int state; 6 unsigned int state;
7   7  
8 #define RUNNING 1 8 #define RUNNING 1
9 #define STOPPED 0 9 #define STOPPED 0
10   10  
11 #int_RB 11 #int_RB
12 void RB_isr(void) 12 void RB_isr(void)
13 { 13 {
14 section[position]= get_timer0(); 14 section[position]= get_timer0();
15 time=0; 15 time=0;
16 set_timer0(0); 16 set_timer0(0);
17 if(position < 3) position++; 17 if(position < 3) position++;
18 else { 18 else {
19 disable_interrupts(INT_RB); 19 disable_interrupts(INT_RB);
20 state = STOPPED; 20 state = STOPPED;
21 } 21 }
22 22
23 time=0; 23 time=0;
24 set_timer0(0); 24 set_timer0(0);
25   25  
26 } 26 }
27   27  
28 #int_RTCC 28 #int_RTCC
29 void RTCC_isr(void) // preruseni od pretekleho casovace 29 void RTCC_isr(void) // preruseni od pretekleho casovace
30 { 30 {
31 time++; 31 time++;
32 } 32 }
33   33  
34 #define LCD_ENABLE_PIN PIN_E0 34 #define LCD_ENABLE_PIN PIN_E0
35 #define LCD_RS_PIN PIN_E1 35 #define LCD_RS_PIN PIN_E1
36 #define LCD_RW_PIN PIN_E2 36 #define LCD_RW_PIN PIN_E2
37 #define LCD_DATA4 PIN_D4 37 #define LCD_DATA4 PIN_D4
38 #define LCD_DATA5 PIN_D5 38 #define LCD_DATA5 PIN_D5
39 #define LCD_DATA6 PIN_D6 39 #define LCD_DATA6 PIN_D6
40 #define LCD_DATA7 PIN_D7 40 #define LCD_DATA7 PIN_D7
41   41  
42 #include <lcd.c> 42 #include <lcd.c>
43   43  
44 #define BUTTON1 PIN_D0 44 #define BUTTON1 PIN_D0
45 #define BUTTON2 PIN_D1 45 #define BUTTON2 PIN_D1
46 #define BUTTON3 PIN_D2 46 #define BUTTON3 PIN_D2
47 #define BUTTON4 PIN_D3 47 #define BUTTON4 PIN_D3
48   48  
49 #define BEEPER PIN_C0 49 #define BEEPER PIN_C0
50   50  
51 unsigned int32 run; 51 unsigned int32 run;
52   52  
53 // Includes all USB code and interrupts, as well as the CDC API 53 // Includes all USB code and interrupts, as well as the CDC API
54 #include <usb_cdc.h> 54 #include <usb_cdc.h>
55   55  
56 void beep(int16 period) 56 void beep(int16 period)
57 { 57 {
58 while(period){ 58 while(period){
59 output_toggle(BEEPER); 59 output_toggle(BEEPER);
60 delay_us(800); 60 delay_us(800);
61 period--; 61 period--;
62 } 62 }
63 } 63 }
64   64  
65 void test_mode() 65 void test_mode()
66 { 66 {
67 lcd_gotoxy(1,1); 67 lcd_gotoxy(1,1);
68 printf(lcd_putc,"TEST MODE"); 68 printf(lcd_putc,"\f TEST MODE");
69 } 69 }
70   70  
71 void main() { 71 void main() {
72   72  
73 setup_adc_ports(NO_ANALOGS|VSS_VDD); 73 setup_adc_ports(NO_ANALOGS|VSS_VDD);
74 port_b_pullups(TRUE); 74 port_b_pullups(TRUE);
75 setup_adc(ADC_OFF); 75 setup_adc(ADC_OFF);
76 setup_psp(PSP_DISABLED); 76 setup_psp(PSP_DISABLED);
77 setup_spi(SPI_SS_DISABLED); 77 setup_spi(SPI_SS_DISABLED);
78 setup_wdt(WDT_OFF); 78 setup_wdt(WDT_OFF);
79 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16); 79 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
80 setup_timer_1(T1_DISABLED); 80 setup_timer_1(T1_DISABLED);
81 setup_timer_2(T2_DISABLED,0,1); 81 setup_timer_2(T2_DISABLED,0,1);
82 setup_timer_3(T3_DISABLED|T3_DIV_BY_1); 82 setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
83 setup_comparator(NC_NC_NC_NC); 83 setup_comparator(NC_NC_NC_NC);
84 setup_vref(FALSE); 84 setup_vref(FALSE);
85 enable_interrupts(GLOBAL); 85 enable_interrupts(GLOBAL);
86   86  
87 lcd_init(); 87 lcd_init();
88 usb_init(); 88 usb_init();
89   89  
90 lcd_gotoxy(1,1); 90 lcd_gotoxy(1,1);
91 printf(lcd_putc,"STOPWATCH01A"); 91 printf(lcd_putc,"\f STOPWATCH01A");
92 lcd_gotoxy(1,2); 92 lcd_gotoxy(1,2);
93 printf(lcd_putc," Kaklik 2010 "); 93 printf(lcd_putc," Kaklik 2010 ");
94 delay_ms(1000); 94 delay_ms(1000);
95 lcd_gotoxy(1,1); 95 lcd_gotoxy(1,1);
96 printf(lcd_putc,"\n Waiting for PC... "); 96 printf(lcd_putc,"\f Waiting for PC... ");
97   97  
98 while(!usb_cdc_connected()); 98 while(!usb_cdc_connected());
99 lcd_gotoxy(1,1); 99 lcd_gotoxy(1,1);
100 printf(lcd_putc,"PC connected..."); 100 printf(lcd_putc,"\f PC connected...");
101 beep(10); 101 beep(10);
102 102
103 if(!input(BUTTON1)) test_mode(); 103 if(!input(BUTTON1)) test_mode();
104 run=0; 104 run=0;
105 105
106 section[0]=0; 106 section[0]=0;
107 section[1]=0; 107 section[1]=0;
108 section[2]=0; 108 section[2]=0;
109 section[3]=0; 109 section[3]=0;
110 110
111 111
112 while (TRUE) 112 while (TRUE)
113 { 113 {
114 run++; 114 run++;
115 lcd_gotoxy(3,1); 115 lcd_gotoxy(3,1);
116 printf(lcd_putc,"START"); 116 printf(lcd_putc,"\f START");
117   117  
118 while(input(BUTTON2)); 118 while(input(BUTTON2));
119 time = 0; 119 time = 0;
120 set_timer0(0); 120 set_timer0(0);
121 position = 0; 121 position = 0;
122 state = RUNNING; 122 state = RUNNING;
123 clear_interrupt(int_timer0); 123 clear_interrupt(int_timer0);
124 clear_interrupt(int_RB); 124 clear_interrupt(int_RB);
125 enable_interrupts(INT_RTCC); 125 enable_interrupts(INT_RTCC);
126 enable_interrupts(INT_RB); 126 enable_interrupts(INT_RB);
127 127
128 lcd_gotoxy(6,1); 128 lcd_gotoxy(6,1);
129 printf(lcd_putc,"STOP"); 129 printf(lcd_putc,"\f STOP");
130 while(state==RUNNING); 130 while(state==RUNNING);
131   131  
132   132  
133 printf(usb_cdc_putc, "\n\r %ld %ld %ld %ld %ld", run, section[0], section[1], section[2], section[3]); 133 printf(usb_cdc_putc, "\n\r %ld %ld %ld %ld %ld", run, section[0], section[1], section[2], section[3]);
134 } 134 }
135 } 135 }