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