create and send the file to pc
Dependencies: MCP23017 WattBob_TextLCD mbed
Fork of HelloWorld by
main.cpp@4:b5b159adc261, 2014-02-26 (annotated)
- Committer:
- haseo1989
- Date:
- Wed Feb 26 11:30:18 2014 +0000
- Revision:
- 4:b5b159adc261
- Parent:
- 3:6cc06b58870b
- Child:
- 5:27b1b41b2366
test each task
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:fb6bbc10ffa0 | 1 | #include "mbed.h" |
haseo1989 | 4:b5b159adc261 | 2 | //#include "mon.cpp" |
simon | 0:fb6bbc10ffa0 | 3 | |
haseo1989 | 4:b5b159adc261 | 4 | DigitalIn IP1s(p5); |
haseo1989 | 4:b5b159adc261 | 5 | DigitalIn IP1_400ms(p6); |
haseo1989 | 4:b5b159adc261 | 6 | DigitalIn IP2_400ms(p7); |
haseo1989 | 4:b5b159adc261 | 7 | AnalogIn IP1_800ms(p19); |
haseo1989 | 4:b5b159adc261 | 8 | AnalogIn IP2_800ms(p20); |
haseo1989 | 3:6cc06b58870b | 9 | |
haseo1989 | 4:b5b159adc261 | 10 | DigitalOut bit4(LED1); |
haseo1989 | 4:b5b159adc261 | 11 | DigitalOut bit3(LED2); |
haseo1989 | 4:b5b159adc261 | 12 | DigitalOut bit2(LED3); |
haseo1989 | 4:b5b159adc261 | 13 | DigitalOut bit1(LED4); |
haseo1989 | 3:6cc06b58870b | 14 | |
haseo1989 | 3:6cc06b58870b | 15 | int IP1s,IP400ms1,IP400ms2 = 0; |
haseo1989 | 3:6cc06b58870b | 16 | int IP800ms1[4],IP800ms2[4] = 0; |
haseo1989 | 3:6cc06b58870b | 17 | int freq = 0; |
haseo1989 | 3:6cc06b58870b | 18 | int aver_anl1,aver_anl2 = 0; |
haseo1989 | 3:6cc06b58870b | 19 | int digi_val = 0; |
haseo1989 | 3:6cc06b58870b | 20 | |
haseo1989 | 3:6cc06b58870b | 21 | int ero_code = 0; |
haseo1989 | 3:6cc06b58870b | 22 | |
haseo1989 | 3:6cc06b58870b | 23 | int flag4task3 = 0; |
haseo1989 | 3:6cc06b58870b | 24 | int run_task = 0; |
haseo1989 | 3:6cc06b58870b | 25 | int led_show = 0; |
simon | 0:fb6bbc10ffa0 | 26 | |
simon | 0:fb6bbc10ffa0 | 27 | int main() { |
haseo1989 | 3:6cc06b58870b | 28 | |
haseo1989 | 3:6cc06b58870b | 29 | while(1) |
haseo1989 | 3:6cc06b58870b | 30 | { |
haseo1989 | 3:6cc06b58870b | 31 | } |
haseo1989 | 3:6cc06b58870b | 32 | |
haseo1989 | 3:6cc06b58870b | 33 | } |
haseo1989 | 3:6cc06b58870b | 34 | |
haseo1989 | 3:6cc06b58870b | 35 | // wait(1) -> waiting 10ms |
haseo1989 | 3:6cc06b58870b | 36 | void task1_check_wava1s() // check the square wave per second |
haseo1989 | 3:6cc06b58870b | 37 | { |
haseo1989 | 3:6cc06b58870b | 38 | IP1s = 0; |
haseo1989 | 3:6cc06b58870b | 39 | for(int i = 0; i < 1000; i++) //the accuracy is 1us |
haseo1989 | 4:b5b159adc261 | 40 | if(IP1s == 1) |
haseo1989 | 3:6cc06b58870b | 41 | IP1s++; //the lasting time of the impulse of square wave |
haseo1989 | 3:6cc06b58870b | 42 | } |
haseo1989 | 3:6cc06b58870b | 43 | |
haseo1989 | 3:6cc06b58870b | 44 | void task2_checkDIP400ms() //check the SWes per 400ms |
haseo1989 | 3:6cc06b58870b | 45 | { |
haseo1989 | 4:b5b159adc261 | 46 | if( IP1_400ms != 0 ) |
haseo1989 | 3:6cc06b58870b | 47 | IP400ms1 = 1; |
haseo1989 | 3:6cc06b58870b | 48 | else IP400ms1 = 0; |
haseo1989 | 3:6cc06b58870b | 49 | |
haseo1989 | 4:b5b159adc261 | 50 | if( IP2_400ms !=0 ) |
haseo1989 | 3:6cc06b58870b | 51 | IP400ms2 = 1; |
haseo1989 | 3:6cc06b58870b | 52 | else TP400ms2 = 0; |
haseo1989 | 3:6cc06b58870b | 53 | |
haseo1989 | 3:6cc06b58870b | 54 | } |
haseo1989 | 3:6cc06b58870b | 55 | |
haseo1989 | 3:6cc06b58870b | 56 | void task3_checkAIP800ms() |
haseo1989 | 3:6cc06b58870b | 57 | { |
haseo1989 | 4:b5b159adc261 | 58 | IP800ms1[flag4task3] = IP1_800ms; |
haseo1989 | 4:b5b159adc261 | 59 | IP800ms2[flag4task3] = IP2_800ms; |
haseo1989 | 3:6cc06b58870b | 60 | |
haseo1989 | 3:6cc06b58870b | 61 | flag4task3++; |
haseo1989 | 3:6cc06b58870b | 62 | flag4task3 = flag4task3%4; |
simon | 0:fb6bbc10ffa0 | 63 | } |
haseo1989 | 3:6cc06b58870b | 64 | |
haseo1989 | 3:6cc06b58870b | 65 | void task4_display2s() |
haseo1989 | 3:6cc06b58870b | 66 | { |
haseo1989 | 3:6cc06b58870b | 67 | double freq2; |
haseo1989 | 3:6cc06b58870b | 68 | freq2 = 1/IP1s; |
haseo1989 | 3:6cc06b58870b | 69 | freq2 = freq2*1000000; |
haseo1989 | 3:6cc06b58870b | 70 | freq = freq2/1; //frenquency of square wave |
haseo1989 | 3:6cc06b58870b | 71 | |
haseo1989 | 3:6cc06b58870b | 72 | double aver_anl3,aver_anl4 = 0; |
haseo1989 | 3:6cc06b58870b | 73 | aver_anl3 = IP800ms1[0] + IP800ms1[1] + IP800ms1[2] + IP800ms1[3]; |
haseo1989 | 3:6cc06b58870b | 74 | aver_nal3 = aver_anl3/4; |
haseo1989 | 3:6cc06b58870b | 75 | |
haseo1989 | 3:6cc06b58870b | 76 | aver_nal1 = aver_anl3/1; //averaging analog input1 |
haseo1989 | 3:6cc06b58870b | 77 | |
haseo1989 | 3:6cc06b58870b | 78 | aver_anl4 = IP800ms2[0] + IP800ms2[1] + IP800ms2[2] + IP800ms2[3]; |
haseo1989 | 3:6cc06b58870b | 79 | aver_nal4 = aver_anl4/4; |
haseo1989 | 3:6cc06b58870b | 80 | |
haseo1989 | 3:6cc06b58870b | 81 | aver_nal2 = aver_anl4/1; //averaging analog input2 |
haseo1989 | 3:6cc06b58870b | 82 | |
haseo1989 | 3:6cc06b58870b | 83 | if ( IP400ms2 ==0 && IP400ms1 ==0 ) digi_val = 00; |
haseo1989 | 3:6cc06b58870b | 84 | else if ( IP400ms2 ==0 && IP400ms1 ==1 ) digi_val = 01; |
haseo1989 | 3:6cc06b58870b | 85 | else if ( IP400ms2 ==1 && IP400ms1 ==0 ) digi_val = 10; |
haseo1989 | 3:6cc06b58870b | 86 | else digi_val = 11; |
haseo1989 | 3:6cc06b58870b | 87 | // digital value |
haseo1989 | 3:6cc06b58870b | 88 | |
haseo1989 | 3:6cc06b58870b | 89 | // show out all the values |
haseo1989 | 3:6cc06b58870b | 90 | |
haseo1989 | 3:6cc06b58870b | 91 | |
haseo1989 | 3:6cc06b58870b | 92 | } |
haseo1989 | 3:6cc06b58870b | 93 | |
haseo1989 | 3:6cc06b58870b | 94 | void task5_check_SWes1.8s() |
haseo1989 | 3:6cc06b58870b | 95 | { |
haseo1989 | 3:6cc06b58870b | 96 | if((IP400ms1 == 1) && (aver_anl1 > aver_anl2)) ero_code = 3; |
haseo1989 | 3:6cc06b58870b | 97 | else ero_code = 0; //error code |
haseo1989 | 3:6cc06b58870b | 98 | |
haseo1989 | 3:6cc06b58870b | 99 | if(IP400ms2 == 1) |
haseo1989 | 3:6cc06b58870b | 100 | run_task = 1; |
haseo1989 | 3:6cc06b58870b | 101 | else |
haseo1989 | 3:6cc06b58870b | 102 | run_task = 0; |
haseo1989 | 3:6cc06b58870b | 103 | |
haseo1989 | 3:6cc06b58870b | 104 | } |
haseo1989 | 3:6cc06b58870b | 105 | |
haseo1989 | 3:6cc06b58870b | 106 | void run_task1.5s() //blink led |
haseo1989 | 3:6cc06b58870b | 107 | { |
haseo1989 | 3:6cc06b58870b | 108 | led_show = led_show%15; |
haseo1989 | 3:6cc06b58870b | 109 | |
haseo1989 | 3:6cc06b58870b | 110 | int num_led = led_show; |
haseo1989 | 3:6cc06b58870b | 111 | |
haseo1989 | 3:6cc06b58870b | 112 | if(num_led > 7) {num_led = num_led - 8; bit4 = 1 } |
haseo1989 | 3:6cc06b58870b | 113 | else bit4 = 0; |
haseo1989 | 3:6cc06b58870b | 114 | |
haseo1989 | 3:6cc06b58870b | 115 | if(num_led > 3) {num_led = num_led - 4; bit3 = 1} |
haseo1989 | 3:6cc06b58870b | 116 | else bit3 = 0; |
haseo1989 | 3:6cc06b58870b | 117 | |
haseo1989 | 3:6cc06b58870b | 118 | if(num_led > 1) {num_led = num_led - 2; bit2 = 1} |
haseo1989 | 3:6cc06b58870b | 119 | else bit2 = 0; |
haseo1989 | 3:6cc06b58870b | 120 | |
haseo1989 | 3:6cc06b58870b | 121 | bit1 = num; |
haseo1989 | 3:6cc06b58870b | 122 | |
haseo1989 | 3:6cc06b58870b | 123 | led_show++; |
haseo1989 | 3:6cc06b58870b | 124 | } |
haseo1989 | 3:6cc06b58870b | 125 | |
haseo1989 | 3:6cc06b58870b | 126 | void task6_updated5s() //update A.Frequency value B.digital input values C.Filtered analogue values |
haseo1989 | 3:6cc06b58870b | 127 | { |
haseo1989 | 3:6cc06b58870b | 128 | |
haseo1989 | 3:6cc06b58870b | 129 | |
haseo1989 | 3:6cc06b58870b | 130 | { |