PWM with L298 for two DC Motors.

Dependencies:   mbed FPointer TextLCD1 keypad

Fork of L298 by Juan Carlos Suárez Barón

Committer:
juanitoinig84
Date:
Thu Jul 14 22:49:36 2016 +0000
Revision:
4:72727d4c1d72
Parent:
2:2e93c305bb62
Child:
5:486d0a696705
Segunda versi?n

Who changed what in which revision?

UserRevisionLine numberNew contents of line
satelite 0:f3cd2e9ab89b 1 #include "mbed.h"
juanitoinig84 4:72727d4c1d72 2 # define ON 1
juanitoinig84 4:72727d4c1d72 3 # define OFF 0
juanitoinig84 4:72727d4c1d72 4
juanitoinig84 4:72727d4c1d72 5 ////////////////********Definitions***********///////
juanitoinig84 4:72727d4c1d72 6 DigitalIn sw3(PTA4);
juanitoinig84 4:72727d4c1d72 7
juanitoinig84 4:72727d4c1d72 8 DigitalOut led_red(LED_RED);
juanitoinig84 4:72727d4c1d72 9 DigitalOut led_green(LED_GREEN);
juanitoinig84 4:72727d4c1d72 10 DigitalOut led_blue(LED_BLUE);
juanitoinig84 4:72727d4c1d72 11
juanitoinig84 4:72727d4c1d72 12 InterruptIn sw2(SW2);
juanitoinig84 4:72727d4c1d72 13
juanitoinig84 4:72727d4c1d72 14 Timer debounce; // define debounce timer
satelite 0:f3cd2e9ab89b 15
juanitoinig84 2:2e93c305bb62 16
juanitoinig84 2:2e93c305bb62 17 Serial pc(USBTX,USBRX); // tx,rx
satelite 0:f3cd2e9ab89b 18 DigitalOut in1(PTC3);
satelite 0:f3cd2e9ab89b 19 DigitalOut in2(PTC2);
satelite 0:f3cd2e9ab89b 20 PwmOut ena(PTA2);
satelite 0:f3cd2e9ab89b 21
juanitoinig84 4:72727d4c1d72 22
juanitoinig84 1:c83539e7f8a0 23 DigitalOut in3(PTD1);
juanitoinig84 1:c83539e7f8a0 24 DigitalOut in4(PTD2);
juanitoinig84 1:c83539e7f8a0 25 PwmOut enb(PTD3);
juanitoinig84 1:c83539e7f8a0 26
juanitoinig84 4:72727d4c1d72 27 ///////////****Prototype Functions*******//////////
juanitoinig84 4:72727d4c1d72 28 void stop1(void);
juanitoinig84 4:72727d4c1d72 29 void turnRight1(void);
juanitoinig84 4:72727d4c1d72 30 void turnLeft1(void);
juanitoinig84 4:72727d4c1d72 31 void toggle(void); // function prototype
juanitoinig84 4:72727d4c1d72 32
juanitoinig84 4:72727d4c1d72 33 ///////////////////////////////////////////////////
juanitoinig84 4:72727d4c1d72 34
satelite 0:f3cd2e9ab89b 35 int main()
juanitoinig84 2:2e93c305bb62 36
satelite 0:f3cd2e9ab89b 37 {
juanitoinig84 4:72727d4c1d72 38 debounce.start();
juanitoinig84 4:72727d4c1d72 39 ena.period(1.0/10000.0);// 5Khz period
juanitoinig84 4:72727d4c1d72 40 sw2.rise(&turnRight1); // attach the address of the toggle
juanitoinig84 4:72727d4c1d72 41 sw2.fall(&stop1);
juanitoinig84 4:72727d4c1d72 42 //pc.baud(115200);
juanitoinig84 4:72727d4c1d72 43
satelite 0:f3cd2e9ab89b 44 while (true) {
juanitoinig84 4:72727d4c1d72 45
juanitoinig84 4:72727d4c1d72 46 sw2.rise(&turnRight1); // attach the address of the toggle
juanitoinig84 4:72727d4c1d72 47 sw2.fall(&stop1);
juanitoinig84 1:c83539e7f8a0 48
juanitoinig84 4:72727d4c1d72 49 /* if (sw3 == ON){
juanitoinig84 4:72727d4c1d72 50 led_red = ON;
juanitoinig84 4:72727d4c1d72 51 turnRight1();
juanitoinig84 4:72727d4c1d72 52 ena.write(0.50f);
juanitoinig84 4:72727d4c1d72 53 }
juanitoinig84 4:72727d4c1d72 54 else{
juanitoinig84 4:72727d4c1d72 55 led_red = OFF;
juanitoinig84 4:72727d4c1d72 56 stop1();
juanitoinig84 4:72727d4c1d72 57 wait(0.5f);
juanitoinig84 4:72727d4c1d72 58 }*/
juanitoinig84 4:72727d4c1d72 59
satelite 0:f3cd2e9ab89b 60 }
juanitoinig84 4:72727d4c1d72 61 }
juanitoinig84 4:72727d4c1d72 62
juanitoinig84 4:72727d4c1d72 63 ///////////////***********FUNCTIONS***********************//////////////
juanitoinig84 4:72727d4c1d72 64 void stop1(void) {
juanitoinig84 4:72727d4c1d72 65 //led_red = ON;
juanitoinig84 4:72727d4c1d72 66 in1 = OFF;
juanitoinig84 4:72727d4c1d72 67 in2 = OFF;
juanitoinig84 4:72727d4c1d72 68 //wait(0.5f);
juanitoinig84 4:72727d4c1d72 69 }
juanitoinig84 4:72727d4c1d72 70
juanitoinig84 4:72727d4c1d72 71 void turnRight1(void) {
juanitoinig84 4:72727d4c1d72 72 // led_red = ON;
juanitoinig84 4:72727d4c1d72 73 in1 = ON;
juanitoinig84 4:72727d4c1d72 74 in2 = OFF;
juanitoinig84 4:72727d4c1d72 75
juanitoinig84 4:72727d4c1d72 76 }
juanitoinig84 4:72727d4c1d72 77
juanitoinig84 4:72727d4c1d72 78 void turnLeft1() {
juanitoinig84 4:72727d4c1d72 79 led_blue = ON;
juanitoinig84 4:72727d4c1d72 80 in1 = OFF;
juanitoinig84 4:72727d4c1d72 81 in2 = ON;
juanitoinig84 4:72727d4c1d72 82 }
juanitoinig84 4:72727d4c1d72 83
juanitoinig84 4:72727d4c1d72 84 void toggle() {
juanitoinig84 4:72727d4c1d72 85 if (debounce.read_ms()>200) // only allow toggle if debounce timer
juanitoinig84 4:72727d4c1d72 86 led_red=!led_red; // has passed 200 ms
juanitoinig84 4:72727d4c1d72 87 debounce.reset(); // restart timer when the toggle is performed
satelite 0:f3cd2e9ab89b 88 }