![](/media/cache/profiles/ddcbe25988981920c872c1787382f04d.50x50_q85.jpg)
ガイガーカウンタのカウントアップに合わせて表示内容を更新する仕様にしました
Dependencies: Terminal TextLCD mbed SDFileSystem
geiger.cpp@1:ce4b3a504955, 2011-05-04 (annotated)
- Committer:
- abe00makoto
- Date:
- Wed May 04 19:37:08 2011 +0000
- Revision:
- 1:ce4b3a504955
- Parent:
- 0:0d32a6635113
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abe00makoto | 0:0d32a6635113 | 1 | #include "geiger.h" |
abe00makoto | 0:0d32a6635113 | 2 | #include "Terminal.h" |
abe00makoto | 0:0d32a6635113 | 3 | #include "TextLCD.h" |
abe00makoto | 0:0d32a6635113 | 4 | #include "SDFileSystem.h" |
abe00makoto | 0:0d32a6635113 | 5 | |
abe00makoto | 0:0d32a6635113 | 6 | |
abe00makoto | 0:0d32a6635113 | 7 | //debug use |
abe00makoto | 0:0d32a6635113 | 8 | //#define DEBUG |
abe00makoto | 0:0d32a6635113 | 9 | |
abe00makoto | 0:0d32a6635113 | 10 | //avarage range minutes |
abe00makoto | 0:0d32a6635113 | 11 | #define AVARAGE_MIN 5 |
abe00makoto | 0:0d32a6635113 | 12 | |
abe00makoto | 0:0d32a6635113 | 13 | //sleeptime after a minute cout |
abe00makoto | 1:ce4b3a504955 | 14 | #define SLEEP_SEC 30 |
abe00makoto | 0:0d32a6635113 | 15 | |
abe00makoto | 0:0d32a6635113 | 16 | //if sdcard not use commentout #define SD_USE |
abe00makoto | 0:0d32a6635113 | 17 | #define SD_USE |
abe00makoto | 0:0d32a6635113 | 18 | |
abe00makoto | 0:0d32a6635113 | 19 | #ifdef DEBUG |
abe00makoto | 0:0d32a6635113 | 20 | Terminal output(USBTX, USBRX); // tx, rx |
abe00makoto | 0:0d32a6635113 | 21 | #else |
abe00makoto | 0:0d32a6635113 | 22 | TextLCD output( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3 |
abe00makoto | 0:0d32a6635113 | 23 | #endif |
abe00makoto | 0:0d32a6635113 | 24 | |
abe00makoto | 0:0d32a6635113 | 25 | //Trans contorol pin |
abe00makoto | 0:0d32a6635113 | 26 | #define CTL_TRANS_PIN p21 |
abe00makoto | 0:0d32a6635113 | 27 | |
abe00makoto | 0:0d32a6635113 | 28 | //Trans params |
abe00makoto | 0:0d32a6635113 | 29 | #define TRANS_PERIOD (0.16/1000.0) |
abe00makoto | 0:0d32a6635113 | 30 | #define TRANS_DUTY (0.83) |
abe00makoto | 0:0d32a6635113 | 31 | |
abe00makoto | 0:0d32a6635113 | 32 | //Giger Counter Pin |
abe00makoto | 0:0d32a6635113 | 33 | #define GIGER_PIN p18 |
abe00makoto | 0:0d32a6635113 | 34 | |
abe00makoto | 0:0d32a6635113 | 35 | #ifdef SD_USE |
abe00makoto | 0:0d32a6635113 | 36 | //SD File system setup |
abe00makoto | 0:0d32a6635113 | 37 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sclk, cs |
abe00makoto | 0:0d32a6635113 | 38 | #endif |
abe00makoto | 0:0d32a6635113 | 39 | |
abe00makoto | 0:0d32a6635113 | 40 | DigitalOut waiting(LED1); |
abe00makoto | 0:0d32a6635113 | 41 | DigitalOut working(LED2); |
abe00makoto | 0:0d32a6635113 | 42 | |
abe00makoto | 0:0d32a6635113 | 43 | PwmOut trans_ctl(CTL_TRANS_PIN); |
abe00makoto | 0:0d32a6635113 | 44 | InterruptIn gigerin(GIGER_PIN); |
abe00makoto | 0:0d32a6635113 | 45 | Ticker show; |
abe00makoto | 0:0d32a6635113 | 46 | //for timedata |
abe00makoto | 0:0d32a6635113 | 47 | char buf[40]; |
abe00makoto | 0:0d32a6635113 | 48 | time_t sec; |
abe00makoto | 0:0d32a6635113 | 49 | |
abe00makoto | 0:0d32a6635113 | 50 | //counter |
abe00makoto | 0:0d32a6635113 | 51 | int cnt; |
abe00makoto | 0:0d32a6635113 | 52 | //array first touch index chk |
abe00makoto | 0:0d32a6635113 | 53 | int first_touch_index; |
abe00makoto | 0:0d32a6635113 | 54 | //micro sv value |
abe00makoto | 0:0d32a6635113 | 55 | float usv; |
abe00makoto | 0:0d32a6635113 | 56 | //peak sv value; |
abe00makoto | 0:0d32a6635113 | 57 | float peak_usv; |
abe00makoto | 0:0d32a6635113 | 58 | |
abe00makoto | 0:0d32a6635113 | 59 | |
abe00makoto | 0:0d32a6635113 | 60 | //for avarage calculate arrays |
abe00makoto | 0:0d32a6635113 | 61 | float cpm_array[AVARAGE_MIN]; |
abe00makoto | 0:0d32a6635113 | 62 | //current index for array |
abe00makoto | 0:0d32a6635113 | 63 | int cpm_index; |
abe00makoto | 0:0d32a6635113 | 64 | |
abe00makoto | 0:0d32a6635113 | 65 | |
abe00makoto | 0:0d32a6635113 | 66 | ///////////////////////////////// |
abe00makoto | 0:0d32a6635113 | 67 | //trans contorol for 400v make // |
abe00makoto | 0:0d32a6635113 | 68 | ///////////////////////////////// |
abe00makoto | 0:0d32a6635113 | 69 | |
abe00makoto | 0:0d32a6635113 | 70 | //make 400v |
abe00makoto | 0:0d32a6635113 | 71 | void transon() |
abe00makoto | 0:0d32a6635113 | 72 | { |
abe00makoto | 0:0d32a6635113 | 73 | //6khz puluse |
abe00makoto | 0:0d32a6635113 | 74 | trans_ctl.period(TRANS_PERIOD); |
abe00makoto | 0:0d32a6635113 | 75 | trans_ctl=TRANS_DUTY; |
abe00makoto | 0:0d32a6635113 | 76 | } |
abe00makoto | 0:0d32a6635113 | 77 | |
abe00makoto | 0:0d32a6635113 | 78 | //trans sleep! |
abe00makoto | 0:0d32a6635113 | 79 | void transoff() |
abe00makoto | 0:0d32a6635113 | 80 | { |
abe00makoto | 0:0d32a6635113 | 81 | trans_ctl=0.0; |
abe00makoto | 0:0d32a6635113 | 82 | } |
abe00makoto | 0:0d32a6635113 | 83 | |
abe00makoto | 0:0d32a6635113 | 84 | |
abe00makoto | 0:0d32a6635113 | 85 | //print lcd for 1sec |
abe00makoto | 0:0d32a6635113 | 86 | void show_result() |
abe00makoto | 0:0d32a6635113 | 87 | { |
abe00makoto | 0:0d32a6635113 | 88 | output.cls(); |
abe00makoto | 0:0d32a6635113 | 89 | output.printf("NOW%fuSv/h",usv); |
abe00makoto | 0:0d32a6635113 | 90 | output.printf("PEK%fuSv/h",peak_usv); |
abe00makoto | 0:0d32a6635113 | 91 | |
abe00makoto | 0:0d32a6635113 | 92 | } |
abe00makoto | 0:0d32a6635113 | 93 | //sv value calc |
abe00makoto | 0:0d32a6635113 | 94 | void calculate_sv() |
abe00makoto | 0:0d32a6635113 | 95 | { |
abe00makoto | 0:0d32a6635113 | 96 | float cpm=0; |
abe00makoto | 0:0d32a6635113 | 97 | for(int i=0;i<=first_touch_index;i++) |
abe00makoto | 0:0d32a6635113 | 98 | { |
abe00makoto | 0:0d32a6635113 | 99 | cpm+=cpm_array[i]; |
abe00makoto | 0:0d32a6635113 | 100 | } |
abe00makoto | 0:0d32a6635113 | 101 | cpm/=(first_touch_index+1); |
abe00makoto | 1:ce4b3a504955 | 102 | //si-3bg is maybe 200 cps/R/h = 200 cps/ 10000uSv/h = 0.02 cps/uSv/h |
abe00makoto | 1:ce4b3a504955 | 103 | usv=cpm/60.0*0.02; |
abe00makoto | 0:0d32a6635113 | 104 | |
abe00makoto | 0:0d32a6635113 | 105 | //peak usv set |
abe00makoto | 0:0d32a6635113 | 106 | if(peak_usv<usv) |
abe00makoto | 0:0d32a6635113 | 107 | { |
abe00makoto | 0:0d32a6635113 | 108 | peak_usv=usv; |
abe00makoto | 0:0d32a6635113 | 109 | } |
abe00makoto | 0:0d32a6635113 | 110 | } |
abe00makoto | 0:0d32a6635113 | 111 | |
abe00makoto | 0:0d32a6635113 | 112 | |
abe00makoto | 0:0d32a6635113 | 113 | void debug_print() |
abe00makoto | 0:0d32a6635113 | 114 | { |
abe00makoto | 0:0d32a6635113 | 115 | |
abe00makoto | 0:0d32a6635113 | 116 | //printf("\r\n\r\nresult_str=%s\r\n",result_str); |
abe00makoto | 0:0d32a6635113 | 117 | |
abe00makoto | 0:0d32a6635113 | 118 | float cpm=0; |
abe00makoto | 0:0d32a6635113 | 119 | for(int i=0;i<=first_touch_index;i++) |
abe00makoto | 0:0d32a6635113 | 120 | { |
abe00makoto | 0:0d32a6635113 | 121 | cpm+=cpm_array[i]; |
abe00makoto | 0:0d32a6635113 | 122 | } |
abe00makoto | 0:0d32a6635113 | 123 | cpm/=(first_touch_index+1); |
abe00makoto | 0:0d32a6635113 | 124 | printf("%fuSv/h\r\n",cpm/20.0); |
abe00makoto | 0:0d32a6635113 | 125 | for(int i=0;i<AVARAGE_MIN;i++) |
abe00makoto | 0:0d32a6635113 | 126 | { |
abe00makoto | 0:0d32a6635113 | 127 | printf("[%d]=%f\r\n",i,cpm_array[i]); |
abe00makoto | 0:0d32a6635113 | 128 | } |
abe00makoto | 0:0d32a6635113 | 129 | printf("first_touch_index=%d\r\n",first_touch_index); |
abe00makoto | 0:0d32a6635113 | 130 | printf("cpm_index=%d\r\n",cpm_index); |
abe00makoto | 0:0d32a6635113 | 131 | |
abe00makoto | 0:0d32a6635113 | 132 | |
abe00makoto | 0:0d32a6635113 | 133 | } |
abe00makoto | 0:0d32a6635113 | 134 | //interuput giger pin |
abe00makoto | 0:0d32a6635113 | 135 | void geiger_countup() |
abe00makoto | 0:0d32a6635113 | 136 | { |
abe00makoto | 0:0d32a6635113 | 137 | //if sleeptime countup stoped. |
abe00makoto | 0:0d32a6635113 | 138 | if(!working) |
abe00makoto | 0:0d32a6635113 | 139 | return; |
abe00makoto | 0:0d32a6635113 | 140 | |
abe00makoto | 0:0d32a6635113 | 141 | cnt++; |
abe00makoto | 0:0d32a6635113 | 142 | cpm_array[cpm_index]=cnt; |
abe00makoto | 0:0d32a6635113 | 143 | calculate_sv(); |
abe00makoto | 0:0d32a6635113 | 144 | // debug_print(); |
abe00makoto | 0:0d32a6635113 | 145 | } |
abe00makoto | 0:0d32a6635113 | 146 | |
abe00makoto | 0:0d32a6635113 | 147 | //geiger count after 1min |
abe00makoto | 0:0d32a6635113 | 148 | void next_geiger_countup() |
abe00makoto | 0:0d32a6635113 | 149 | { |
abe00makoto | 0:0d32a6635113 | 150 | |
abe00makoto | 0:0d32a6635113 | 151 | cnt=0; |
abe00makoto | 0:0d32a6635113 | 152 | cpm_index++; |
abe00makoto | 0:0d32a6635113 | 153 | if(cpm_index >=AVARAGE_MIN) |
abe00makoto | 0:0d32a6635113 | 154 | { |
abe00makoto | 0:0d32a6635113 | 155 | cpm_index=0; |
abe00makoto | 0:0d32a6635113 | 156 | } |
abe00makoto | 0:0d32a6635113 | 157 | |
abe00makoto | 0:0d32a6635113 | 158 | //first touch index count up |
abe00makoto | 0:0d32a6635113 | 159 | if(cpm_index>first_touch_index) |
abe00makoto | 0:0d32a6635113 | 160 | { |
abe00makoto | 0:0d32a6635113 | 161 | first_touch_index=cpm_index; |
abe00makoto | 0:0d32a6635113 | 162 | } |
abe00makoto | 0:0d32a6635113 | 163 | |
abe00makoto | 0:0d32a6635113 | 164 | cpm_array[cpm_index]=cnt; |
abe00makoto | 0:0d32a6635113 | 165 | calculate_sv(); |
abe00makoto | 0:0d32a6635113 | 166 | // debug_print(); |
abe00makoto | 0:0d32a6635113 | 167 | } |
abe00makoto | 0:0d32a6635113 | 168 | |
abe00makoto | 0:0d32a6635113 | 169 | |
abe00makoto | 0:0d32a6635113 | 170 | void setup_pinmode() |
abe00makoto | 0:0d32a6635113 | 171 | { |
abe00makoto | 0:0d32a6635113 | 172 | //p25 lowlevel = write mode? |
abe00makoto | 0:0d32a6635113 | 173 | gigerin.mode(PullNone); |
abe00makoto | 0:0d32a6635113 | 174 | gigerin.rise(&geiger_countup); |
abe00makoto | 0:0d32a6635113 | 175 | } |
abe00makoto | 0:0d32a6635113 | 176 | |
abe00makoto | 0:0d32a6635113 | 177 | |
abe00makoto | 0:0d32a6635113 | 178 | |
abe00makoto | 0:0d32a6635113 | 179 | //geiger system init |
abe00makoto | 0:0d32a6635113 | 180 | void geiger_setup() |
abe00makoto | 0:0d32a6635113 | 181 | { |
abe00makoto | 0:0d32a6635113 | 182 | first_touch_index=0; |
abe00makoto | 0:0d32a6635113 | 183 | cnt=0; |
abe00makoto | 0:0d32a6635113 | 184 | setup_pinmode(); |
abe00makoto | 0:0d32a6635113 | 185 | sec=time(NULL); |
abe00makoto | 0:0d32a6635113 | 186 | strftime(buf,40, "%Y/%m/%d\n%X", localtime(&sec)); |
abe00makoto | 0:0d32a6635113 | 187 | //fprintf(fp, "giger start %s \n",buf); |
abe00makoto | 0:0d32a6635113 | 188 | output.cls(); |
abe00makoto | 0:0d32a6635113 | 189 | output.printf("TIME%s",buf); |
abe00makoto | 0:0d32a6635113 | 190 | wait(1); |
abe00makoto | 0:0d32a6635113 | 191 | output.cls(); |
abe00makoto | 0:0d32a6635113 | 192 | output.printf("please wait...."); |
abe00makoto | 0:0d32a6635113 | 193 | //lcd show setup 1sec print |
abe00makoto | 0:0d32a6635113 | 194 | show.attach(&show_result,1.0); |
abe00makoto | 0:0d32a6635113 | 195 | } |
abe00makoto | 0:0d32a6635113 | 196 | |
abe00makoto | 0:0d32a6635113 | 197 | |
abe00makoto | 0:0d32a6635113 | 198 | void geigerrun() |
abe00makoto | 0:0d32a6635113 | 199 | { |
abe00makoto | 0:0d32a6635113 | 200 | |
abe00makoto | 0:0d32a6635113 | 201 | |
abe00makoto | 0:0d32a6635113 | 202 | #ifdef SD_USE |
abe00makoto | 0:0d32a6635113 | 203 | //SD card write |
abe00makoto | 0:0d32a6635113 | 204 | { |
abe00makoto | 0:0d32a6635113 | 205 | FILE *fp = fopen("/sd/giger.txt", "a"); |
abe00makoto | 0:0d32a6635113 | 206 | if(fp == NULL) { |
abe00makoto | 0:0d32a6635113 | 207 | // error("Could not open file for write\n"); |
abe00makoto | 0:0d32a6635113 | 208 | printf("please insert SD"); |
abe00makoto | 0:0d32a6635113 | 209 | } |
abe00makoto | 0:0d32a6635113 | 210 | else |
abe00makoto | 0:0d32a6635113 | 211 | { |
abe00makoto | 0:0d32a6635113 | 212 | sec=time(NULL); |
abe00makoto | 0:0d32a6635113 | 213 | strftime(buf,40, "%Y/%m/%d %X", localtime(&sec)); |
abe00makoto | 0:0d32a6635113 | 214 | fprintf(fp, "giger start %s \n",buf); |
abe00makoto | 0:0d32a6635113 | 215 | fclose(fp); |
abe00makoto | 0:0d32a6635113 | 216 | } |
abe00makoto | 0:0d32a6635113 | 217 | } |
abe00makoto | 0:0d32a6635113 | 218 | #endif |
abe00makoto | 0:0d32a6635113 | 219 | geiger_setup(); |
abe00makoto | 0:0d32a6635113 | 220 | while(1) |
abe00makoto | 0:0d32a6635113 | 221 | { |
abe00makoto | 0:0d32a6635113 | 222 | waiting=1; |
abe00makoto | 0:0d32a6635113 | 223 | transon(); |
abe00makoto | 0:0d32a6635113 | 224 | //400v voltage set waiting |
abe00makoto | 0:0d32a6635113 | 225 | wait(1); |
abe00makoto | 0:0d32a6635113 | 226 | waiting=0; |
abe00makoto | 0:0d32a6635113 | 227 | |
abe00makoto | 0:0d32a6635113 | 228 | // |
abe00makoto | 0:0d32a6635113 | 229 | //count up start |
abe00makoto | 0:0d32a6635113 | 230 | // |
abe00makoto | 0:0d32a6635113 | 231 | working=1; |
abe00makoto | 0:0d32a6635113 | 232 | //count start |
abe00makoto | 0:0d32a6635113 | 233 | cnt=0; |
abe00makoto | 0:0d32a6635113 | 234 | calculate_sv(); |
abe00makoto | 0:0d32a6635113 | 235 | wait(60); |
abe00makoto | 0:0d32a6635113 | 236 | working=0; |
abe00makoto | 0:0d32a6635113 | 237 | waiting=1; |
abe00makoto | 0:0d32a6635113 | 238 | // |
abe00makoto | 0:0d32a6635113 | 239 | // trans cooldown time |
abe00makoto | 0:0d32a6635113 | 240 | // |
abe00makoto | 0:0d32a6635113 | 241 | transoff(); |
abe00makoto | 0:0d32a6635113 | 242 | //next countup move index |
abe00makoto | 0:0d32a6635113 | 243 | next_geiger_countup(); |
abe00makoto | 0:0d32a6635113 | 244 | |
abe00makoto | 0:0d32a6635113 | 245 | #ifdef SD_USE |
abe00makoto | 0:0d32a6635113 | 246 | //SD card write gigier.txt |
abe00makoto | 0:0d32a6635113 | 247 | { |
abe00makoto | 0:0d32a6635113 | 248 | FILE *fp = fopen("/sd/giger.txt", "a"); |
abe00makoto | 0:0d32a6635113 | 249 | if(fp == NULL) { |
abe00makoto | 0:0d32a6635113 | 250 | //error("Could not open file for write\n"); |
abe00makoto | 0:0d32a6635113 | 251 | printf("please insert SD"); |
abe00makoto | 0:0d32a6635113 | 252 | } |
abe00makoto | 0:0d32a6635113 | 253 | else |
abe00makoto | 0:0d32a6635113 | 254 | { |
abe00makoto | 0:0d32a6635113 | 255 | sec=time(NULL); |
abe00makoto | 0:0d32a6635113 | 256 | strftime(buf,40, "%Y/%m/%d %X", localtime(&sec)); |
abe00makoto | 0:0d32a6635113 | 257 | fprintf(fp, "%s %fuSv/h\n",buf,usv); |
abe00makoto | 0:0d32a6635113 | 258 | fclose(fp); |
abe00makoto | 0:0d32a6635113 | 259 | } |
abe00makoto | 0:0d32a6635113 | 260 | } |
abe00makoto | 0:0d32a6635113 | 261 | |
abe00makoto | 0:0d32a6635113 | 262 | //SD Card Write raw,txt |
abe00makoto | 0:0d32a6635113 | 263 | { |
abe00makoto | 0:0d32a6635113 | 264 | FILE *fp = fopen("/sd/raw.txt", "a"); |
abe00makoto | 0:0d32a6635113 | 265 | if(fp == NULL) { |
abe00makoto | 0:0d32a6635113 | 266 | printf("please insert SD"); |
abe00makoto | 0:0d32a6635113 | 267 | } |
abe00makoto | 0:0d32a6635113 | 268 | else |
abe00makoto | 0:0d32a6635113 | 269 | { |
abe00makoto | 0:0d32a6635113 | 270 | |
abe00makoto | 0:0d32a6635113 | 271 | sec=time(NULL); |
abe00makoto | 0:0d32a6635113 | 272 | strftime(buf,40, "%Y/%m/%d %X", localtime(&sec)); |
abe00makoto | 0:0d32a6635113 | 273 | fprintf(fp, "%s cpm=%d\n",buf,cpm_array[cpm_index]); |
abe00makoto | 0:0d32a6635113 | 274 | fclose(fp); |
abe00makoto | 0:0d32a6635113 | 275 | } |
abe00makoto | 0:0d32a6635113 | 276 | |
abe00makoto | 0:0d32a6635113 | 277 | } |
abe00makoto | 0:0d32a6635113 | 278 | #endif |
abe00makoto | 0:0d32a6635113 | 279 | |
abe00makoto | 0:0d32a6635113 | 280 | //Sleep time for geiger lifetime safe |
abe00makoto | 0:0d32a6635113 | 281 | wait(SLEEP_SEC); |
abe00makoto | 0:0d32a6635113 | 282 | waiting=0; |
abe00makoto | 0:0d32a6635113 | 283 | } |
abe00makoto | 0:0d32a6635113 | 284 | |
abe00makoto | 0:0d32a6635113 | 285 | |
abe00makoto | 0:0d32a6635113 | 286 | } |
abe00makoto | 0:0d32a6635113 | 287 |