Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:a94da558b5ac, 2020-12-09 (annotated)
- Committer:
- yeongsookim
- Date:
- Wed Dec 09 00:06:26 2020 +0000
- Revision:
- 0:a94da558b5ac
Init commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yeongsookim | 0:a94da558b5ac | 1 | #include "mbed.h" |
| yeongsookim | 0:a94da558b5ac | 2 | Serial pc(USBTX, USBRX); // tx, rx |
| yeongsookim | 0:a94da558b5ac | 3 | PwmOut mypwm(p24); |
| yeongsookim | 0:a94da558b5ac | 4 | DigitalIn sensorIn(p23, PullUp); |
| yeongsookim | 0:a94da558b5ac | 5 | |
| yeongsookim | 0:a94da558b5ac | 6 | DigitalOut myled(LED1); |
| yeongsookim | 0:a94da558b5ac | 7 | |
| yeongsookim | 0:a94da558b5ac | 8 | |
| yeongsookim | 0:a94da558b5ac | 9 | //Interrupt is generated every 1ms and count is increased by 1 |
| yeongsookim | 0:a94da558b5ac | 10 | int flag_01ms=0; |
| yeongsookim | 0:a94da558b5ac | 11 | int flag_1ms=0; |
| yeongsookim | 0:a94da558b5ac | 12 | int flag_20ms=0; |
| yeongsookim | 0:a94da558b5ac | 13 | void counter_100ns() |
| yeongsookim | 0:a94da558b5ac | 14 | { |
| yeongsookim | 0:a94da558b5ac | 15 | flag_01ms++; |
| yeongsookim | 0:a94da558b5ac | 16 | flag_1ms++; |
| yeongsookim | 0:a94da558b5ac | 17 | flag_20ms++; |
| yeongsookim | 0:a94da558b5ac | 18 | } |
| yeongsookim | 0:a94da558b5ac | 19 | |
| yeongsookim | 0:a94da558b5ac | 20 | int main() |
| yeongsookim | 0:a94da558b5ac | 21 | { |
| yeongsookim | 0:a94da558b5ac | 22 | |
| yeongsookim | 0:a94da558b5ac | 23 | wait(1); |
| yeongsookim | 0:a94da558b5ac | 24 | |
| yeongsookim | 0:a94da558b5ac | 25 | //Set the 1ms thicker. |
| yeongsookim | 0:a94da558b5ac | 26 | Ticker ticker_100ns; |
| yeongsookim | 0:a94da558b5ac | 27 | ticker_100ns.attach(&counter_100ns,0.0001); |
| yeongsookim | 0:a94da558b5ac | 28 | |
| yeongsookim | 0:a94da558b5ac | 29 | Timer time; |
| yeongsookim | 0:a94da558b5ac | 30 | time.start(); |
| yeongsookim | 0:a94da558b5ac | 31 | float init_time = time.read(); |
| yeongsookim | 0:a94da558b5ac | 32 | pc.printf("init_time %.3f sec\n", init_time); |
| yeongsookim | 0:a94da558b5ac | 33 | mypwm.period_us(5); |
| yeongsookim | 0:a94da558b5ac | 34 | mypwm.pulsewidth_us(3); |
| yeongsookim | 0:a94da558b5ac | 35 | |
| yeongsookim | 0:a94da558b5ac | 36 | |
| yeongsookim | 0:a94da558b5ac | 37 | float start_time=0.0; |
| yeongsookim | 0:a94da558b5ac | 38 | float end_time=0.0; |
| yeongsookim | 0:a94da558b5ac | 39 | int wait_5s = 10*1000*2; |
| yeongsookim | 0:a94da558b5ac | 40 | int wait_count =0; |
| yeongsookim | 0:a94da558b5ac | 41 | bool is_start=false; |
| yeongsookim | 0:a94da558b5ac | 42 | bool is_end=false; |
| yeongsookim | 0:a94da558b5ac | 43 | int up_count=0; |
| yeongsookim | 0:a94da558b5ac | 44 | |
| yeongsookim | 0:a94da558b5ac | 45 | bool is_first_rising =false; |
| yeongsookim | 0:a94da558b5ac | 46 | float first_rising_time=0.0; |
| yeongsookim | 0:a94da558b5ac | 47 | |
| yeongsookim | 0:a94da558b5ac | 48 | while(1) { |
| yeongsookim | 0:a94da558b5ac | 49 | |
| yeongsookim | 0:a94da558b5ac | 50 | if(flag_01ms>=1) { |
| yeongsookim | 0:a94da558b5ac | 51 | flag_01ms=0; |
| yeongsookim | 0:a94da558b5ac | 52 | if(is_start==false) { |
| yeongsookim | 0:a94da558b5ac | 53 | if(sensorIn==0) { |
| yeongsookim | 0:a94da558b5ac | 54 | up_count++; |
| yeongsookim | 0:a94da558b5ac | 55 | if (is_first_rising == false) { |
| yeongsookim | 0:a94da558b5ac | 56 | is_first_rising =true; |
| yeongsookim | 0:a94da558b5ac | 57 | first_rising_time = time.read(); |
| yeongsookim | 0:a94da558b5ac | 58 | } |
| yeongsookim | 0:a94da558b5ac | 59 | } |
| yeongsookim | 0:a94da558b5ac | 60 | } else { |
| yeongsookim | 0:a94da558b5ac | 61 | if(wait_count>wait_5s) { |
| yeongsookim | 0:a94da558b5ac | 62 | if(sensorIn==0) { |
| yeongsookim | 0:a94da558b5ac | 63 | up_count++; |
| yeongsookim | 0:a94da558b5ac | 64 | if (is_first_rising == false) { |
| yeongsookim | 0:a94da558b5ac | 65 | is_first_rising =true; |
| yeongsookim | 0:a94da558b5ac | 66 | first_rising_time = time.read(); |
| yeongsookim | 0:a94da558b5ac | 67 | } |
| yeongsookim | 0:a94da558b5ac | 68 | } |
| yeongsookim | 0:a94da558b5ac | 69 | } else { |
| yeongsookim | 0:a94da558b5ac | 70 | wait_count++; |
| yeongsookim | 0:a94da558b5ac | 71 | } |
| yeongsookim | 0:a94da558b5ac | 72 | } |
| yeongsookim | 0:a94da558b5ac | 73 | } |
| yeongsookim | 0:a94da558b5ac | 74 | if(flag_1ms>=10) { |
| yeongsookim | 0:a94da558b5ac | 75 | flag_1ms=0; |
| yeongsookim | 0:a94da558b5ac | 76 | if(up_count>=5 && is_start==false) { |
| yeongsookim | 0:a94da558b5ac | 77 | start_time=first_rising_time; |
| yeongsookim | 0:a94da558b5ac | 78 | pc.printf("start time %.5f sec %.5f\n", start_time-init_time); |
| yeongsookim | 0:a94da558b5ac | 79 | wait_count=0; |
| yeongsookim | 0:a94da558b5ac | 80 | is_first_rising =false; |
| yeongsookim | 0:a94da558b5ac | 81 | first_rising_time = 0.0; |
| yeongsookim | 0:a94da558b5ac | 82 | up_count=0; |
| yeongsookim | 0:a94da558b5ac | 83 | is_start = true; |
| yeongsookim | 0:a94da558b5ac | 84 | } else if(up_count>=5 && is_start==true && is_end==false) { |
| yeongsookim | 0:a94da558b5ac | 85 | end_time=first_rising_time; |
| yeongsookim | 0:a94da558b5ac | 86 | |
| yeongsookim | 0:a94da558b5ac | 87 | pc.printf("end time %.5f sec %.5f \n", end_time-init_time); |
| yeongsookim | 0:a94da558b5ac | 88 | pc.printf("time gap %.5f sec \n", end_time-start_time); |
| yeongsookim | 0:a94da558b5ac | 89 | is_end=true; |
| yeongsookim | 0:a94da558b5ac | 90 | } |
| yeongsookim | 0:a94da558b5ac | 91 | |
| yeongsookim | 0:a94da558b5ac | 92 | } |
| yeongsookim | 0:a94da558b5ac | 93 | if(flag_20ms>=20*10) { |
| yeongsookim | 0:a94da558b5ac | 94 | flag_20ms=0; |
| yeongsookim | 0:a94da558b5ac | 95 | is_first_rising = false; |
| yeongsookim | 0:a94da558b5ac | 96 | first_rising_time=0.0; |
| yeongsookim | 0:a94da558b5ac | 97 | up_count=0; |
| yeongsookim | 0:a94da558b5ac | 98 | } |
| yeongsookim | 0:a94da558b5ac | 99 | myled = sensorIn; |
| yeongsookim | 0:a94da558b5ac | 100 | } |
| yeongsookim | 0:a94da558b5ac | 101 | } |