Frank Hsu
/
MY_DC_FUCKING_DATA
why
Fork of MY_DC_FUCKING_DATA by
main.cpp@3:007e753b7417, 2018-04-03 (annotated)
- Committer:
- NTHU666
- Date:
- Tue Apr 03 08:25:46 2018 +0000
- Revision:
- 3:007e753b7417
- Parent:
- 2:b7cbe24ed792
delete static int count
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
enormousjj | 0:449c53fccaef | 1 | #include "mbed.h" |
enormousjj | 0:449c53fccaef | 2 | |
enormousjj | 0:449c53fccaef | 3 | ////////////////////////////////// |
enormousjj | 0:449c53fccaef | 4 | InterruptIn HallA_1(A1); |
enormousjj | 0:449c53fccaef | 5 | InterruptIn HallB_1(A2); |
enormousjj | 0:449c53fccaef | 6 | // Motor2 sensor |
enormousjj | 0:449c53fccaef | 7 | InterruptIn HallA_2(D13); |
enormousjj | 0:449c53fccaef | 8 | InterruptIn HallB_2(D12); |
enormousjj | 0:449c53fccaef | 9 | |
enormousjj | 0:449c53fccaef | 10 | /////////////////////////////////////// |
enormousjj | 1:303fa4b0fa67 | 11 | //DigitalOut led_D3(A4); |
enormousjj | 1:303fa4b0fa67 | 12 | DigitalOut led_D4(A5); |
enormousjj | 0:449c53fccaef | 13 | /////////////////////////////////////// |
enormousjj | 0:449c53fccaef | 14 | enum State {CW, CCW}state; |
enormousjj | 0:449c53fccaef | 15 | int speed_count = 0; |
enormousjj | 0:449c53fccaef | 16 | void Speed_count(); |
enormousjj | 0:449c53fccaef | 17 | float DetectRPM_Motor_1(); |
enormousjj | 0:449c53fccaef | 18 | ///////////////////////////////// |
enormousjj | 0:449c53fccaef | 19 | Timer RPM_timer; |
enormousjj | 0:449c53fccaef | 20 | Ticker timer; |
enormousjj | 0:449c53fccaef | 21 | |
enormousjj | 0:449c53fccaef | 22 | int hallA_1 = 0; |
enormousjj | 0:449c53fccaef | 23 | int hallA_1_old = 0; |
enormousjj | 0:449c53fccaef | 24 | int hallB_1 = 0; |
enormousjj | 0:449c53fccaef | 25 | int hallB_1_old =0; |
enormousjj | 0:449c53fccaef | 26 | int hallA_2 = 0; |
enormousjj | 0:449c53fccaef | 27 | int hallA_2_old = 0; |
enormousjj | 0:449c53fccaef | 28 | int hallB_2 = 0; |
enormousjj | 0:449c53fccaef | 29 | int hallB_2_old = 0; |
enormousjj | 0:449c53fccaef | 30 | |
enormousjj | 0:449c53fccaef | 31 | //////// |
enormousjj | 0:449c53fccaef | 32 | float DetectRPM_Motor_1(); |
enormousjj | 0:449c53fccaef | 33 | void Speed_count(); |
enormousjj | 0:449c53fccaef | 34 | void HallSensor(); |
enormousjj | 1:303fa4b0fa67 | 35 | void init_timer(); |
enormousjj | 1:303fa4b0fa67 | 36 | void init_function(); |
enormousjj | 0:449c53fccaef | 37 | ///////////////////////////////// |
enormousjj | 0:449c53fccaef | 38 | float DetectRPM_Motor_1() |
enormousjj | 0:449c53fccaef | 39 | { |
enormousjj | 1:303fa4b0fa67 | 40 | |
enormousjj | 0:449c53fccaef | 41 | float time_difference = 0; |
NTHU666 | 2:b7cbe24ed792 | 42 | |
NTHU666 | 2:b7cbe24ed792 | 43 | time_difference = speed_count*100.0f/12.0f*60.0f; |
enormousjj | 0:449c53fccaef | 44 | |
enormousjj | 0:449c53fccaef | 45 | return time_difference; |
enormousjj | 0:449c53fccaef | 46 | |
enormousjj | 0:449c53fccaef | 47 | } |
enormousjj | 0:449c53fccaef | 48 | |
enormousjj | 0:449c53fccaef | 49 | void HallSensor() |
enormousjj | 0:449c53fccaef | 50 | { |
enormousjj | 1:303fa4b0fa67 | 51 | |
NTHU666 | 3:007e753b7417 | 52 | |
enormousjj | 1:303fa4b0fa67 | 53 | |
enormousjj | 0:449c53fccaef | 54 | hallA_1 = HallA_1.read(); |
enormousjj | 0:449c53fccaef | 55 | hallB_1 = HallB_1.read(); |
enormousjj | 0:449c53fccaef | 56 | |
enormousjj | 0:449c53fccaef | 57 | if(hallB_1-hallB_1_old==1 && hallA_1==0) |
enormousjj | 0:449c53fccaef | 58 | { |
NTHU666 | 2:b7cbe24ed792 | 59 | speed_count++; |
enormousjj | 0:449c53fccaef | 60 | state = CW; |
enormousjj | 0:449c53fccaef | 61 | led_D4=1; |
enormousjj | 1:303fa4b0fa67 | 62 | |
enormousjj | 1:303fa4b0fa67 | 63 | wait(1000); |
enormousjj | 1:303fa4b0fa67 | 64 | led_D4 =0; |
enormousjj | 1:303fa4b0fa67 | 65 | |
enormousjj | 1:303fa4b0fa67 | 66 | // led_D3=0; |
enormousjj | 0:449c53fccaef | 67 | } |
enormousjj | 0:449c53fccaef | 68 | else if(hallB_1-hallB_1_old==-1 && hallA_1 == 1) |
enormousjj | 0:449c53fccaef | 69 | { |
NTHU666 | 2:b7cbe24ed792 | 70 | speed_count++; |
enormousjj | 0:449c53fccaef | 71 | state = CW; |
enormousjj | 0:449c53fccaef | 72 | led_D4=1; |
enormousjj | 1:303fa4b0fa67 | 73 | |
enormousjj | 1:303fa4b0fa67 | 74 | wait(1000); |
enormousjj | 1:303fa4b0fa67 | 75 | led_D4 =0; |
enormousjj | 1:303fa4b0fa67 | 76 | // led_D3=0; |
enormousjj | 0:449c53fccaef | 77 | } |
enormousjj | 0:449c53fccaef | 78 | |
enormousjj | 0:449c53fccaef | 79 | else if(hallB_1-hallB_1_old==1 && hallA_1==1) |
NTHU666 | 2:b7cbe24ed792 | 80 | { |
NTHU666 | 2:b7cbe24ed792 | 81 | speed_count++; |
enormousjj | 0:449c53fccaef | 82 | state =CCW; |
enormousjj | 1:303fa4b0fa67 | 83 | // led_D3=1; |
enormousjj | 0:449c53fccaef | 84 | led_D4=0; |
enormousjj | 0:449c53fccaef | 85 | |
enormousjj | 0:449c53fccaef | 86 | } |
enormousjj | 0:449c53fccaef | 87 | else if(hallB_1-hallB_1_old==-1 && hallA_1==0) |
enormousjj | 0:449c53fccaef | 88 | { |
NTHU666 | 2:b7cbe24ed792 | 89 | speed_count++; |
enormousjj | 0:449c53fccaef | 90 | state = CCW; |
enormousjj | 1:303fa4b0fa67 | 91 | // led_D3=1; |
enormousjj | 0:449c53fccaef | 92 | led_D4=0; |
enormousjj | 0:449c53fccaef | 93 | } |
enormousjj | 0:449c53fccaef | 94 | |
NTHU666 | 2:b7cbe24ed792 | 95 | hallA_1_old = hallA_1; |
NTHU666 | 2:b7cbe24ed792 | 96 | hallB_1_old = hallB_1; |
enormousjj | 0:449c53fccaef | 97 | |
enormousjj | 0:449c53fccaef | 98 | } |
enormousjj | 1:303fa4b0fa67 | 99 | void timer_interrupt() |
enormousjj | 1:303fa4b0fa67 | 100 | { |
enormousjj | 1:303fa4b0fa67 | 101 | |
enormousjj | 1:303fa4b0fa67 | 102 | DetectRPM_Motor_1(); |
NTHU666 | 2:b7cbe24ed792 | 103 | speed_count = 0; |
enormousjj | 1:303fa4b0fa67 | 104 | HallSensor(); |
enormousjj | 1:303fa4b0fa67 | 105 | |
enormousjj | 1:303fa4b0fa67 | 106 | |
enormousjj | 1:303fa4b0fa67 | 107 | } |
enormousjj | 1:303fa4b0fa67 | 108 | |
enormousjj | 0:449c53fccaef | 109 | |
enormousjj | 0:449c53fccaef | 110 | |
enormousjj | 0:449c53fccaef | 111 | int main() |
enormousjj | 0:449c53fccaef | 112 | { |
enormousjj | 1:303fa4b0fa67 | 113 | init_function(); |
enormousjj | 1:303fa4b0fa67 | 114 | init_timer(); |
enormousjj | 1:303fa4b0fa67 | 115 | while(1) |
enormousjj | 1:303fa4b0fa67 | 116 | { |
enormousjj | 1:303fa4b0fa67 | 117 | ; |
enormousjj | 1:303fa4b0fa67 | 118 | } |
enormousjj | 0:449c53fccaef | 119 | |
enormousjj | 1:303fa4b0fa67 | 120 | |
enormousjj | 1:303fa4b0fa67 | 121 | |
enormousjj | 1:303fa4b0fa67 | 122 | } |
enormousjj | 1:303fa4b0fa67 | 123 | |
enormousjj | 1:303fa4b0fa67 | 124 | |
enormousjj | 1:303fa4b0fa67 | 125 | void init_function() |
enormousjj | 1:303fa4b0fa67 | 126 | { |
enormousjj | 1:303fa4b0fa67 | 127 | // led_D3=0; |
enormousjj | 1:303fa4b0fa67 | 128 | led_D4=0; |
enormousjj | 0:449c53fccaef | 129 | |
enormousjj | 0:449c53fccaef | 130 | } |
enormousjj | 0:449c53fccaef | 131 | |
enormousjj | 0:449c53fccaef | 132 | |
enormousjj | 0:449c53fccaef | 133 | void init_timer() |
enormousjj | 0:449c53fccaef | 134 | { |
enormousjj | 0:449c53fccaef | 135 | timer.attach_us(&timer_interrupt, 10000);//10ms interrupt period (100 Hz) |
enormousjj | 0:449c53fccaef | 136 | } |