create and send the file to pc

Dependencies:   MCP23017 WattBob_TextLCD mbed

Fork of HelloWorld by Simon Ford

Committer:
haseo1989
Date:
Tue Mar 04 15:37:21 2014 +0000
Revision:
7:0c36c40f2ffd
Parent:
6:94ee12962e13
test whole system

Who changed what in which revision?

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