control de velocidad progresivo de un motor de CC en ambas direcciones

Dependencies:   mbed

Fork of pwm_example by Baser Kandehir

Committer:
BaserK
Date:
Tue Jul 21 08:22:09 2015 +0000
Revision:
3:1174132d5190
Parent:
2:65f66eff154f
Child:
4:84669d69a2d4
MIT license is added

Who changed what in which revision?

UserRevisionLine numberNew 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
BaserK 0:908bb50b9f41 50 PwmOut PWM1(p21); // pwm outputs
BaserK 0:908bb50b9f41 51 PwmOut PWM2(p22);
BaserK 0:908bb50b9f41 52
BaserK 0:908bb50b9f41 53 /* Function prototype */
BaserK 0:908bb50b9f41 54 void motorControl(bool dir, float pwmVal);
BaserK 0:908bb50b9f41 55
BaserK 0:908bb50b9f41 56 int main()
BaserK 0:908bb50b9f41 57 {
BaserK 0:908bb50b9f41 58 while(1)
BaserK 0:908bb50b9f41 59 {
BaserK 0:908bb50b9f41 60 for(float i=0;i<1;i=i+0.01) // increase speed from 0 to 1 in 5 seconds (50ms*100).
BaserK 0:908bb50b9f41 61 {
BaserK 0:908bb50b9f41 62 motorControl(1,i);
BaserK 0:908bb50b9f41 63 wait_ms(50);
BaserK 0:908bb50b9f41 64 }
BaserK 0:908bb50b9f41 65 for(float i=1;i>0;i=i-0.01) // decrease speed from 1 to 0 in 5 seconds (50ms*100).
BaserK 0:908bb50b9f41 66 {
BaserK 0:908bb50b9f41 67 motorControl(0,i);
BaserK 0:908bb50b9f41 68 wait_ms(50);
BaserK 0:908bb50b9f41 69 }
BaserK 0:908bb50b9f41 70 }
BaserK 0:908bb50b9f41 71 }
BaserK 0:908bb50b9f41 72
BaserK 0:908bb50b9f41 73 // This is a motor control function which controls the speed and direction of
BaserK 0:908bb50b9f41 74 // a motor. Motor drivers like L293D can be used.
BaserK 0:908bb50b9f41 75 void motorControl(bool dir, float pwmVal)
BaserK 0:908bb50b9f41 76 {
BaserK 0:908bb50b9f41 77 PWM1.write(0); // Duty cycles are initially zero
BaserK 0:908bb50b9f41 78 PWM2.write(0);
BaserK 0:908bb50b9f41 79 PWM1.period_ms(1); // 1 kHz pwm frequency
BaserK 0:908bb50b9f41 80 PWM2.period_ms(1);
BaserK 0:908bb50b9f41 81
BaserK 0:908bb50b9f41 82 if(dir==1) // forward direction
BaserK 0:908bb50b9f41 83 {
BaserK 0:908bb50b9f41 84 PWM1.write(pwmVal);
BaserK 0:908bb50b9f41 85 PWM2.write(0);
BaserK 0:908bb50b9f41 86 }
BaserK 0:908bb50b9f41 87 else if(dir==0) // reverse direction
BaserK 0:908bb50b9f41 88 {
BaserK 0:908bb50b9f41 89 PWM2.write(pwmVal);
BaserK 0:908bb50b9f41 90 PWM1.write(0);
BaserK 0:908bb50b9f41 91 }
BaserK 0:908bb50b9f41 92 }