![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
VMA306 Library HelloWorld program
main.cpp@0:9afd9068bcc9, 2018-05-31 (annotated)
- Committer:
- haarkon
- Date:
- Thu May 31 17:35:40 2018 +0000
- Revision:
- 0:9afd9068bcc9
- Child:
- 1:dde51c1d6477
VMA306 HelloWorld
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haarkon | 0:9afd9068bcc9 | 1 | #include "mbed.h" |
haarkon | 0:9afd9068bcc9 | 2 | |
haarkon | 0:9afd9068bcc9 | 3 | Serial pc (PA_2, PA_3, 921600); |
haarkon | 0:9afd9068bcc9 | 4 | |
haarkon | 0:9afd9068bcc9 | 5 | Ticker _tick; |
haarkon | 0:9afd9068bcc9 | 6 | Timer _tim; |
haarkon | 0:9afd9068bcc9 | 7 | |
haarkon | 0:9afd9068bcc9 | 8 | DigitalOut _trig1 (PB_15); |
haarkon | 0:9afd9068bcc9 | 9 | DigitalOut _trig2 (PB_14); |
haarkon | 0:9afd9068bcc9 | 10 | DigitalOut _trig3 (PB_13); |
haarkon | 0:9afd9068bcc9 | 11 | |
haarkon | 0:9afd9068bcc9 | 12 | InterruptIn _echo1 (PA_6); |
haarkon | 0:9afd9068bcc9 | 13 | InterruptIn _echo2 (PC_7); |
haarkon | 0:9afd9068bcc9 | 14 | InterruptIn _echo3 (PB_2); |
haarkon | 0:9afd9068bcc9 | 15 | |
haarkon | 0:9afd9068bcc9 | 16 | DigitalOut led1 (PA_5); |
haarkon | 0:9afd9068bcc9 | 17 | DigitalOut led2 (PD_2); |
haarkon | 0:9afd9068bcc9 | 18 | DigitalOut disquette (PA_12); |
haarkon | 0:9afd9068bcc9 | 19 | |
haarkon | 0:9afd9068bcc9 | 20 | |
haarkon | 0:9afd9068bcc9 | 21 | int start1, start2, start3, FlagMes = 0;; |
haarkon | 0:9afd9068bcc9 | 22 | double dist1, dist2, dist3; |
haarkon | 0:9afd9068bcc9 | 23 | |
haarkon | 0:9afd9068bcc9 | 24 | void monte1 (void) |
haarkon | 0:9afd9068bcc9 | 25 | { |
haarkon | 0:9afd9068bcc9 | 26 | start1 = _tim.read_us(); |
haarkon | 0:9afd9068bcc9 | 27 | //pc.printf("\n\rE1_ON"); |
haarkon | 0:9afd9068bcc9 | 28 | } |
haarkon | 0:9afd9068bcc9 | 29 | |
haarkon | 0:9afd9068bcc9 | 30 | void monte2 (void) |
haarkon | 0:9afd9068bcc9 | 31 | { |
haarkon | 0:9afd9068bcc9 | 32 | start2 = _tim.read_us(); |
haarkon | 0:9afd9068bcc9 | 33 | //pc.printf("\n\rE2_ON"); |
haarkon | 0:9afd9068bcc9 | 34 | } |
haarkon | 0:9afd9068bcc9 | 35 | |
haarkon | 0:9afd9068bcc9 | 36 | void monte3 (void) |
haarkon | 0:9afd9068bcc9 | 37 | { |
haarkon | 0:9afd9068bcc9 | 38 | start3 = _tim.read_us(); |
haarkon | 0:9afd9068bcc9 | 39 | //pc.printf("\n\rE3_ON"); |
haarkon | 0:9afd9068bcc9 | 40 | } |
haarkon | 0:9afd9068bcc9 | 41 | |
haarkon | 0:9afd9068bcc9 | 42 | void tombe1 (void) |
haarkon | 0:9afd9068bcc9 | 43 | { |
haarkon | 0:9afd9068bcc9 | 44 | dist1 = (double)(_tim.read_us() - start1)/58.0; |
haarkon | 0:9afd9068bcc9 | 45 | if (dist1 > 500) dist1 = 500; |
haarkon | 0:9afd9068bcc9 | 46 | //pc.printf("\n\rE1_OFF"); |
haarkon | 0:9afd9068bcc9 | 47 | } |
haarkon | 0:9afd9068bcc9 | 48 | |
haarkon | 0:9afd9068bcc9 | 49 | void tombe2 (void) |
haarkon | 0:9afd9068bcc9 | 50 | { |
haarkon | 0:9afd9068bcc9 | 51 | dist2 = (double)(_tim.read_us() - start2)/58.0; |
haarkon | 0:9afd9068bcc9 | 52 | if (dist2 > 500) dist1 = 500; |
haarkon | 0:9afd9068bcc9 | 53 | //pc.printf("\n\rE2_OFF"); |
haarkon | 0:9afd9068bcc9 | 54 | } |
haarkon | 0:9afd9068bcc9 | 55 | |
haarkon | 0:9afd9068bcc9 | 56 | void tombe3 (void) |
haarkon | 0:9afd9068bcc9 | 57 | { |
haarkon | 0:9afd9068bcc9 | 58 | dist3 = (double)(_tim.read_us() - start3)/58.0; |
haarkon | 0:9afd9068bcc9 | 59 | if (dist3 > 500) dist1 = 500; |
haarkon | 0:9afd9068bcc9 | 60 | FlagMes = 1; |
haarkon | 0:9afd9068bcc9 | 61 | //pc.printf("\n\rE3_OFF"); |
haarkon | 0:9afd9068bcc9 | 62 | } |
haarkon | 0:9afd9068bcc9 | 63 | |
haarkon | 0:9afd9068bcc9 | 64 | void picko (void) |
haarkon | 0:9afd9068bcc9 | 65 | { |
haarkon | 0:9afd9068bcc9 | 66 | static int etape = 1; |
haarkon | 0:9afd9068bcc9 | 67 | //pc.printf ("\n\rpick : %d",etape); |
haarkon | 0:9afd9068bcc9 | 68 | switch(etape) { |
haarkon | 0:9afd9068bcc9 | 69 | case 1: |
haarkon | 0:9afd9068bcc9 | 70 | _trig1 = 1; |
haarkon | 0:9afd9068bcc9 | 71 | wait_us(20); |
haarkon | 0:9afd9068bcc9 | 72 | _trig1 = 0; |
haarkon | 0:9afd9068bcc9 | 73 | etape = 2; |
haarkon | 0:9afd9068bcc9 | 74 | break; |
haarkon | 0:9afd9068bcc9 | 75 | |
haarkon | 0:9afd9068bcc9 | 76 | case 2: |
haarkon | 0:9afd9068bcc9 | 77 | _trig2 = 1; |
haarkon | 0:9afd9068bcc9 | 78 | wait_us(20); |
haarkon | 0:9afd9068bcc9 | 79 | _trig2 = 0; |
haarkon | 0:9afd9068bcc9 | 80 | etape = 3; |
haarkon | 0:9afd9068bcc9 | 81 | break; |
haarkon | 0:9afd9068bcc9 | 82 | |
haarkon | 0:9afd9068bcc9 | 83 | case 3: |
haarkon | 0:9afd9068bcc9 | 84 | _trig3 = 1; |
haarkon | 0:9afd9068bcc9 | 85 | wait_us(20); |
haarkon | 0:9afd9068bcc9 | 86 | _trig3 = 0; |
haarkon | 0:9afd9068bcc9 | 87 | etape = 1; |
haarkon | 0:9afd9068bcc9 | 88 | break; |
haarkon | 0:9afd9068bcc9 | 89 | } |
haarkon | 0:9afd9068bcc9 | 90 | } |
haarkon | 0:9afd9068bcc9 | 91 | |
haarkon | 0:9afd9068bcc9 | 92 | int main() |
haarkon | 0:9afd9068bcc9 | 93 | { |
haarkon | 0:9afd9068bcc9 | 94 | _tim.start(); |
haarkon | 0:9afd9068bcc9 | 95 | |
haarkon | 0:9afd9068bcc9 | 96 | _echo1.rise (&monte1); |
haarkon | 0:9afd9068bcc9 | 97 | _echo2.rise (&monte2); |
haarkon | 0:9afd9068bcc9 | 98 | _echo3.rise (&monte3); |
haarkon | 0:9afd9068bcc9 | 99 | |
haarkon | 0:9afd9068bcc9 | 100 | _echo1.fall (&tombe1); |
haarkon | 0:9afd9068bcc9 | 101 | _echo2.fall (&tombe2); |
haarkon | 0:9afd9068bcc9 | 102 | _echo3.fall (&tombe3); |
haarkon | 0:9afd9068bcc9 | 103 | |
haarkon | 0:9afd9068bcc9 | 104 | _tick.attach (&picko, 0.05); |
haarkon | 0:9afd9068bcc9 | 105 | |
haarkon | 0:9afd9068bcc9 | 106 | led1 = 1; |
haarkon | 0:9afd9068bcc9 | 107 | led2 = 0; |
haarkon | 0:9afd9068bcc9 | 108 | disquette = 0; |
haarkon | 0:9afd9068bcc9 | 109 | |
haarkon | 0:9afd9068bcc9 | 110 | while(1) { |
haarkon | 0:9afd9068bcc9 | 111 | if (FlagMes) { |
haarkon | 0:9afd9068bcc9 | 112 | FlagMes = 0; |
haarkon | 0:9afd9068bcc9 | 113 | pc.printf("\rG = %5.2lf - B = %5.2lf - D = %5.2lf", dist1, dist2, dist3); |
haarkon | 0:9afd9068bcc9 | 114 | led1 = !led1; |
haarkon | 0:9afd9068bcc9 | 115 | led2 = !led2; |
haarkon | 0:9afd9068bcc9 | 116 | } |
haarkon | 0:9afd9068bcc9 | 117 | } |
haarkon | 0:9afd9068bcc9 | 118 | } |