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