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
2744 kaklik 5 #define LED3 PIN_C4 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
6 #define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
2742 kaklik 7  
2744 kaklik 8 #define S1 PIN_C2 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
9 #define S2 PIN_C3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
10 #define S3 PIN_D0 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
11 #define S4 PIN_D1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
2742 kaklik 12  
2744 kaklik 13 #define BEEP PIN_D2 //piezo beeper
14  
15 #define LCD_ENABLE_PIN PIN_E0 ////
16 #define LCD_RS_PIN PIN_E1 ////
17 #define LCD_RW_PIN PIN_E2 ////
18 #define LCD_DATA4 PIN_D4 ////
19 #define LCD_DATA5 PIN_D5 ////
20 #define LCD_DATA6 PIN_D6 ////
21 #define LCD_DATA7 PIN_D7
22 #include <lcd.c>
23  
24  
25 unsigned int32 pulse_count=0;
26 unsigned int16 overflow_count=0;
27 unsigned int16 time_overflow_count=0;
28  
29  
30 #int_RTCC
31 void RTCC_isr(void)
2742 kaklik 32 {
2744 kaklik 33 time_overflow_count++;
34 }
35  
36 #int_TIMER1
37 void TIMER1_isr(void)
38 {
39 overflow_count++;
2742 kaklik 40 output_toggle(LED3);
2744 kaklik 41 output_toggle(BEEP);
2742 kaklik 42 }
43  
2744 kaklik 44 void sound_beep( unsigned int lenght, int16 frequency)
45 {
46 unsigned int i;
47  
48 for(i=0;i<=lenght;i++)
49 {
50 output_toggle(BEEP);
51 delay_us(1/frequency);
52 }
53 }
54  
2742 kaklik 55 void main()
56 {
2744 kaklik 57 unsigned int16 integration_time=60;
58 unsigned int16 time;
59 unsigned int16 last_timer; // promena pro praskani
60 unsigned int1 button_press; // semafor pro cteni tlacitek
61 unsigned int16 measurement_number=0;
2742 kaklik 62  
63 setup_adc_ports(sAN0|VSS_VDD);
64 setup_adc(ADC_CLOCK_DIV_32);
65 setup_spi(SPI_SS_DISABLED);
2744 kaklik 66 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
67 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
68 setup_timer_2(T2_DISABLED,0,1);
2742 kaklik 69 setup_ccp1(CCP_PWM);
70 setup_ccp2(CCP_PWM);
71 setup_oscillator(OSC_8MHZ);
72  
73 set_pwm1_duty(0);
74 set_pwm2_duty(0);
75  
76 output_high(LED1);
77 output_high(LED2);
78 output_high(LED3);
79 output_high(LED4);
2744 kaklik 80 output_low(BEEP);
2742 kaklik 81  
2744 kaklik 82 setup_comparator(CP2_A0_VREF|CP2_OUT_ON_A5); // sets two comparators(A1 and VR and A2 as the output)
83 setup_vref(VREF_HIGH|6); //sets 3.6(vdd *value/32 +vdd/4) if vdd is 5.0V
84 enable_interrupts(INT_COMP); //enables the comparator interrupt
85 enable_interrupts(INT_RTCC);
86 enable_interrupts(INT_TIMER1);
87 enable_interrupts(INT_TIMER2);
2742 kaklik 88 enable_interrupts(GLOBAL);
2744 kaklik 89 lcd_init();
2742 kaklik 90  
2744 kaklik 91 lcd_putc("\fGM counter V1.0 \n 2013 MLAB");
92 printf("Geiger-Muller Counter V1.0 \r\n");
93 printf("(c) 2013 MLAB and UST.cz \r\n");
94 Delay_ms(1000);
2742 kaklik 95  
2744 kaklik 96 lcd_putc("\f");
97 printf("\r\n");
2742 kaklik 98  
2744 kaklik 99 set_timer1(0);
100  
2742 kaklik 101 while(true)
102 {
2744 kaklik 103 pulse_count = get_timer1() + (0xffff * overflow_count);
104 time = integration_time + 1 - (time_overflow_count * 0.0327) ;
105  
106 if(get_timer1() != last_timer)
2742 kaklik 107 {
2744 kaklik 108 output_toggle(BEEP);
109 last_timer=get_timer1();
2742 kaklik 110 }
2744 kaklik 111  
112 lcd_gotoxy(1,1);
113 printf(lcd_putc,"Count:%lu ",pulse_count);
114 lcd_gotoxy(1,2);
115 printf(lcd_putc,"T:%lu ",time);
116 lcd_gotoxy(9,2);
117 printf(lcd_putc,"I:%lu ", integration_time);
118  
119 if(time == 0)
2742 kaklik 120 {
2744 kaklik 121 set_timer0(0);
122 time_overflow_count=0;
123 sound_beep(1000,700);
124 printf("$GMC1.0 %lu %lu %lu \r\n", measurement_number, integration_time, pulse_count);
125 set_timer1(0);
126 overflow_count=0;
127 measurement_number++;
2742 kaklik 128 }
129  
2744 kaklik 130 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
131 {
132 if(!input(S1))
133 {
134 delay_ms(20);
135 if(!input(S1))
136 {
137 button_press=true;
138 sound_beep(100,700);
139 if(integration_time < 0xfffa ) integration_time+=5;
140  
141 }
142 }
2742 kaklik 143  
2744 kaklik 144 if(!input(S2))
145 {
146 delay_ms(20);
147 if(!input(S2))
148 {
149 button_press=true;
150 sound_beep(100,600);
151 if(integration_time < 0xfffa ) integration_time-=5;
2742 kaklik 152  
2744 kaklik 153 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
154 time_overflow_count=0;
155 set_timer1(0);
156 overflow_count=0;
157 }
158 }
159  
160 if(!input(S3))
161 {
162 delay_ms(20);
163 if(!input(S3))
164 {
165 button_press=true;
166 sound_beep(100,500);
167 if(integration_time < 0xffff ) integration_time++;
168 }
169 }
170  
171 if(!input(S4))
172 {
173 delay_ms(20);
174 if(!input(S4))
175 {
176 button_press=true;
177 sound_beep(100,400);
178 if(integration_time < 0xffff ) integration_time--;
179  
180 set_timer0(0); // vynulovani pocitadel, kdyz uzivatel sahne na tlacitka
181 time_overflow_count=0;
182 set_timer1(0);
183 overflow_count=0;
184 }
185 }
186 }
187  
188 if ( input(S1) && input(S2) && input(S3) && input(S4) )
189 {
190 button_press=false;
191 }
192 }
193  
2742 kaklik 194 }