han back
/
CLEO_DCMOTOR
SMART CLEO DC Motor
main.cpp@0:daf8e3af4eec, 2017-09-28 (annotated)
- Committer:
- SMART_CLEO
- Date:
- Thu Sep 28 02:11:18 2017 +0000
- Revision:
- 0:daf8e3af4eec
SMART_CLEO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SMART_CLEO | 0:daf8e3af4eec | 1 | #include "mbed.h" |
SMART_CLEO | 0:daf8e3af4eec | 2 | #include "TextLCD.h" |
SMART_CLEO | 0:daf8e3af4eec | 3 | |
SMART_CLEO | 0:daf8e3af4eec | 4 | // INA, INB, INT |
SMART_CLEO | 0:daf8e3af4eec | 5 | PinName pin_DC[3] = {PA_6, PA_7, PA_8}; |
SMART_CLEO | 0:daf8e3af4eec | 6 | |
SMART_CLEO | 0:daf8e3af4eec | 7 | PwmOut INA(pin_DC[0]); |
SMART_CLEO | 0:daf8e3af4eec | 8 | PwmOut INB(pin_DC[1]); |
SMART_CLEO | 0:daf8e3af4eec | 9 | InterruptIn INT(pin_DC[2]); |
SMART_CLEO | 0:daf8e3af4eec | 10 | |
SMART_CLEO | 0:daf8e3af4eec | 11 | // rs, rw, e, d0-d3 |
SMART_CLEO | 0:daf8e3af4eec | 12 | TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11); |
SMART_CLEO | 0:daf8e3af4eec | 13 | |
SMART_CLEO | 0:daf8e3af4eec | 14 | enum{STOP, CW, CCW}; |
SMART_CLEO | 0:daf8e3af4eec | 15 | uint16_t Int_Count = 0; |
SMART_CLEO | 0:daf8e3af4eec | 16 | |
SMART_CLEO | 0:daf8e3af4eec | 17 | void DC_Ctrl(uint8_t dir, float Speed); |
SMART_CLEO | 0:daf8e3af4eec | 18 | void INT_Count_ISR(void); |
SMART_CLEO | 0:daf8e3af4eec | 19 | |
SMART_CLEO | 0:daf8e3af4eec | 20 | int main() { |
SMART_CLEO | 0:daf8e3af4eec | 21 | |
SMART_CLEO | 0:daf8e3af4eec | 22 | lcd.printf(" DC Motor\n"); |
SMART_CLEO | 0:daf8e3af4eec | 23 | lcd.printf(" Volt : [mV]"); |
SMART_CLEO | 0:daf8e3af4eec | 24 | |
SMART_CLEO | 0:daf8e3af4eec | 25 | INT.rise(&INT_Count_ISR); |
SMART_CLEO | 0:daf8e3af4eec | 26 | |
SMART_CLEO | 0:daf8e3af4eec | 27 | while(1) { |
SMART_CLEO | 0:daf8e3af4eec | 28 | // CW |
SMART_CLEO | 0:daf8e3af4eec | 29 | for(int i=0; i<=100; i++) |
SMART_CLEO | 0:daf8e3af4eec | 30 | { |
SMART_CLEO | 0:daf8e3af4eec | 31 | DC_Ctrl(CW, i); |
SMART_CLEO | 0:daf8e3af4eec | 32 | wait(0.02); |
SMART_CLEO | 0:daf8e3af4eec | 33 | } |
SMART_CLEO | 0:daf8e3af4eec | 34 | DC_Ctrl(CW, 0); |
SMART_CLEO | 0:daf8e3af4eec | 35 | wait(2); |
SMART_CLEO | 0:daf8e3af4eec | 36 | lcd.locate(8, 1); |
SMART_CLEO | 0:daf8e3af4eec | 37 | lcd.printf("%4d", Int_Count); |
SMART_CLEO | 0:daf8e3af4eec | 38 | Int_Count = 0; |
SMART_CLEO | 0:daf8e3af4eec | 39 | // CCW |
SMART_CLEO | 0:daf8e3af4eec | 40 | for(int i=0; i<=100; i++) |
SMART_CLEO | 0:daf8e3af4eec | 41 | { |
SMART_CLEO | 0:daf8e3af4eec | 42 | DC_Ctrl(CCW, i); |
SMART_CLEO | 0:daf8e3af4eec | 43 | wait(0.02); |
SMART_CLEO | 0:daf8e3af4eec | 44 | } |
SMART_CLEO | 0:daf8e3af4eec | 45 | DC_Ctrl(CCW, 0); |
SMART_CLEO | 0:daf8e3af4eec | 46 | wait(2); |
SMART_CLEO | 0:daf8e3af4eec | 47 | lcd.locate(8, 1); |
SMART_CLEO | 0:daf8e3af4eec | 48 | lcd.printf("%4d", Int_Count); |
SMART_CLEO | 0:daf8e3af4eec | 49 | Int_Count = 0; |
SMART_CLEO | 0:daf8e3af4eec | 50 | } |
SMART_CLEO | 0:daf8e3af4eec | 51 | } |
SMART_CLEO | 0:daf8e3af4eec | 52 | |
SMART_CLEO | 0:daf8e3af4eec | 53 | void DC_Ctrl(uint8_t dir, float Speed) |
SMART_CLEO | 0:daf8e3af4eec | 54 | { |
SMART_CLEO | 0:daf8e3af4eec | 55 | switch(dir) |
SMART_CLEO | 0:daf8e3af4eec | 56 | { |
SMART_CLEO | 0:daf8e3af4eec | 57 | case STOP: |
SMART_CLEO | 0:daf8e3af4eec | 58 | INA = 0; |
SMART_CLEO | 0:daf8e3af4eec | 59 | INB = 0; |
SMART_CLEO | 0:daf8e3af4eec | 60 | break; |
SMART_CLEO | 0:daf8e3af4eec | 61 | case CW: |
SMART_CLEO | 0:daf8e3af4eec | 62 | INA = Speed/100; |
SMART_CLEO | 0:daf8e3af4eec | 63 | INB = 0; |
SMART_CLEO | 0:daf8e3af4eec | 64 | break; |
SMART_CLEO | 0:daf8e3af4eec | 65 | case CCW: |
SMART_CLEO | 0:daf8e3af4eec | 66 | INA = 0; |
SMART_CLEO | 0:daf8e3af4eec | 67 | INB = Speed/100; |
SMART_CLEO | 0:daf8e3af4eec | 68 | break; |
SMART_CLEO | 0:daf8e3af4eec | 69 | } |
SMART_CLEO | 0:daf8e3af4eec | 70 | } |
SMART_CLEO | 0:daf8e3af4eec | 71 | |
SMART_CLEO | 0:daf8e3af4eec | 72 | void INT_Count_ISR(void) |
SMART_CLEO | 0:daf8e3af4eec | 73 | { |
SMART_CLEO | 0:daf8e3af4eec | 74 | Int_Count++; |
SMART_CLEO | 0:daf8e3af4eec | 75 | } |