Phillip Tran
/
dcSpeedController_KL46Z
This program manipulates the speed of a DC motor using PWM for the KL46Z
main.cpp@0:7465d24798a5, 2017-11-25 (annotated)
- Committer:
- philliptran011
- Date:
- Sat Nov 25 23:31:24 2017 +0000
- Revision:
- 0:7465d24798a5
Lab 6 for Microprocessor's Lab to control the speed of a DC Motor using a KL46Z Microcontroller
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
philliptran011 | 0:7465d24798a5 | 1 | #include "mbed.h" |
philliptran011 | 0:7465d24798a5 | 2 | |
philliptran011 | 0:7465d24798a5 | 3 | PwmOut motorc(PTB19); |
philliptran011 | 0:7465d24798a5 | 4 | DigitalOut dir1(PTC11); |
philliptran011 | 0:7465d24798a5 | 5 | DigitalOut dir2(PTC10); |
philliptran011 | 0:7465d24798a5 | 6 | |
philliptran011 | 0:7465d24798a5 | 7 | Serial pc(USBTX, USBRX); |
philliptran011 | 0:7465d24798a5 | 8 | float duty[8] = {1, 0.75, 0.5, 0.25, 0.25, 0.5, 0.75, 1}; |
philliptran011 | 0:7465d24798a5 | 9 | int index = 0; |
philliptran011 | 0:7465d24798a5 | 10 | char entry; |
philliptran011 | 0:7465d24798a5 | 11 | |
philliptran011 | 0:7465d24798a5 | 12 | int main() |
philliptran011 | 0:7465d24798a5 | 13 | { |
philliptran011 | 0:7465d24798a5 | 14 | pc.baud(115200); |
philliptran011 | 0:7465d24798a5 | 15 | pc.printf("Connection Established\n"); |
philliptran011 | 0:7465d24798a5 | 16 | while (true) { |
philliptran011 | 0:7465d24798a5 | 17 | entry = 'A'; |
philliptran011 | 0:7465d24798a5 | 18 | while (entry != '+' && entry != '-') { |
philliptran011 | 0:7465d24798a5 | 19 | pc.printf("Enter a key\n"); |
philliptran011 | 0:7465d24798a5 | 20 | pc.scanf("%c", &entry); |
philliptran011 | 0:7465d24798a5 | 21 | pc.printf("\n%c\n", entry); |
philliptran011 | 0:7465d24798a5 | 22 | if(entry == '+' && index < 7) |
philliptran011 | 0:7465d24798a5 | 23 | { |
philliptran011 | 0:7465d24798a5 | 24 | index=index + 1; |
philliptran011 | 0:7465d24798a5 | 25 | pc.printf("Increase Speed, current speed %f\n", duty[index]); |
philliptran011 | 0:7465d24798a5 | 26 | } |
philliptran011 | 0:7465d24798a5 | 27 | if(entry == '-' && index > 0) |
philliptran011 | 0:7465d24798a5 | 28 | { |
philliptran011 | 0:7465d24798a5 | 29 | index=index - 1; |
philliptran011 | 0:7465d24798a5 | 30 | pc.printf("Decrease Speed, current speed %f\n", duty[index]); |
philliptran011 | 0:7465d24798a5 | 31 | } |
philliptran011 | 0:7465d24798a5 | 32 | } |
philliptran011 | 0:7465d24798a5 | 33 | if(index <=3) |
philliptran011 | 0:7465d24798a5 | 34 | { |
philliptran011 | 0:7465d24798a5 | 35 | dir1 = 0; |
philliptran011 | 0:7465d24798a5 | 36 | dir2 = 1; |
philliptran011 | 0:7465d24798a5 | 37 | } |
philliptran011 | 0:7465d24798a5 | 38 | if(index >3) |
philliptran011 | 0:7465d24798a5 | 39 | { |
philliptran011 | 0:7465d24798a5 | 40 | dir1 = 1; |
philliptran011 | 0:7465d24798a5 | 41 | dir2 = 0; |
philliptran011 | 0:7465d24798a5 | 42 | } |
philliptran011 | 0:7465d24798a5 | 43 | motorc.write(duty[index]); |
philliptran011 | 0:7465d24798a5 | 44 | } |
philliptran011 | 0:7465d24798a5 | 45 | } |