control de velocidad progresivo de un motor de CC en ambas direcciones
Fork of pwm_example by
main.cpp@4:84669d69a2d4, 2017-03-22 (annotated)
- Committer:
- javierjsp
- Date:
- Wed Mar 22 17:08:53 2017 +0000
- Revision:
- 4:84669d69a2d4
- Parent:
- 3:1174132d5190
version 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BaserK | 0:908bb50b9f41 | 1 | /* PWM motor driving example on LPC1768 |
BaserK | 0:908bb50b9f41 | 2 | * |
BaserK | 0:908bb50b9f41 | 3 | * @author: Baser Kandehir |
BaserK | 0:908bb50b9f41 | 4 | * @date: July 9, 2015 |
BaserK | 3:1174132d5190 | 5 | * @license: MIT license |
BaserK | 3:1174132d5190 | 6 | * |
BaserK | 3:1174132d5190 | 7 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 3:1174132d5190 | 8 | * |
BaserK | 3:1174132d5190 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 3:1174132d5190 | 10 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 3:1174132d5190 | 11 | * in the Software without restriction, including without limitation the rights |
BaserK | 3:1174132d5190 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 3:1174132d5190 | 13 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 3:1174132d5190 | 14 | * furnished to do so, subject to the following conditions: |
BaserK | 3:1174132d5190 | 15 | * |
BaserK | 3:1174132d5190 | 16 | * The above copyright notice and this permission notice shall be included in |
BaserK | 3:1174132d5190 | 17 | * all copies or substantial portions of the Software. |
BaserK | 3:1174132d5190 | 18 | * |
BaserK | 3:1174132d5190 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 3:1174132d5190 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 3:1174132d5190 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 3:1174132d5190 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 3:1174132d5190 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 3:1174132d5190 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 3:1174132d5190 | 25 | * THE SOFTWARE. |
BaserK | 3:1174132d5190 | 26 | * |
BaserK | 0:908bb50b9f41 | 27 | * @description of the program: |
BaserK | 0:908bb50b9f41 | 28 | * |
BaserK | 1:554b65be8506 | 29 | * Program is written as an example for the PWM control on mbed LPC1768. |
BaserK | 0:908bb50b9f41 | 30 | * After making neccessary connections, this program can control speed |
BaserK | 0:908bb50b9f41 | 31 | * and direction of a motor. Example code will increase motor speed to |
BaserK | 0:908bb50b9f41 | 32 | * its max in 5 seconds and decrease the motor speed from max to zero |
BaserK | 2:65f66eff154f | 33 | * in 5 seconds and it will keep doing this. |
BaserK | 0:908bb50b9f41 | 34 | * |
BaserK | 0:908bb50b9f41 | 35 | * @connections: |
BaserK | 0:908bb50b9f41 | 36 | *-------------------------------------------------------------- |
BaserK | 0:908bb50b9f41 | 37 | * |LPC1768| |Peripherals| |
BaserK | 0:908bb50b9f41 | 38 | * Pin 21 --------> First input of the L293D |
BaserK | 0:908bb50b9f41 | 39 | * Pin 22 --------> Second input of the L293D |
BaserK | 0:908bb50b9f41 | 40 | * GND -----------> GND of any peripheral |
BaserK | 0:908bb50b9f41 | 41 | * VOUT (3.3 V) --> VCC of the the motor driver |
BaserK | 0:908bb50b9f41 | 42 | *--------------------------------------------------------------- |
BaserK | 0:908bb50b9f41 | 43 | * Note: L293D is a very simple and useful motor driver that mostly used in |
BaserK | 0:908bb50b9f41 | 44 | * robotics. For detailed info and neccessary connections for L293D, please |
BaserK | 0:908bb50b9f41 | 45 | * refer to its datasheet. |
BaserK | 0:908bb50b9f41 | 46 | */ |
BaserK | 0:908bb50b9f41 | 47 | |
BaserK | 0:908bb50b9f41 | 48 | #include "mbed.h" |
BaserK | 0:908bb50b9f41 | 49 | |
javierjsp | 4:84669d69a2d4 | 50 | PwmOut PWM1(PF_8); // pwm outputs |
javierjsp | 4:84669d69a2d4 | 51 | PwmOut PWM2(PF_7); |
javierjsp | 4:84669d69a2d4 | 52 | DigitalOut led1(LED1); |
BaserK | 0:908bb50b9f41 | 53 | |
BaserK | 0:908bb50b9f41 | 54 | /* Function prototype */ |
BaserK | 0:908bb50b9f41 | 55 | void motorControl(bool dir, float pwmVal); |
BaserK | 0:908bb50b9f41 | 56 | |
BaserK | 0:908bb50b9f41 | 57 | int main() |
BaserK | 0:908bb50b9f41 | 58 | { |
javierjsp | 4:84669d69a2d4 | 59 | |
javierjsp | 4:84669d69a2d4 | 60 | led1 = 1; |
javierjsp | 4:84669d69a2d4 | 61 | wait(1); |
javierjsp | 4:84669d69a2d4 | 62 | led1 = 0; |
javierjsp | 4:84669d69a2d4 | 63 | PWM1.write(0); // Duty cycles are initially zero |
javierjsp | 4:84669d69a2d4 | 64 | PWM2.write(0); |
javierjsp | 4:84669d69a2d4 | 65 | PWM1.period(0.02f); // 1 kHz pwm frequency |
javierjsp | 4:84669d69a2d4 | 66 | PWM2.period(0.02f); |
javierjsp | 4:84669d69a2d4 | 67 | |
BaserK | 0:908bb50b9f41 | 68 | while(1) |
BaserK | 0:908bb50b9f41 | 69 | { |
javierjsp | 4:84669d69a2d4 | 70 | for(float i=0;i<1;i=i+0.01f) // increase speed from 0 to 1 in 5 seconds (50ms*100). |
BaserK | 0:908bb50b9f41 | 71 | { |
BaserK | 0:908bb50b9f41 | 72 | motorControl(1,i); |
javierjsp | 4:84669d69a2d4 | 73 | wait(0.05f); |
javierjsp | 4:84669d69a2d4 | 74 | } |
javierjsp | 4:84669d69a2d4 | 75 | for(float i=1;i>0;i=i-0.01f) // decrease speed from 1 to 0 in 5 seconds (50ms*100). |
BaserK | 0:908bb50b9f41 | 76 | { |
BaserK | 0:908bb50b9f41 | 77 | motorControl(0,i); |
javierjsp | 4:84669d69a2d4 | 78 | wait(0.05f); |
BaserK | 0:908bb50b9f41 | 79 | } |
javierjsp | 4:84669d69a2d4 | 80 | led1 = 1; |
javierjsp | 4:84669d69a2d4 | 81 | wait(0.5); |
javierjsp | 4:84669d69a2d4 | 82 | led1 = 0; |
BaserK | 0:908bb50b9f41 | 83 | } |
BaserK | 0:908bb50b9f41 | 84 | } |
BaserK | 0:908bb50b9f41 | 85 | |
BaserK | 0:908bb50b9f41 | 86 | // This is a motor control function which controls the speed and direction of |
BaserK | 0:908bb50b9f41 | 87 | // a motor. Motor drivers like L293D can be used. |
BaserK | 0:908bb50b9f41 | 88 | void motorControl(bool dir, float pwmVal) |
BaserK | 0:908bb50b9f41 | 89 | { |
BaserK | 0:908bb50b9f41 | 90 | if(dir==1) // forward direction |
BaserK | 0:908bb50b9f41 | 91 | { |
javierjsp | 4:84669d69a2d4 | 92 | PWM2.write(0); |
BaserK | 0:908bb50b9f41 | 93 | PWM1.write(pwmVal); |
BaserK | 0:908bb50b9f41 | 94 | } |
BaserK | 0:908bb50b9f41 | 95 | else if(dir==0) // reverse direction |
javierjsp | 4:84669d69a2d4 | 96 | { |
BaserK | 0:908bb50b9f41 | 97 | PWM1.write(0); |
javierjsp | 4:84669d69a2d4 | 98 | PWM2.write(pwmVal); |
BaserK | 0:908bb50b9f41 | 99 | } |
BaserK | 0:908bb50b9f41 | 100 | } |