create and send the file to pc
Dependencies: MCP23017 WattBob_TextLCD mbed
Fork of HelloWorld by
main.cpp@6:94ee12962e13, 2014-03-03 (annotated)
- Committer:
- haseo1989
- Date:
- Mon Mar 03 10:33:15 2014 +0000
- Revision:
- 6:94ee12962e13
- Parent:
- 5:27b1b41b2366
- Child:
- 7:0c36c40f2ffd
working for cyclic executive
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haseo1989 | 6:94ee12962e13 | 1 | #include "mbed.h" |
haseo1989 | 6:94ee12962e13 | 2 | #include "MCP23017.h" // include 16-bit parallel I/O header file |
haseo1989 | 6:94ee12962e13 | 3 | #include "WattBob_TextLCD.h" // include 2*16 character display header file |
haseo1989 | 6:94ee12962e13 | 4 | |
haseo1989 | 6:94ee12962e13 | 5 | #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT) |
haseo1989 | 6:94ee12962e13 | 6 | #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT) |
simon | 0:fb6bbc10ffa0 | 7 | |
haseo1989 | 6:94ee12962e13 | 8 | DigitalIn IP_wave(p5); //input of the square wave,1s |
haseo1989 | 6:94ee12962e13 | 9 | DigitalIn DIP_SW1(p6); //digital input ,switch1,400ms |
haseo1989 | 6:94ee12962e13 | 10 | DigitalIn DIP_SW2(p7); //switch2,400ms |
haseo1989 | 6:94ee12962e13 | 11 | AnalogIn AIP_1(p19); //Analog input1800ms |
haseo1989 | 6:94ee12962e13 | 12 | AnalogIn AIP_2(p20); //AIP 2800ms |
haseo1989 | 3:6cc06b58870b | 13 | |
haseo1989 | 6:94ee12962e13 | 14 | DigitalOut bit4(LED1); //each bits of led |
haseo1989 | 4:b5b159adc261 | 15 | DigitalOut bit3(LED2); |
haseo1989 | 4:b5b159adc261 | 16 | DigitalOut bit2(LED3); |
haseo1989 | 4:b5b159adc261 | 17 | DigitalOut bit1(LED4); |
haseo1989 | 3:6cc06b58870b | 18 | |
haseo1989 | 6:94ee12962e13 | 19 | int S_wave,switch1,switch2 = 0; //check the value of square wave, switches |
haseo1989 | 6:94ee12962e13 | 20 | float Anal_val1[4],Anal_val2[4] = {0,0,0,0}; //for calculating the values of analog inputs |
haseo1989 | 3:6cc06b58870b | 21 | |
haseo1989 | 6:94ee12962e13 | 22 | int freq = 0; // frequency of the wave |
haseo1989 | 6:94ee12962e13 | 23 | float aver_anl1,aver_anl2 = 0; //average values of analog inputs |
haseo1989 | 6:94ee12962e13 | 24 | int ero_code = 0; // error code |
haseo1989 | 6:94ee12962e13 | 25 | int flag_task3 = 0; //flag for task3 |
haseo1989 | 6:94ee12962e13 | 26 | |
haseo1989 | 6:94ee12962e13 | 27 | int run_task = 0; //if run_task = 1,running led |
haseo1989 | 6:94ee12962e13 | 28 | int led_show = 0; //led blinksing in incremental ways |
haseo1989 | 3:6cc06b58870b | 29 | |
haseo1989 | 6:94ee12962e13 | 30 | MCP23017 *par_port; // pointer to 16-bit parallel I/O object |
haseo1989 | 6:94ee12962e13 | 31 | WattBob_TextLCD *lcd; // pointer to 2*16 chacater LCD object |
haseo1989 | 6:94ee12962e13 | 32 | Timer tmr; //define a timer for calculating the frequency |
haseo1989 | 6:94ee12962e13 | 33 | |
haseo1989 | 6:94ee12962e13 | 34 | LocalFileSystem local("local"); //Create the local filesystem under the name "local" |
haseo1989 | 6:94ee12962e13 | 35 | FILE *fp; //define a file |
simon | 0:fb6bbc10ffa0 | 36 | |
haseo1989 | 6:94ee12962e13 | 37 | void iniLCD() //initial LCD |
haseo1989 | 6:94ee12962e13 | 38 | { |
haseo1989 | 6:94ee12962e13 | 39 | par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip |
haseo1989 | 6:94ee12962e13 | 40 | lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display |
haseo1989 | 6:94ee12962e13 | 41 | par_port->write_bit(1,BL_BIT); // turn LCD backlight ON |
haseo1989 | 6:94ee12962e13 | 42 | lcd->cls(); |
haseo1989 | 6:94ee12962e13 | 43 | lcd->locate(0,0); |
haseo1989 | 3:6cc06b58870b | 44 | } |
haseo1989 | 3:6cc06b58870b | 45 | |
haseo1989 | 6:94ee12962e13 | 46 | void iniFile() //create file |
haseo1989 | 6:94ee12962e13 | 47 | { |
haseo1989 | 6:94ee12962e13 | 48 | fp = fopen("/local/data.txt", "w"); // Open "data.txt" on the local file system for writing |
haseo1989 | 6:94ee12962e13 | 49 | fprintf(fp,"freqency, Digital value, Analog value1, Analog value2\n"); |
haseo1989 | 6:94ee12962e13 | 50 | fclose(fp); |
haseo1989 | 6:94ee12962e13 | 51 | } |
haseo1989 | 5:27b1b41b2366 | 52 | |
haseo1989 | 6:94ee12962e13 | 53 | void T1_checkWave() //get the frequency of the wave,works |
haseo1989 | 3:6cc06b58870b | 54 | { |
haseo1989 | 6:94ee12962e13 | 55 | float tm_val1,tm_val2 = 0; |
haseo1989 | 6:94ee12962e13 | 56 | int flag,flag_err = 0; |
haseo1989 | 5:27b1b41b2366 | 57 | |
haseo1989 | 6:94ee12962e13 | 58 | Timer Tcheck; //define a timer for calculating the frequency |
haseo1989 | 6:94ee12962e13 | 59 | |
haseo1989 | 6:94ee12962e13 | 60 | Tcheck.reset(); //to check wether the testing is over time or not |
haseo1989 | 6:94ee12962e13 | 61 | Tcheck.start(); |
haseo1989 | 6:94ee12962e13 | 62 | |
haseo1989 | 6:94ee12962e13 | 63 | tmr.reset(); |
haseo1989 | 6:94ee12962e13 | 64 | |
haseo1989 | 6:94ee12962e13 | 65 | while(IP_wave) |
haseo1989 | 6:94ee12962e13 | 66 | { |
haseo1989 | 6:94ee12962e13 | 67 | tm_val2 = Tcheck.read(); |
haseo1989 | 6:94ee12962e13 | 68 | flag = tm_val2*1000; |
haseo1989 | 6:94ee12962e13 | 69 | |
haseo1989 | 6:94ee12962e13 | 70 | if(flag >= 20) //if the signal keeps hi in 20ms , it will end testing. the flag_err will be set one |
haseo1989 | 6:94ee12962e13 | 71 | { |
haseo1989 | 6:94ee12962e13 | 72 | flag_err = 1; |
haseo1989 | 6:94ee12962e13 | 73 | break; |
haseo1989 | 6:94ee12962e13 | 74 | } |
haseo1989 | 6:94ee12962e13 | 75 | } |
haseo1989 | 3:6cc06b58870b | 76 | |
haseo1989 | 6:94ee12962e13 | 77 | while( !IP_wave ) |
haseo1989 | 6:94ee12962e13 | 78 | { |
haseo1989 | 6:94ee12962e13 | 79 | tm_val2 = Tcheck.read(); |
haseo1989 | 6:94ee12962e13 | 80 | flag = tm_val2*1000; |
haseo1989 | 6:94ee12962e13 | 81 | if((flag >= 40) || flag_err == 1) //if the signal keeps low in 50ms , it will end testing. |
haseo1989 | 6:94ee12962e13 | 82 | flag_err = 1; |
haseo1989 | 6:94ee12962e13 | 83 | break; |
haseo1989 | 6:94ee12962e13 | 84 | } |
haseo1989 | 6:94ee12962e13 | 85 | Tcheck.stop(); |
haseo1989 | 6:94ee12962e13 | 86 | Tcheck.reset(); |
haseo1989 | 5:27b1b41b2366 | 87 | |
haseo1989 | 5:27b1b41b2366 | 88 | tmr.start(); |
haseo1989 | 6:94ee12962e13 | 89 | while(IP_wave) |
haseo1989 | 6:94ee12962e13 | 90 | { |
haseo1989 | 6:94ee12962e13 | 91 | |
haseo1989 | 6:94ee12962e13 | 92 | } |
haseo1989 | 5:27b1b41b2366 | 93 | tmr.stop(); |
haseo1989 | 5:27b1b41b2366 | 94 | |
haseo1989 | 5:27b1b41b2366 | 95 | tm_val1 = tmr.read_us(); |
haseo1989 | 6:94ee12962e13 | 96 | if( (tm_val1 > 500.0) && (flag_err == 0) ) //if impulse last more than 500us, it will get its frequency |
haseo1989 | 6:94ee12962e13 | 97 | freq = 500000/tm_val1; //the frenquency of the wave |
haseo1989 | 6:94ee12962e13 | 98 | else |
haseo1989 | 5:27b1b41b2366 | 99 | freq = 0; |
haseo1989 | 6:94ee12962e13 | 100 | |
haseo1989 | 5:27b1b41b2366 | 101 | } |
haseo1989 | 5:27b1b41b2366 | 102 | |
haseo1989 | 6:94ee12962e13 | 103 | void T2_checkSW() //check the SWes per 400ms,works |
haseo1989 | 3:6cc06b58870b | 104 | { |
haseo1989 | 6:94ee12962e13 | 105 | if( DIP_SW1 != 0 ) |
haseo1989 | 6:94ee12962e13 | 106 | switch1 = 1; |
haseo1989 | 6:94ee12962e13 | 107 | else switch1 = 0; |
haseo1989 | 3:6cc06b58870b | 108 | |
haseo1989 | 6:94ee12962e13 | 109 | if( DIP_SW2 !=0 ) |
haseo1989 | 6:94ee12962e13 | 110 | switch2 = 1; |
haseo1989 | 6:94ee12962e13 | 111 | else switch2 = 0; |
haseo1989 | 3:6cc06b58870b | 112 | |
haseo1989 | 3:6cc06b58870b | 113 | } |
haseo1989 | 3:6cc06b58870b | 114 | |
haseo1989 | 6:94ee12962e13 | 115 | void T3_checkAIP() //get the values of analog inputs per 800MS |
haseo1989 | 3:6cc06b58870b | 116 | { |
haseo1989 | 5:27b1b41b2366 | 117 | float para1,para2; |
haseo1989 | 6:94ee12962e13 | 118 | Anal_val1[flag_task3] = AIP_1.read(); |
haseo1989 | 6:94ee12962e13 | 119 | Anal_val2[flag_task3] = AIP_2.read(); |
haseo1989 | 3:6cc06b58870b | 120 | |
haseo1989 | 6:94ee12962e13 | 121 | flag_task3++; |
haseo1989 | 6:94ee12962e13 | 122 | flag_task3 = flag_task3%4; |
haseo1989 | 3:6cc06b58870b | 123 | |
haseo1989 | 6:94ee12962e13 | 124 | para1 = (Anal_val1[0] + Anal_val1[1] + Anal_val1[2] + Anal_val1[3])*3.3; //filt the values |
haseo1989 | 6:94ee12962e13 | 125 | para2 = (Anal_val2[0] + Anal_val2[1] + Anal_val2[2] + Anal_val2[3])*3.3; |
haseo1989 | 5:27b1b41b2366 | 126 | aver_anl1 = para1/4; |
haseo1989 | 5:27b1b41b2366 | 127 | aver_anl2 = para2/4; |
haseo1989 | 6:94ee12962e13 | 128 | } |
haseo1989 | 3:6cc06b58870b | 129 | |
haseo1989 | 6:94ee12962e13 | 130 | void T4_display() //dispaly each values per 2s |
haseo1989 | 5:27b1b41b2366 | 131 | { |
haseo1989 | 6:94ee12962e13 | 132 | int aver_show; |
haseo1989 | 5:27b1b41b2366 | 133 | /* get frequency from task 1 */ |
haseo1989 | 5:27b1b41b2366 | 134 | // frequency is freq |
haseo1989 | 5:27b1b41b2366 | 135 | |
haseo1989 | 5:27b1b41b2366 | 136 | |
haseo1989 | 5:27b1b41b2366 | 137 | /* get digital value from task2 */ |
haseo1989 | 6:94ee12962e13 | 138 | /* digital number = {switch1,switch2} |
haseo1989 | 5:27b1b41b2366 | 139 | |
haseo1989 | 3:6cc06b58870b | 140 | |
haseo1989 | 5:27b1b41b2366 | 141 | */ |
haseo1989 | 5:27b1b41b2366 | 142 | /* get average value from task3,show integers */ |
haseo1989 | 5:27b1b41b2366 | 143 | //data type of aver_anl1,aver_anl2 are float |
haseo1989 | 5:27b1b41b2366 | 144 | |
haseo1989 | 6:94ee12962e13 | 145 | |
haseo1989 | 6:94ee12962e13 | 146 | aver_show = (aver_anl1+aver_anl2)/2; |
haseo1989 | 5:27b1b41b2366 | 147 | |
haseo1989 | 5:27b1b41b2366 | 148 | /* get error code from task5 */ |
haseo1989 | 6:94ee12962e13 | 149 | // ero_code |
haseo1989 | 6:94ee12962e13 | 150 | |
haseo1989 | 6:94ee12962e13 | 151 | |
haseo1989 | 5:27b1b41b2366 | 152 | /* show them!! */ |
haseo1989 | 5:27b1b41b2366 | 153 | |
haseo1989 | 5:27b1b41b2366 | 154 | lcd->cls(); |
haseo1989 | 5:27b1b41b2366 | 155 | lcd->locate(0,0); |
haseo1989 | 6:94ee12962e13 | 156 | lcd->printf("F=%d Aval=%d",freq,aver_show); |
haseo1989 | 5:27b1b41b2366 | 157 | lcd->locate(1,0); |
haseo1989 | 6:94ee12962e13 | 158 | lcd->printf("Dval=%d%d ErC=%d",switch2,switch1,ero_code); |
haseo1989 | 3:6cc06b58870b | 159 | |
haseo1989 | 3:6cc06b58870b | 160 | } |
haseo1989 | 3:6cc06b58870b | 161 | |
haseo1989 | 5:27b1b41b2366 | 162 | |
haseo1989 | 6:94ee12962e13 | 163 | void T5_SWtask() //check the values of switches and run the task , set error code per 1.8s |
haseo1989 | 3:6cc06b58870b | 164 | { |
haseo1989 | 6:94ee12962e13 | 165 | if((switch1 == 1) && (aver_anl1 > aver_anl2)) ero_code = 3; |
haseo1989 | 3:6cc06b58870b | 166 | else ero_code = 0; //error code |
haseo1989 | 3:6cc06b58870b | 167 | |
haseo1989 | 6:94ee12962e13 | 168 | if(switch2 == 1) |
haseo1989 | 3:6cc06b58870b | 169 | run_task = 1; |
haseo1989 | 3:6cc06b58870b | 170 | else |
haseo1989 | 3:6cc06b58870b | 171 | run_task = 0; |
haseo1989 | 3:6cc06b58870b | 172 | |
haseo1989 | 3:6cc06b58870b | 173 | } |
haseo1989 | 3:6cc06b58870b | 174 | |
haseo1989 | 6:94ee12962e13 | 175 | void RunLed() //blink led 1.5s |
haseo1989 | 3:6cc06b58870b | 176 | { |
haseo1989 | 6:94ee12962e13 | 177 | led_show = led_show%16; |
haseo1989 | 3:6cc06b58870b | 178 | int num_led = led_show; |
haseo1989 | 3:6cc06b58870b | 179 | |
haseo1989 | 5:27b1b41b2366 | 180 | if(num_led > 7) {num_led = num_led - 8; bit4 = 1; } |
haseo1989 | 3:6cc06b58870b | 181 | else bit4 = 0; |
haseo1989 | 3:6cc06b58870b | 182 | |
haseo1989 | 5:27b1b41b2366 | 183 | if(num_led > 3) {num_led = num_led - 4; bit3 = 1;} |
haseo1989 | 3:6cc06b58870b | 184 | else bit3 = 0; |
haseo1989 | 3:6cc06b58870b | 185 | |
haseo1989 | 5:27b1b41b2366 | 186 | if(num_led > 1) {num_led = num_led - 2; bit2 = 1;} |
haseo1989 | 3:6cc06b58870b | 187 | else bit2 = 0; |
haseo1989 | 3:6cc06b58870b | 188 | |
haseo1989 | 6:94ee12962e13 | 189 | bit1 = num_led; |
haseo1989 | 3:6cc06b58870b | 190 | led_show++; |
haseo1989 | 3:6cc06b58870b | 191 | } |
haseo1989 | 6:94ee12962e13 | 192 | |
haseo1989 | 6:94ee12962e13 | 193 | void T6_saveData() //save data in uSD; |
haseo1989 | 6:94ee12962e13 | 194 | { |
haseo1989 | 6:94ee12962e13 | 195 | fp = fopen("/local/data.txt", "a"); //open file and add data into the file |
haseo1989 | 6:94ee12962e13 | 196 | fprintf(fp,"%d, _%d%d, %f, %f\n",freq,switch2,switch1,aver_anl1,aver_anl2); |
haseo1989 | 6:94ee12962e13 | 197 | fclose(fp); //close file |
haseo1989 | 6:94ee12962e13 | 198 | |
haseo1989 | 6:94ee12962e13 | 199 | } |
haseo1989 | 3:6cc06b58870b | 200 | |
haseo1989 | 6:94ee12962e13 | 201 | int main() |
haseo1989 | 6:94ee12962e13 | 202 | { |
haseo1989 | 6:94ee12962e13 | 203 | iniLCD(); |
haseo1989 | 6:94ee12962e13 | 204 | iniFile(); |
haseo1989 | 6:94ee12962e13 | 205 | int tick = 0; |
haseo1989 | 6:94ee12962e13 | 206 | lcd->printf("Initializing!"); |
haseo1989 | 6:94ee12962e13 | 207 | while(1) |
haseo1989 | 6:94ee12962e13 | 208 | { |
haseo1989 | 6:94ee12962e13 | 209 | wait(0.1); |
haseo1989 | 6:94ee12962e13 | 210 | tick++; |
haseo1989 | 6:94ee12962e13 | 211 | if(tick%50 == 0) |
haseo1989 | 6:94ee12962e13 | 212 | T6_saveData(); |
haseo1989 | 6:94ee12962e13 | 213 | |
haseo1989 | 6:94ee12962e13 | 214 | if(tick%20 == 0) |
haseo1989 | 6:94ee12962e13 | 215 | T4_display(); |
haseo1989 | 6:94ee12962e13 | 216 | |
haseo1989 | 6:94ee12962e13 | 217 | if(tick%18 == 0) |
haseo1989 | 6:94ee12962e13 | 218 | T5_SWtask(); |
haseo1989 | 6:94ee12962e13 | 219 | |
haseo1989 | 6:94ee12962e13 | 220 | if((tick%15 == 0) && (run_task ==1 )) |
haseo1989 | 6:94ee12962e13 | 221 | RunLed(); |
haseo1989 | 6:94ee12962e13 | 222 | |
haseo1989 | 6:94ee12962e13 | 223 | if(tick%10 == 0) |
haseo1989 | 6:94ee12962e13 | 224 | T1_checkWave(); |
haseo1989 | 6:94ee12962e13 | 225 | |
haseo1989 | 6:94ee12962e13 | 226 | if(tick%8 == 0) |
haseo1989 | 6:94ee12962e13 | 227 | T3_checkAIP(); |
haseo1989 | 6:94ee12962e13 | 228 | |
haseo1989 | 6:94ee12962e13 | 229 | if(tick%4 == 0) |
haseo1989 | 6:94ee12962e13 | 230 | T2_checkSW(); |
haseo1989 | 6:94ee12962e13 | 231 | |
haseo1989 | 6:94ee12962e13 | 232 | tick = tick%900; |
haseo1989 | 3:6cc06b58870b | 233 | |
haseo1989 | 6:94ee12962e13 | 234 | |
haseo1989 | 6:94ee12962e13 | 235 | } |
haseo1989 | 6:94ee12962e13 | 236 | return 0; |
haseo1989 | 6:94ee12962e13 | 237 | } |
haseo1989 | 6:94ee12962e13 | 238 |