Snippet:L293

From Curuxa

The L293 is an integrated circuit commonly used to control small DC motors from a microcontroller. It's used in the MC2A and MC2B Modules.

With an L293 we can control up to 2 motors independently. Each motor can be set to spin freely, brake it, turn to the left or to the right.

For more information, see the L293B datasheet (used in MC2A) or the L293D datasheet (used in MC2B).

Snippets

Copy the following code into your program. The pinout must be setup so the pins on the L293 (IN1, IN2, IN3, IN4, EN1 and EN2, here called M1In1, M1In2, M1Enable...) match the digital pins to which they are connected on your Main Board. Remember to setup those pins as digital outputs.

//Pinout
#define M1Enable Rxx
#define M1In1 Rxx
#define M1In2 Rxx
#define M2Enable Rxx
#define M2In1 Rxx
#define M2In2 Rxx
 
//Move motor 1 forward
void M1Fwd(){
        M1Enable=1;
        M1In1=1;
        M1In2=0;
}
 
//Move motor 1 backwards
void M1Bck(){
        M1Enable=1;
        M1In1=0;
        M1In2=1;
}
 
//Brake motor 1 electrically
void M1Brake(){
        M1Enable=1;
        M1In1=0;
        M1In2=0;
}
 
//Motor 1 spins free
void M1Free(){
        M1Enable=0;
}
 
//Move motor 2 forward
void M2Fwd(){
        M2Enable=1;
        M2In1=1;
        M2In2=0;
}
 
//Move motor 2 backwards
void M2Bck(){
        M2Enable=1;
        M2In1=0;
        M2In2=1;
}
 
//Brake motor 2 electrically
void M2Brake(){
        M2Enable=1;
        M2In1=0;
        M2In2=0;
}
 
//Motor 2 spins free
void M2Free(){
        M2Enable=0;
}


Sample program

/*==================================================================
* Sample program for MC2A using MBP18
*
* Motor 1 begins moving forward, then backwards, then it stops
* and motor 2 begins moving forward, then backwards, then it stops
* and motor 1 begins moving forward...
*
* Note: PIC16F88 runs by default at 31.25kHz (internal oscillator)
*
* http://curuxa.org
*=================================================================*/
 
#include <pic16f88.h>
 
typedef unsigned int config;
config at _CONFIG1 __CONFIG = _CP_OFF & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO;
 
//Pinout
#define M1Enable Rxx
#define M1In1 Rxx
#define M1In2 Rxx
#define M2Enable Rxx
#define M2In1 Rxx
#define M2In2 Rxx
 
//Move motor 1 forward
void M1Fwd(){
        M1Enable=1;
        M1In1=1;
        M1In2=0;
}
 
//Move motor 1 backwards
void M1Bck(){
        M1Enable=1;
        M1In1=0;
        M1In2=1;
}
 
//Brake motor 1 electrically
void M1Brake(){
        M1Enable=1;
        M1In1=0;
        M1In2=0;
}
 
//Motor 1 spins free
void M1Free(){
        M1Enable=0;
}
 
//Move motor 2 forward
void M2Fwd(){
        M2Enable=1;
        M2In1=1;
        M2In2=0;
}
 
//Move motor 2 backwards
void M2Bck(){
        M2Enable=1;
        M2In1=0;
        M2In2=1;
}
 
//Brake motor 2 electrically
void M2Brake(){
        M2Enable=1;
        M2In1=0;
        M2In2=0;
}
 
//Motor 2 spins free
void M2Free(){
        M2Enable=0;
}
 
//Simple delay for an unspecified amount of time
void Delay(){
    long i;
 
    for (i=0; i<200; i++) {
        //do nothing
    }
}
 
void main(){
        //configure data pins as digital outputs
        ANSEL=0;
        TRISA=(EDIT THIS!!!);
        TRISB=(EDIT THIS!!!);
 
        while(1){
                M1Fwd();
                Delay();
                M1Bck();
                Delay();
                M1Brake();
 
                M2Fwd();
                Delay();
                M2Bck();
                Delay();
                M2Brake();
        }
}