3020 |
jacho |
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 |
|
|
5 |
#define LED3 PIN_C4 // pulse detect |
|
|
6 |
#define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
|
|
7 |
|
|
|
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 |
|
|
14 |
|
|
|
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) |
|
|
34 |
{ |
|
|
35 |
time_overflow_count++; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
#int_TIMER1 |
|
|
39 |
void TIMER1_isr(void) |
|
|
40 |
{ |
|
|
41 |
overflow_count++; |
|
|
42 |
output_toggle(LED3); |
|
|
43 |
output_toggle(BEEP); |
|
|
44 |
} |
|
|
45 |
|
|
|
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 |
|
3020 |
jacho |
61 |
void main() |
|
|
62 |
{ |
|
|
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 |
3020 |
jacho |
67 |
unsigned int16 measurement_number=0; |
|
|
68 |
|
|
|
69 |
setup_adc_ports(sAN0|VSS_VDD); |
|
|
70 |
setup_adc(ADC_CLOCK_DIV_32); |
|
|
71 |
setup_spi(SPI_SS_DISABLED); |
|
|
72 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); |
|
|
73 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
|
|
74 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
75 |
setup_ccp1(CCP_PWM); |
|
|
76 |
setup_ccp2(CCP_PWM); |
|
|
77 |
setup_oscillator(OSC_8MHZ); |
|
|
78 |
|
|
|
79 |
set_pwm1_duty(0); |
|
|
80 |
set_pwm2_duty(0); |
|
|
81 |
|
|
|
82 |
output_high(LED1); |
|
|
83 |
output_high(LED2); |
|
|
84 |
output_high(LED3); |
|
|
85 |
output_high(LED4); |
|
|
86 |
output_low(BEEP); |
|
|
87 |
|
|
|
88 |
setup_comparator(CP2_A0_VREF|CP2_OUT_ON_A5); // sets two comparators(A1 and VR and A2 as the output) |
|
|
89 |
setup_vref(VREF_HIGH|6); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V |
|
|
90 |
enable_interrupts(INT_COMP); //enables the comparator interrupt |
|
|
91 |
enable_interrupts(INT_RTCC); |
|
|
92 |
enable_interrupts(INT_TIMER1); |
|
|
93 |
enable_interrupts(INT_TIMER2); |
|
|
94 |
enable_interrupts(GLOBAL); |
|
|
95 |
lcd_init(); |
|
|
96 |
|
|
|
97 |
lcd_putc("\fGM counter V1.0 \n 2013 MLAB"); |
|
|
98 |
printf("Geiger-Muller Counter V1.0 \r\n"); |
|
|
99 |
printf("(c) 2013 MLAB and UST.cz \r\n"); |
|
|
100 |
Delay_ms(1000); |
|
|
101 |
|
|
|
102 |
lcd_putc("\f"); |
|
|
103 |
printf("\r\n"); |
|
|
104 |
|
|
|
105 |
set_timer1(0); |
|
|
106 |
|
|
|
107 |
lcd_gotoxy(1,1); // vypsani hodnoty count, abz byla na LCD hned od zapnuti |
|
|
108 |
printf(lcd_putc,"Count:%lu ",pulse_count); |
|
|
109 |
|
|
|
110 |
while(true) |
|
|
111 |
{ |
|
|
112 |
pulse_count = get_timer1() + (0xffff * overflow_count); // compute pulse count |
|
|
113 |
time = integration_time + 1 - (time_overflow_count * 0.0327); // compute time interval |
|
|
114 |
|
|
|
115 |
if(get_timer1() != last_timer) // timer count increment detection |
|
|
116 |
{ |
|
|
117 |
output_toggle(BEEP); |
|
|
118 |
delay_ms(1); |
|
|
119 |
output_toggle(BEEP); |
|
|
120 |
output_toggle(LED3); |
|
|
121 |
last_timer=get_timer1(); |
|
|
122 |
lcd_gotoxy(1,1); // prepsani dat na displeje |
|
|
123 |
printf(lcd_putc,"Count:%lu ",pulse_count); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
lcd_gotoxy(1,2); |
|
|
127 |
printf(lcd_putc,"T:%lu ",time); |
|
|
128 |
lcd_gotoxy(9,2); |
|
|
129 |
printf(lcd_putc,"I:%lu ", integration_time); |
|
|
130 |
|
|
|
131 |
if(time == 0) // po uplynuti mereneho intervalu vypis na seriovku |
|
|
132 |
{ |
|
|
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++; |
|
|
140 |
} |
|
|
141 |
|
|
|
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; |
|
|
150 |
if(time_setup==TRUE) |
|
|
151 |
{ |
|
|
152 |
sound_beep(100,700); |
|
|
153 |
if(integration_time < 0xfffa ) integration_time+=5; |
|
|
154 |
} |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
if(!input(S2)) |
|
|
159 |
{ |
|
|
160 |
delay_ms(20); |
|
|
161 |
if(!input(S2)) |
|
|
162 |
{ |
|
|
163 |
button_press=true; |
|
|
164 |
if(time_setup==TRUE) |
|
|
165 |
{ |
|
|
166 |
sound_beep(100,600); |
|
|
167 |
if(integration_time > 0x0005 ) integration_time-=5; |
|
|
168 |
|
|
|
169 |
set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka |
|
|
170 |
time_overflow_count=0; |
|
|
171 |
set_timer1(0); |
|
|
172 |
overflow_count=0; |
|
|
173 |
} |
|
|
174 |
} |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
if(!input(S3)) |
|
|
178 |
{ |
|
|
179 |
delay_ms(20); |
|
|
180 |
if(!input(S3)) |
|
|
181 |
{ |
|
|
182 |
button_press=true; |
|
|
183 |
if(time_setup==TRUE) |
|
|
184 |
{ |
|
|
185 |
sound_beep(100,500); |
|
|
186 |
if(integration_time < 0xffff ) integration_time++; |
|
|
187 |
} |
|
|
188 |
} |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
if(!input(S4)) |
|
|
192 |
{ |
|
|
193 |
delay_ms(20); |
|
|
194 |
if(!input(S4)) |
|
|
195 |
{ |
|
|
196 |
button_press=true; |
3021 |
kaklik |
197 |
switch (setup_mode) |
3020 |
jacho |
198 |
{ |
3021 |
kaklik |
199 |
case INTERVAL_setup: |
|
|
200 |
|
|
|
201 |
sound_beep(100,400); |
|
|
202 |
if(integration_time > 0x0001 ) integration_time--; |
3020 |
jacho |
203 |
|
3021 |
kaklik |
204 |
set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka |
|
|
205 |
time_overflow_count=0; |
|
|
206 |
set_timer1(0); |
|
|
207 |
overflow_count=0; |
|
|
208 |
break; |
3020 |
jacho |
209 |
} |
|
|
210 |
} |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
if(!input(S5)) // prepnuti na nastavovani casu |
|
|
214 |
{ |
|
|
215 |
delay_ms(20); |
|
|
216 |
if(!input(S5)) |
|
|
217 |
{ |
|
|
218 |
button_press=true; |
|
|
219 |
sound_beep(100,800); |
3021 |
kaklik |
220 |
setup_mode=INTERVAL_setup; |
3020 |
jacho |
221 |
} |
|
|
222 |
} |
|
|
223 |
|
|
|
224 |
if(!input(S6)) // prepnuti na nastavovani napeti |
|
|
225 |
{ |
|
|
226 |
delay_ms(20); |
|
|
227 |
if(!input(S6)) |
|
|
228 |
{ |
|
|
229 |
button_press=true; |
|
|
230 |
sound_beep(100,800); |
3021 |
kaklik |
231 |
setup_mode=TRESHOLD_setup; |
3020 |
jacho |
232 |
} |
|
|
233 |
} |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
if ( input(S1) && input(S2) && input(S3) && input(S4) && input(S5) && input(S6)) // detekce pustenych tlacitek |
|
|
237 |
{ |
|
|
238 |
button_press=false; |
|
|
239 |
} |
|
|
240 |
} |
|
|
241 |
} |