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
main.cpp@0:55725179457f, 2020-03-09 (annotated)
- Committer:
- kuni97
- Date:
- Mon Mar 09 10:08:38 2020 +0000
- Revision:
- 0:55725179457f
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kuni97 | 0:55725179457f | 1 | #include "mbed.h" |
kuni97 | 0:55725179457f | 2 | #include "EncoderCounter.h" |
kuni97 | 0:55725179457f | 3 | |
kuni97 | 0:55725179457f | 4 | DigitalOut myled(LED1); |
kuni97 | 0:55725179457f | 5 | DigitalOut enable(PB_15); |
kuni97 | 0:55725179457f | 6 | |
kuni97 | 0:55725179457f | 7 | // initialise PWM |
kuni97 | 0:55725179457f | 8 | PwmOut pwm_motor1(PB_13); |
kuni97 | 0:55725179457f | 9 | PwmOut pwm_motor2(PA_9); |
kuni97 | 0:55725179457f | 10 | PwmOut pwm_motor3(PA_10); |
kuni97 | 0:55725179457f | 11 | |
kuni97 | 0:55725179457f | 12 | // crete Encoder read objects |
kuni97 | 0:55725179457f | 13 | EncoderCounter counter1(PA_6, PC_7); // counter(pin A, pin B) |
kuni97 | 0:55725179457f | 14 | EncoderCounter counter2(PB_6, PB_7); |
kuni97 | 0:55725179457f | 15 | EncoderCounter counter3(PA_0, PA_1); |
kuni97 | 0:55725179457f | 16 | |
kuni97 | 0:55725179457f | 17 | DigitalIn user_button(USER_BUTTON); |
kuni97 | 0:55725179457f | 18 | |
kuni97 | 0:55725179457f | 19 | int main() |
kuni97 | 0:55725179457f | 20 | { |
kuni97 | 0:55725179457f | 21 | // initialise PWM |
kuni97 | 0:55725179457f | 22 | pwm_motor1.period(0.0005f);// 0.5ms 2KHz |
kuni97 | 0:55725179457f | 23 | pwm_motor1.write(0.5f); |
kuni97 | 0:55725179457f | 24 | pwm_motor2.period(0.0005f);// 0.5ms 2KHz |
kuni97 | 0:55725179457f | 25 | pwm_motor2.write(0.5f); |
kuni97 | 0:55725179457f | 26 | pwm_motor3.period(0.0005f);// 0.5ms 2KHz |
kuni97 | 0:55725179457f | 27 | pwm_motor3.write(0.5f); |
kuni97 | 0:55725179457f | 28 | |
kuni97 | 0:55725179457f | 29 | printf("initialization succesfull\n\r"); |
kuni97 | 0:55725179457f | 30 | |
kuni97 | 0:55725179457f | 31 | while (1) { |
kuni97 | 0:55725179457f | 32 | |
kuni97 | 0:55725179457f | 33 | if(!user_button) { |
kuni97 | 0:55725179457f | 34 | // LED off, PWM 60% |
kuni97 | 0:55725179457f | 35 | myled = 0; |
kuni97 | 0:55725179457f | 36 | enable = 1; |
kuni97 | 0:55725179457f | 37 | pwm_motor1.write(0.6f); |
kuni97 | 0:55725179457f | 38 | pwm_motor2.write(0.6f); |
kuni97 | 0:55725179457f | 39 | pwm_motor3.write(0.6f); |
kuni97 | 0:55725179457f | 40 | |
kuni97 | 0:55725179457f | 41 | |
kuni97 | 0:55725179457f | 42 | } else { |
kuni97 | 0:55725179457f | 43 | // LED on, PWM 50% (no motion) |
kuni97 | 0:55725179457f | 44 | enable = 0; |
kuni97 | 0:55725179457f | 45 | myled = 1; |
kuni97 | 0:55725179457f | 46 | pwm_motor1.write(0.5f); |
kuni97 | 0:55725179457f | 47 | pwm_motor2.write(0.5f); |
kuni97 | 0:55725179457f | 48 | pwm_motor3.write(0.5f); |
kuni97 | 0:55725179457f | 49 | } |
kuni97 | 0:55725179457f | 50 | |
kuni97 | 0:55725179457f | 51 | |
kuni97 | 0:55725179457f | 52 | // display encoder values to console |
kuni97 | 0:55725179457f | 53 | printf("counter1 = %d counter2 = %d counter3 = %d\r\n",counter1.read(),counter2.read(),counter3.read()); |
kuni97 | 0:55725179457f | 54 | |
kuni97 | 0:55725179457f | 55 | wait(0.02f); // Takt 0.02s, 50Hz |
kuni97 | 0:55725179457f | 56 | |
kuni97 | 0:55725179457f | 57 | } |
kuni97 | 0:55725179457f | 58 | |
kuni97 | 0:55725179457f | 59 | } |
kuni97 | 0:55725179457f | 60 |