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.
Dependencies: mbed
AI_Friday.cpp@10:06cc2b1d2aaf, 2016-05-25 (annotated)
- Committer:
- smilestone520
- Date:
- Wed May 25 10:29:05 2016 +0000
- Revision:
- 10:06cc2b1d2aaf
- Child:
- 11:44989c0bcea5
hw
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| smilestone520 | 10:06cc2b1d2aaf | 1 | /*LAB_DCMotor*/ |
| smilestone520 | 10:06cc2b1d2aaf | 2 | #include "mbed.h" |
| smilestone520 | 10:06cc2b1d2aaf | 3 | |
| smilestone520 | 10:06cc2b1d2aaf | 4 | //The number will be compiled as type "double" in default |
| smilestone520 | 10:06cc2b1d2aaf | 5 | //Add a "f" after the number can make it compiled as type "float" |
| smilestone520 | 10:06cc2b1d2aaf | 6 | #define Ts 0.01f //period of timer1 (s) |
| smilestone520 | 10:06cc2b1d2aaf | 7 | #define Kp 0.003f |
| smilestone520 | 10:06cc2b1d2aaf | 8 | #define Ki 0.01f |
| smilestone520 | 10:06cc2b1d2aaf | 9 | |
| smilestone520 | 10:06cc2b1d2aaf | 10 | PwmOut pwm1(D7); |
| smilestone520 | 10:06cc2b1d2aaf | 11 | PwmOut pwm1n(D11); |
| smilestone520 | 10:06cc2b1d2aaf | 12 | PwmOut pwm2(D8); |
| smilestone520 | 10:06cc2b1d2aaf | 13 | PwmOut pwm2n(A3); |
| smilestone520 | 10:06cc2b1d2aaf | 14 | |
| smilestone520 | 10:06cc2b1d2aaf | 15 | Serial bluetooth(D10,D2); |
| smilestone520 | 10:06cc2b1d2aaf | 16 | Serial pc(D1, D0); |
| smilestone520 | 10:06cc2b1d2aaf | 17 | |
| smilestone520 | 10:06cc2b1d2aaf | 18 | DigitalOut led1(A4); |
| smilestone520 | 10:06cc2b1d2aaf | 19 | DigitalOut led2(A5); |
| smilestone520 | 10:06cc2b1d2aaf | 20 | |
| smilestone520 | 10:06cc2b1d2aaf | 21 | //Motor1 sensor |
| smilestone520 | 10:06cc2b1d2aaf | 22 | InterruptIn HallA_1(A1); |
| smilestone520 | 10:06cc2b1d2aaf | 23 | InterruptIn HallB_1(A2); |
| smilestone520 | 10:06cc2b1d2aaf | 24 | //Motor2 sensor |
| smilestone520 | 10:06cc2b1d2aaf | 25 | InterruptIn HallA_2(D13); |
| smilestone520 | 10:06cc2b1d2aaf | 26 | InterruptIn HallB_2(D12); |
| smilestone520 | 10:06cc2b1d2aaf | 27 | |
| smilestone520 | 10:06cc2b1d2aaf | 28 | Ticker timer1; |
| smilestone520 | 10:06cc2b1d2aaf | 29 | void timer1_interrupt(void); |
| smilestone520 | 10:06cc2b1d2aaf | 30 | void CN_interrupt(void); |
| smilestone520 | 10:06cc2b1d2aaf | 31 | |
| smilestone520 | 10:06cc2b1d2aaf | 32 | void init_TIMER(void); |
| smilestone520 | 10:06cc2b1d2aaf | 33 | void init_PWM(void); |
| smilestone520 | 10:06cc2b1d2aaf | 34 | void init_CN(void); |
| smilestone520 | 10:06cc2b1d2aaf | 35 | |
| smilestone520 | 10:06cc2b1d2aaf | 36 | int8_t stateA_1=0, stateB_1=0, stateA_2=0, stateB_2=0; |
| smilestone520 | 10:06cc2b1d2aaf | 37 | int8_t state_1 = 0, state_1_old = 0, state_2 = 0, state_2_old = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 38 | |
| smilestone520 | 10:06cc2b1d2aaf | 39 | int v1Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 40 | int v2Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 41 | |
| smilestone520 | 10:06cc2b1d2aaf | 42 | float v1 = 0.0, v1_ref = 60.0; |
| smilestone520 | 10:06cc2b1d2aaf | 43 | float v1_err = 0.0, v1_ierr = 0.0, PIout_1 = 0.0, PIout_1_old = 0.0; |
| smilestone520 | 10:06cc2b1d2aaf | 44 | float v2 = 0.0, v2_ref = -60.0; |
| smilestone520 | 10:06cc2b1d2aaf | 45 | float v2_err = 0.0, v2_ierr = 0.0, PIout_2 = 0.0, PIout_2_old = 0.0; |
| smilestone520 | 10:06cc2b1d2aaf | 46 | |
| smilestone520 | 10:06cc2b1d2aaf | 47 | int main() { |
| smilestone520 | 10:06cc2b1d2aaf | 48 | |
| smilestone520 | 10:06cc2b1d2aaf | 49 | init_TIMER(); |
| smilestone520 | 10:06cc2b1d2aaf | 50 | init_PWM(); |
| smilestone520 | 10:06cc2b1d2aaf | 51 | init_CN(); |
| smilestone520 | 10:06cc2b1d2aaf | 52 | |
| smilestone520 | 10:06cc2b1d2aaf | 53 | bluetooth.baud(115200); //設定鮑率 |
| smilestone520 | 10:06cc2b1d2aaf | 54 | pc.baud(57600); |
| smilestone520 | 10:06cc2b1d2aaf | 55 | |
| smilestone520 | 10:06cc2b1d2aaf | 56 | |
| smilestone520 | 10:06cc2b1d2aaf | 57 | while(1) |
| smilestone520 | 10:06cc2b1d2aaf | 58 | { |
| smilestone520 | 10:06cc2b1d2aaf | 59 | if(pc.readable()) |
| smilestone520 | 10:06cc2b1d2aaf | 60 | { |
| smilestone520 | 10:06cc2b1d2aaf | 61 | bluetooth.putc(pc.getc()); |
| smilestone520 | 10:06cc2b1d2aaf | 62 | } |
| smilestone520 | 10:06cc2b1d2aaf | 63 | if(bluetooth.readable()) |
| smilestone520 | 10:06cc2b1d2aaf | 64 | { |
| smilestone520 | 10:06cc2b1d2aaf | 65 | pc.putc(bluetooth.getc()); |
| smilestone520 | 10:06cc2b1d2aaf | 66 | } |
| smilestone520 | 10:06cc2b1d2aaf | 67 | } |
| smilestone520 | 10:06cc2b1d2aaf | 68 | } |
| smilestone520 | 10:06cc2b1d2aaf | 69 | |
| smilestone520 | 10:06cc2b1d2aaf | 70 | void timer1_interrupt(void) |
| smilestone520 | 10:06cc2b1d2aaf | 71 | { |
| smilestone520 | 10:06cc2b1d2aaf | 72 | //Motor 1 |
| smilestone520 | 10:06cc2b1d2aaf | 73 | v1 = (float)v1Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
| smilestone520 | 10:06cc2b1d2aaf | 74 | v1Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 75 | |
| smilestone520 | 10:06cc2b1d2aaf | 76 | ///code for PI control/// |
| smilestone520 | 10:06cc2b1d2aaf | 77 | v1_err = v1_ref - v1; |
| smilestone520 | 10:06cc2b1d2aaf | 78 | v1_ierr = 0.01f*v1_err + v1_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 79 | PIout_1 = Kp*v1_err + Ki*v1_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 80 | |
| smilestone520 | 10:06cc2b1d2aaf | 81 | ///////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 82 | |
| smilestone520 | 10:06cc2b1d2aaf | 83 | if(PIout_1 >= 0.5f)PIout_1 = 0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 84 | else if(PIout_1 <= -0.5f)PIout_1 = -0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 85 | pwm1.write(PIout_1 + 0.5f); |
| smilestone520 | 10:06cc2b1d2aaf | 86 | TIM1->CCER |= 0x4; |
| smilestone520 | 10:06cc2b1d2aaf | 87 | |
| smilestone520 | 10:06cc2b1d2aaf | 88 | |
| smilestone520 | 10:06cc2b1d2aaf | 89 | //Motor 2 |
| smilestone520 | 10:06cc2b1d2aaf | 90 | v2 = (float)v2Count * 100.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
| smilestone520 | 10:06cc2b1d2aaf | 91 | v2Count = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 92 | |
| smilestone520 | 10:06cc2b1d2aaf | 93 | ///code for PI control/// |
| smilestone520 | 10:06cc2b1d2aaf | 94 | v2_err = v2_ref - v2; |
| smilestone520 | 10:06cc2b1d2aaf | 95 | v2_ierr = 0.01f*v2_err + v2_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 96 | PIout_2 = Kp*v2_err + Ki*v2_ierr; |
| smilestone520 | 10:06cc2b1d2aaf | 97 | |
| smilestone520 | 10:06cc2b1d2aaf | 98 | ///////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 99 | |
| smilestone520 | 10:06cc2b1d2aaf | 100 | if(PIout_2 >= 0.5f)PIout_2 = 0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 101 | else if(PIout_2 <= -0.5f)PIout_2 = -0.5f; |
| smilestone520 | 10:06cc2b1d2aaf | 102 | pwm2.write(PIout_2 + 0.5f); |
| smilestone520 | 10:06cc2b1d2aaf | 103 | TIM1->CCER |= 0x40; |
| smilestone520 | 10:06cc2b1d2aaf | 104 | |
| smilestone520 | 10:06cc2b1d2aaf | 105 | switch(bluetooth.getc()) |
| smilestone520 | 10:06cc2b1d2aaf | 106 | { |
| smilestone520 | 10:06cc2b1d2aaf | 107 | case '1': |
| smilestone520 | 10:06cc2b1d2aaf | 108 | v1_ref = 30; |
| smilestone520 | 10:06cc2b1d2aaf | 109 | v2_ref = -30; |
| smilestone520 | 10:06cc2b1d2aaf | 110 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 111 | case '2': |
| smilestone520 | 10:06cc2b1d2aaf | 112 | v1_ref = 40; |
| smilestone520 | 10:06cc2b1d2aaf | 113 | v2_ref = -40; |
| smilestone520 | 10:06cc2b1d2aaf | 114 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 115 | case '3': |
| smilestone520 | 10:06cc2b1d2aaf | 116 | v1_ref = 50; |
| smilestone520 | 10:06cc2b1d2aaf | 117 | v2_ref = -50; |
| smilestone520 | 10:06cc2b1d2aaf | 118 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 119 | case '4': |
| smilestone520 | 10:06cc2b1d2aaf | 120 | v1_ref = 60; |
| smilestone520 | 10:06cc2b1d2aaf | 121 | v2_ref = -60; |
| smilestone520 | 10:06cc2b1d2aaf | 122 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 123 | case '5': |
| smilestone520 | 10:06cc2b1d2aaf | 124 | v1_ref = -70; |
| smilestone520 | 10:06cc2b1d2aaf | 125 | v2_ref = 70; |
| smilestone520 | 10:06cc2b1d2aaf | 126 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 127 | case '6': |
| smilestone520 | 10:06cc2b1d2aaf | 128 | v1_ref = -80; |
| smilestone520 | 10:06cc2b1d2aaf | 129 | v2_ref = 80; |
| smilestone520 | 10:06cc2b1d2aaf | 130 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 131 | case '7': |
| smilestone520 | 10:06cc2b1d2aaf | 132 | v1_ref = -100; |
| smilestone520 | 10:06cc2b1d2aaf | 133 | v2_ref = 100; |
| smilestone520 | 10:06cc2b1d2aaf | 134 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 135 | case '8': |
| smilestone520 | 10:06cc2b1d2aaf | 136 | v1_ref = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 137 | v2_ref = 0; |
| smilestone520 | 10:06cc2b1d2aaf | 138 | break; |
| smilestone520 | 10:06cc2b1d2aaf | 139 | } |
| smilestone520 | 10:06cc2b1d2aaf | 140 | |
| smilestone520 | 10:06cc2b1d2aaf | 141 | |
| smilestone520 | 10:06cc2b1d2aaf | 142 | } |
| smilestone520 | 10:06cc2b1d2aaf | 143 | |
| smilestone520 | 10:06cc2b1d2aaf | 144 | void CN_interrupt(void) |
| smilestone520 | 10:06cc2b1d2aaf | 145 | { |
| smilestone520 | 10:06cc2b1d2aaf | 146 | //Motor 1 |
| smilestone520 | 10:06cc2b1d2aaf | 147 | stateA_1 = HallA_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 148 | stateB_1 = HallB_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 149 | |
| smilestone520 | 10:06cc2b1d2aaf | 150 | ///code for state determination/// |
| smilestone520 | 10:06cc2b1d2aaf | 151 | if(stateA_1==0&&stateB_1==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 152 | state_1 = 1;} |
| smilestone520 | 10:06cc2b1d2aaf | 153 | else if(stateA_1==0&&stateB_1==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 154 | state_1 = 2;} |
| smilestone520 | 10:06cc2b1d2aaf | 155 | else if(stateA_1==1&&stateB_1==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 156 | state_1 = 3;} |
| smilestone520 | 10:06cc2b1d2aaf | 157 | else if(stateA_1==1&&stateB_1==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 158 | state_1 = 4;} |
| smilestone520 | 10:06cc2b1d2aaf | 159 | |
| smilestone520 | 10:06cc2b1d2aaf | 160 | if(state_1 == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 161 | { |
| smilestone520 | 10:06cc2b1d2aaf | 162 | if(state_1-state_1_old == -3) |
| smilestone520 | 10:06cc2b1d2aaf | 163 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 164 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 165 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 166 | } |
| smilestone520 | 10:06cc2b1d2aaf | 167 | else if(state_1 == 2) |
| smilestone520 | 10:06cc2b1d2aaf | 168 | { |
| smilestone520 | 10:06cc2b1d2aaf | 169 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 170 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 171 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 172 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 173 | } |
| smilestone520 | 10:06cc2b1d2aaf | 174 | else if(state_1 == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 175 | { |
| smilestone520 | 10:06cc2b1d2aaf | 176 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 177 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 178 | else if(state_1-state_1_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 179 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 180 | } |
| smilestone520 | 10:06cc2b1d2aaf | 181 | else if(state_1 == 4) |
| smilestone520 | 10:06cc2b1d2aaf | 182 | { |
| smilestone520 | 10:06cc2b1d2aaf | 183 | if(state_1-state_1_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 184 | v1Count=v1Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 185 | else if(state_1-state_1_old == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 186 | v1Count=v1Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 187 | } |
| smilestone520 | 10:06cc2b1d2aaf | 188 | state_1_old = state_1; |
| smilestone520 | 10:06cc2b1d2aaf | 189 | |
| smilestone520 | 10:06cc2b1d2aaf | 190 | |
| smilestone520 | 10:06cc2b1d2aaf | 191 | ////////////////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 192 | |
| smilestone520 | 10:06cc2b1d2aaf | 193 | //Forward |
| smilestone520 | 10:06cc2b1d2aaf | 194 | //v1Count +1 |
| smilestone520 | 10:06cc2b1d2aaf | 195 | //Inverse |
| smilestone520 | 10:06cc2b1d2aaf | 196 | //v1Count -1 |
| smilestone520 | 10:06cc2b1d2aaf | 197 | |
| smilestone520 | 10:06cc2b1d2aaf | 198 | |
| smilestone520 | 10:06cc2b1d2aaf | 199 | //Motor 2 |
| smilestone520 | 10:06cc2b1d2aaf | 200 | stateA_2 = HallA_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 201 | stateB_2 = HallB_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 202 | |
| smilestone520 | 10:06cc2b1d2aaf | 203 | ///code for state determination/// |
| smilestone520 | 10:06cc2b1d2aaf | 204 | if(stateA_2==0&&stateB_2==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 205 | state_2 = 1;} |
| smilestone520 | 10:06cc2b1d2aaf | 206 | else if(stateA_2==0&&stateB_2==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 207 | state_2 = 2;} |
| smilestone520 | 10:06cc2b1d2aaf | 208 | else if(stateA_2==1&&stateB_2==1){ |
| smilestone520 | 10:06cc2b1d2aaf | 209 | state_2 = 3;} |
| smilestone520 | 10:06cc2b1d2aaf | 210 | else if(stateA_2==1&&stateB_2==0){ |
| smilestone520 | 10:06cc2b1d2aaf | 211 | state_2 = 4;} |
| smilestone520 | 10:06cc2b1d2aaf | 212 | |
| smilestone520 | 10:06cc2b1d2aaf | 213 | if(state_2 == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 214 | { |
| smilestone520 | 10:06cc2b1d2aaf | 215 | if(state_2-state_2_old == -3) |
| smilestone520 | 10:06cc2b1d2aaf | 216 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 217 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 218 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 219 | } |
| smilestone520 | 10:06cc2b1d2aaf | 220 | else if(state_2 == 2) |
| smilestone520 | 10:06cc2b1d2aaf | 221 | { |
| smilestone520 | 10:06cc2b1d2aaf | 222 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 223 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 224 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 225 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 226 | } |
| smilestone520 | 10:06cc2b1d2aaf | 227 | else if(state_2 == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 228 | { |
| smilestone520 | 10:06cc2b1d2aaf | 229 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 230 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 231 | else if(state_2-state_2_old == -1) |
| smilestone520 | 10:06cc2b1d2aaf | 232 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 233 | } |
| smilestone520 | 10:06cc2b1d2aaf | 234 | else if(state_2 == 4) |
| smilestone520 | 10:06cc2b1d2aaf | 235 | { |
| smilestone520 | 10:06cc2b1d2aaf | 236 | if(state_2-state_2_old == 1) |
| smilestone520 | 10:06cc2b1d2aaf | 237 | v2Count=v2Count+1; |
| smilestone520 | 10:06cc2b1d2aaf | 238 | else if(state_2-state_2_old == 3) |
| smilestone520 | 10:06cc2b1d2aaf | 239 | v2Count=v2Count-1; |
| smilestone520 | 10:06cc2b1d2aaf | 240 | } |
| smilestone520 | 10:06cc2b1d2aaf | 241 | state_2_old = state_2; |
| smilestone520 | 10:06cc2b1d2aaf | 242 | |
| smilestone520 | 10:06cc2b1d2aaf | 243 | ////////////////////////////////// |
| smilestone520 | 10:06cc2b1d2aaf | 244 | |
| smilestone520 | 10:06cc2b1d2aaf | 245 | //Forward |
| smilestone520 | 10:06cc2b1d2aaf | 246 | //v2Count +1 |
| smilestone520 | 10:06cc2b1d2aaf | 247 | //Inverse |
| smilestone520 | 10:06cc2b1d2aaf | 248 | //v2Count -1 |
| smilestone520 | 10:06cc2b1d2aaf | 249 | } |
| smilestone520 | 10:06cc2b1d2aaf | 250 | |
| smilestone520 | 10:06cc2b1d2aaf | 251 | void init_TIMER(void) |
| smilestone520 | 10:06cc2b1d2aaf | 252 | { |
| smilestone520 | 10:06cc2b1d2aaf | 253 | timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz) |
| smilestone520 | 10:06cc2b1d2aaf | 254 | } |
| smilestone520 | 10:06cc2b1d2aaf | 255 | |
| smilestone520 | 10:06cc2b1d2aaf | 256 | void init_PWM(void) |
| smilestone520 | 10:06cc2b1d2aaf | 257 | { |
| smilestone520 | 10:06cc2b1d2aaf | 258 | pwm1.period_us(50); |
| smilestone520 | 10:06cc2b1d2aaf | 259 | pwm1.write(0.5); |
| smilestone520 | 10:06cc2b1d2aaf | 260 | TIM1->CCER |= 0x4; |
| smilestone520 | 10:06cc2b1d2aaf | 261 | |
| smilestone520 | 10:06cc2b1d2aaf | 262 | pwm2.period_us(50); |
| smilestone520 | 10:06cc2b1d2aaf | 263 | pwm2.write(0.5); |
| smilestone520 | 10:06cc2b1d2aaf | 264 | TIM1->CCER |= 0x40; |
| smilestone520 | 10:06cc2b1d2aaf | 265 | } |
| smilestone520 | 10:06cc2b1d2aaf | 266 | |
| smilestone520 | 10:06cc2b1d2aaf | 267 | void init_CN(void) |
| smilestone520 | 10:06cc2b1d2aaf | 268 | { |
| smilestone520 | 10:06cc2b1d2aaf | 269 | HallA_1.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 270 | HallA_1.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 271 | HallB_1.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 272 | HallB_1.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 273 | |
| smilestone520 | 10:06cc2b1d2aaf | 274 | HallA_2.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 275 | HallA_2.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 276 | HallB_2.rise(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 277 | HallB_2.fall(&CN_interrupt); |
| smilestone520 | 10:06cc2b1d2aaf | 278 | |
| smilestone520 | 10:06cc2b1d2aaf | 279 | stateA_1 = HallA_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 280 | stateB_1 = HallB_1.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 281 | stateA_2 = HallA_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 282 | stateB_2 = HallB_2.read(); |
| smilestone520 | 10:06cc2b1d2aaf | 283 | } |