Rev 3338 Rev 3342
1 #include ".\main.h" 1 #include ".\main.h"
2   2  
3 #define LED1 PIN_C6 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5 3 #define LED1 PIN_C6 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
4 #define LED2 PIN_C5 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5 4 #define LED2 PIN_C5 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
5 #define LED3 PIN_C4 // pulse detect 5 #define LED3 PIN_C4 // pulse detect
6 #define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER 6 #define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
7   7  
8 #define S1 PIN_C2 // raw up 8 #define S1 PIN_C2 // raw up
9 #define S2 PIN_C3 // raw down 9 #define S2 PIN_C3 // raw down
10 #define S3 PIN_D0 // fine up 10 #define S3 PIN_D0 // fine up
11 #define S4 PIN_D1 // fine down 11 #define S4 PIN_D1 // fine down
12 #define S5 PIN_B1 // time setup 12 #define S5 PIN_B1 // time setup
13 #define S6 PIN_B2 // treshold setup 13 #define S6 PIN_B2 // treshold setup
14   14  
15 #define BEEP PIN_D2 //piezo beeper 15 #define BEEP PIN_D2 //piezo beeper
16   16  
17 #define LCD_ENABLE_PIN PIN_E0 //// 17 #define LCD_ENABLE_PIN PIN_E0 ////
18 #define LCD_RS_PIN PIN_E1 //// 18 #define LCD_RS_PIN PIN_E1 ////
19 #define LCD_RW_PIN PIN_E2 //// 19 #define LCD_RW_PIN PIN_E2 ////
20 #define LCD_DATA4 PIN_D4 //// 20 #define LCD_DATA4 PIN_D4 ////
21 #define LCD_DATA5 PIN_D5 //// 21 #define LCD_DATA5 PIN_D5 ////
22 #define LCD_DATA6 PIN_D6 //// 22 #define LCD_DATA6 PIN_D6 ////
23 #define LCD_DATA7 PIN_D7 23 #define LCD_DATA7 PIN_D7
24 #include <lcd.c> 24 #include <lcd.c>
25   25  
26   26  
27 unsigned int32 pulse_count=0; 27 unsigned int32 pulse_count=0;
28 unsigned int16 overflow_count=0; 28 unsigned int16 overflow_count=0;
29 unsigned int16 time_overflow_count=0; 29 unsigned int16 time_overflow_count=0;
30   30  
31   31  
32 #int_RTCC 32 #int_RTCC
33 void RTCC_isr(void) 33 void RTCC_isr(void)
34 { 34 {
35 time_overflow_count++; 35 time_overflow_count++;
36 } 36 }
37   37  
38 #int_TIMER1 38 #int_TIMER1
39 void TIMER1_isr(void) 39 void TIMER1_isr(void)
40 { 40 {
41 overflow_count++; 41 overflow_count++;
42 output_toggle(LED3); 42 output_toggle(LED3);
43 output_toggle(BEEP); 43 output_toggle(BEEP);
44 } 44 }
45   45  
46 void sound_beep( unsigned int lenght, int16 frequency) 46 void sound_beep( unsigned int lenght, int16 frequency)
47 { 47 {
48 unsigned int i; 48 unsigned int i;
49 49
50 for(i=0;i<=lenght;i++) 50 for(i=0;i<=lenght;i++)
51 { 51 {
52 output_toggle(BEEP); 52 output_toggle(BEEP);
53 delay_us(1/frequency); 53 delay_us(1/frequency);
54 } 54 }
55 } 55 }
56   56  
57 #define TRESHOLD_setup 1 57 #define TRESHOLD_setup 1
58 #define INTERVAL_setup 0 58 #define INTERVAL_setup 0
59   59  
60   60  
61 void main() 61 void main()
62 { 62 {
63 unsigned int16 integration_time=60; 63 unsigned int16 integration_time=60;
64 unsigned int16 time; 64 unsigned int16 time;
65 unsigned int16 last_timer; // promena pro praskani 65 unsigned int16 last_timer; // promena pro praskani
66 unsigned int1 button_press,setup_mode=INTERVAL_setup; // semafor pro cteni tlacitek 66 unsigned int1 button_press,setup_mode=INTERVAL_setup; // semafor pro cteni tlacitek
67 unsigned int16 measurement_number=0; 67 unsigned int16 measurement_number=0;
68 unsigned int8 treshold=6; 68 unsigned int8 treshold=6;
69   69  
70 setup_adc_ports(sAN0|VSS_VDD); 70 setup_adc_ports(sAN0|VSS_VDD);
71 setup_adc(ADC_CLOCK_DIV_32); 71 setup_adc(ADC_CLOCK_DIV_32);
72 setup_spi(SPI_SS_DISABLED); 72 setup_spi(SPI_SS_DISABLED);
73 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); 73 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
74 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); 74 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
75 setup_timer_2(T2_DISABLED,0,1); 75 setup_timer_2(T2_DISABLED,0,1);
76 setup_ccp1(CCP_PWM); 76 setup_ccp1(CCP_PWM);
77 setup_ccp2(CCP_PWM); 77 setup_ccp2(CCP_PWM);
78 setup_oscillator(OSC_8MHZ); 78 setup_oscillator(OSC_8MHZ);
79   79  
80 set_pwm1_duty(0); 80 set_pwm1_duty(0);
81 set_pwm2_duty(0); 81 set_pwm2_duty(0);
82   82  
83 output_high(LED1); 83 output_high(LED1);
84 output_high(LED2); 84 output_high(LED2);
85 output_high(LED3); 85 output_high(LED3);
86 output_high(LED4); 86 output_high(LED4);
87 output_low(BEEP); 87 output_low(BEEP);
88   88  
89 setup_comparator(CP2_A0_VREF|CP2_OUT_ON_A5); // sets two comparators(A1 and VR and A2 as the output) 89 setup_comparator(CP2_A0_VREF|CP2_OUT_ON_A5); // sets two comparators(A1 and VR and A2 as the output)
90 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V 90 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
91 enable_interrupts(INT_COMP); //enables the comparator interrupt 91 enable_interrupts(INT_COMP); //enables the comparator interrupt
92 enable_interrupts(INT_RTCC); 92 enable_interrupts(INT_RTCC);
93 enable_interrupts(INT_TIMER1); 93 enable_interrupts(INT_TIMER1);
94 enable_interrupts(INT_TIMER2); 94 enable_interrupts(INT_TIMER2);
95 enable_interrupts(GLOBAL); 95 enable_interrupts(GLOBAL);
96 lcd_init(); 96 lcd_init();
97   97  
98 lcd_putc("\fGM counter V1.0 \n 2013 MLAB"); 98 lcd_putc("\fGM counter V1.0 \n 2013 MLAB");
99 printf("Geiger-Muller Counter V1.0 \r\n"); 99 printf("Geiger-Muller Counter V1.0 \r\n");
100 printf("(c) 2013 MLAB and UST.cz \r\n"); 100 printf("(c) 2013 MLAB and UST.cz \r\n");
101 Delay_ms(1000); 101 Delay_ms(1000);
102 102
103 lcd_putc("\f"); 103 lcd_putc("\f");
104 printf("\r\n"); 104 printf("\r\n");
105 105
106 set_timer1(0); 106 set_timer1(0);
107   107  
108 lcd_gotoxy(1,1); // vypsani hodnoty count, abz byla na LCD hned od zapnuti 108 lcd_gotoxy(1,1); // vypsani hodnoty count, abz byla na LCD hned od zapnuti
109 printf(lcd_putc,"Count:%lu ",pulse_count); 109 printf(lcd_putc,"Count:%lu ",pulse_count);
110   110  
111 while(true) 111 while(true)
112 { 112 {
113 pulse_count = get_timer1() + (0xffff * overflow_count); // compute pulse count 113 pulse_count = get_timer1() + (0xffff * overflow_count); // compute pulse count
114 time = integration_time + 1 - (time_overflow_count * 0.0327); // compute time interval 114 time = integration_time + 1 - (time_overflow_count * 0.0327); // compute time interval
115 115
116 if(get_timer1() != last_timer) // timer count increment detection 116 if(get_timer1() != last_timer) // timer count increment detection
117 { 117 {
118 output_toggle(BEEP); 118 output_toggle(BEEP);
119 delay_ms(1); 119 delay_ms(1);
120 output_toggle(BEEP); 120 output_toggle(BEEP);
121 output_toggle(LED3); 121 output_toggle(LED3);
122 last_timer=get_timer1(); 122 last_timer=get_timer1();
123 lcd_gotoxy(1,1); // prepsani dat na displeje 123 lcd_gotoxy(1,1); // prepsani dat na displeje
124 printf(lcd_putc,"Count:%lu ",pulse_count); 124 printf(lcd_putc,"Count:%lu ",pulse_count);
125 } 125 }
126 126
127 lcd_gotoxy(1,2); 127 lcd_gotoxy(1,2);
128 printf(lcd_putc,"T:%3lu I:%3lu U:%2u ",time,integration_time,treshold); 128 printf(lcd_putc,"T:%3lu I:%3lu U:%2u ",time,integration_time,treshold);
129 // printf(lcd_putc,"I:%4lu ", integration_time); 129 // printf(lcd_putc,"I:%4lu ", integration_time);
130   130  
131 if(time == 0) // po uplynuti mereneho intervalu vypis na seriovku 131 if(time == 0) // po uplynuti mereneho intervalu vypis na seriovku
132 { 132 {
133 set_timer0(0); 133 set_timer0(0);
134 time_overflow_count=0; 134 time_overflow_count=0;
135 sound_beep(1000,700); 135 sound_beep(1000,700);
136 printf("$GMC1.0 %lu %lu %lu \r\n", measurement_number, integration_time, pulse_count); 136 printf("$GMC1.0 %lu %lu %lu \r\n", measurement_number, integration_time, pulse_count);
137 set_timer1(0); 137 set_timer1(0);
138 overflow_count=0; 138 overflow_count=0;
139 measurement_number++; 139 measurement_number++;
140 } 140 }
141   141  
142 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. 142 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
143 { 143 {
144 if(!input(S1)) 144 if(!input(S1))
145 { 145 {
146 delay_ms(20); 146 delay_ms(20);
147 if(!input(S1)) 147 if(!input(S1))
148 { 148 {
149 button_press=true; 149 button_press=true;
150 sound_beep(100,700); 150 sound_beep(100,700);
151 switch (setup_mode) 151 switch (setup_mode)
152 { 152 {
153 case INTERVAL_setup: 153 case INTERVAL_setup:
154 if(integration_time < 0xfffa ) integration_time+=5; 154 if(integration_time < 0xfffa ) integration_time+=5;
155 break; 155 break;
156 156
157 case TRESHOLD_setup: 157 case TRESHOLD_setup:
158 if(treshold < 0x0a ) treshold +=5; 158 if(treshold < 0x0a ) treshold +=5;
159 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V 159 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
160 break; 160 break;
161 } 161 }
162 } 162 }
163 } 163 }
164   164  
165 if(!input(S2)) 165 if(!input(S2))
166 { 166 {
167 delay_ms(20); 167 delay_ms(20);
168 if(!input(S2)) 168 if(!input(S2))
169 { 169 {
170 button_press=true; 170 button_press=true;
171 sound_beep(100,600); 171 sound_beep(100,600);
172 switch (setup_mode) 172 switch (setup_mode)
173 { 173 {
174 case INTERVAL_setup: 174 case INTERVAL_setup:
175 if(integration_time > 0x0005 ) integration_time-=5; 175 if(integration_time > 0x0005 ) integration_time-=5;
176 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka 176 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
177 time_overflow_count=0; 177 time_overflow_count=0;
178 set_timer1(0); 178 set_timer1(0);
179 overflow_count=0; 179 overflow_count=0;
180 break; 180 break;
181 case TRESHOLD_setup: 181 case TRESHOLD_setup:
182 if(treshold > 0x05 ) treshold +=5; 182 if(treshold > 0x05 ) treshold -=5;
183 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V 183 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
184 break; 184 break;
185 } 185 }
186 } 186 }
187 } 187 }
188   188  
189 if(!input(S3)) 189 if(!input(S3))
190 { 190 {
191 delay_ms(20); 191 delay_ms(20);
192 if(!input(S3)) 192 if(!input(S3))
193 { 193 {
194 button_press=true; 194 button_press=true;
195 sound_beep(100,500); 195 sound_beep(100,500);
196 switch (setup_mode) 196 switch (setup_mode)
197 { 197 {
198 case INTERVAL_setup: 198 case INTERVAL_setup:
199 if(integration_time < 0xffff ) integration_time++; 199 if(integration_time < 0xffff ) integration_time++;
200 break; 200 break;
201   201  
202 case TRESHOLD_setup: 202 case TRESHOLD_setup:
203 if(treshold < 0x0F ) treshold ++; 203 if(treshold < 0x0F ) treshold ++;
204 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V 204 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
205 break; 205 break;
206 } 206 }
207 } 207 }
208 } 208 }
209   209  
210 if(!input(S4)) 210 if(!input(S4))
211 { 211 {
212 delay_ms(20); 212 delay_ms(20);
213 if(!input(S4)) 213 if(!input(S4))
214 { 214 {
215 button_press=true; 215 button_press=true;
216 sound_beep(100,400); 216 sound_beep(100,400);
217 switch (setup_mode) 217 switch (setup_mode)
218 { 218 {
219 case INTERVAL_setup: 219 case INTERVAL_setup:
220 220
221 if(integration_time > 0x0001 ) integration_time--; 221 if(integration_time > 0x0001 ) integration_time--;
222   222  
223 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka 223 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
224 time_overflow_count=0; 224 time_overflow_count=0;
225 set_timer1(0); 225 set_timer1(0);
226 overflow_count=0; 226 overflow_count=0;
227 break; 227 break;
228 case TRESHOLD_setup: 228 case TRESHOLD_setup:
229 if(treshold > 0x01 ) treshold--; 229 if(treshold > 0x01 ) treshold--;
230 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V 230 setup_vref(VREF_HIGH|treshold); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
231 break; 231 break;
232 } 232 }
233 } 233 }
234 } 234 }
235 235
236 if(!input(S5)) // prepnuti na nastavovani casu 236 if(!input(S5)) // prepnuti na nastavovani casu
237 { 237 {
238 delay_ms(20); 238 delay_ms(20);
239 if(!input(S5)) 239 if(!input(S5))
240 { 240 {
241 button_press=true; 241 button_press=true;
242 sound_beep(100,800); 242 sound_beep(100,800);
243 setup_mode=INTERVAL_setup; 243 setup_mode=INTERVAL_setup;
244 lcd_gotoxy(16,1); // prepsani dat na displeje 244 lcd_gotoxy(16,1); // prepsani dat na displeje
245 printf(lcd_putc,"I"); 245 printf(lcd_putc,"I");
246 } 246 }
247 } 247 }
248 248
249 if(!input(S6)) // prepnuti na nastavovani napeti 249 if(!input(S6)) // prepnuti na nastavovani napeti
250 { 250 {
251 delay_ms(20); 251 delay_ms(20);
252 if(!input(S6)) 252 if(!input(S6))
253 { 253 {
254 button_press=true; 254 button_press=true;
255 sound_beep(100,800); 255 sound_beep(100,800);
256 setup_mode=TRESHOLD_setup; 256 setup_mode=TRESHOLD_setup;
257 lcd_gotoxy(16,1); // prepsani dat na displeje 257 lcd_gotoxy(16,1); // prepsani dat na displeje
258 printf(lcd_putc,"U"); 258 printf(lcd_putc,"U");
259 } 259 }
260 } 260 }
261 } 261 }
262 262
263 if ( input(S1) && input(S2) && input(S3) && input(S4) && input(S5) && input(S6)) // detekce pustenych tlacitek 263 if ( input(S1) && input(S2) && input(S3) && input(S4) && input(S5) && input(S6)) // detekce pustenych tlacitek
264 { 264 {
265 button_press=false; 265 button_press=false;
266 } 266 }
267 } 267 }
268 } 268 }