DC motor interfacing with AVR microcontroller(L293D H-bridge This tutorial is taken from the book
MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER
IN C USING WINAVR/AVRGCC.
you can download it free from the files
section(FILE-AVRBOOK.RAR) of yahoo group-booksbybibinhttp://groups.yahoo.com/group/booksbybibin/
comments about this book on
www.booksbybibin.blogspot.comL293D INTERFACING WITH AVR
L293D used as H-bridge to drive DC motor. It can drive a bipolar stepper motor.Here are the pictures for connecting L293D to AVR.
This figure shows connecting a motor to L293D. Here I applied 12V from adapter to the pin8 of L293D. I used a normal 12V,250mA DC motor.
/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN L293D AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define L293D_DDR DDRC
#define L293D_PORT PORTC
#define Pin_1 0
#define Pin_2 1
#define L293D_Mask 0x03
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN INPUT AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define Input_DDR DDRD
#define Input_PIN PIND
#define IPin_1 6
#define IPin_2 5
#define Input_Mask 0x60
/*----------------------------------------------------------------
-----------------FUNCTIONS---------------------------------------
-----------------------------------------------------------------*/
void Init_Ports(void);
void forward(void);
void backward(void);
void brake(void);
void stop(void);
void main()
{
unsigned int a,b;
Init_Ports();
do
{
a=((Input_PIN&_BV(IPin_1))>>(IPin_1));
b=((Input_PIN&_BV(IPin_2))>>(IPin_2));
if(a==1&&b==1) brake();
if(a==0&&b==0) stop();
if(a==0&&b==1) forward();
if(a==1&&b==0) backward();
}
while(1);
}
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
L293D_DDR|=(_BV(Pin_1 )|_BV(Pin_2)); //setting pins for output
L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2));//starting motor is Off
Input_DDR&=~(_BV(5)|_BV(6));//making as input pins
}
void forward(void)
{
L293D_PORT|=_BV(Pin_1 );
L293D_PORT&=~_BV(Pin_2);
}
void backward(void)
{
L293D_PORT|=_BV(Pin_2);
L293D_PORT&=~_BV(Pin_1 );
}
void brake(void)
{
L293D_PORT|=(_BV(Pin_2)|_BV(Pin_1 ));
}
void stop(void)
{
L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2));
}
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN L293D AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define L293D_DDR DDRC
#define L293D_PORT PORTC
#define Pin_1 0
#define Pin_2 1
#define L293D_Mask 0x03
/*----------------------------------------------------------------
-------------CONNECTION BETWEEN INPUT AND ATMEGA32---------------
-----------------------------------------------------------------*/
#define Input_DDR DDRD
#define Input_PIN PIND
#define IPin_1 6
#define IPin_2 5
#define Input_Mask 0x60
Here I am using PORTC to connect to L293D and PORTD for input pins to test Hbridge.
L293D_Mask,Input_Mask is used so that remaining pins of the port can be
used for other purposes.
/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
L293D_DDR|=(_BV(Pin_1 )|_BV(Pin_2)); //setting pins for output
L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2));//starting motor is Off
Input_DDR&=~(_BV(5)|_BV(6));//making as input pins
}
Here the L293 DDR is used for ouput and L293D_PORT is set LOW to stop motor at the starting.
a=((Input_PIN&_BV(IPin_1))>>(IPin_1));
b=((Input_PIN&_BV(IPin_2))>>(IPin_2));
These statements give a,b= 0 or 1. I used right shifting operation here. Now you can drive a robot using L293D.
INPUTS: D6, D5(the motor will rotate forward,backward,stop, brake according to these two inputs)
intelnside- 11-26-2006
if i'm doing using voice to control the dc motor , izzit possible using mega32? and can give me some guidance?
yogi- 11-30-2006
first of all AVR do not have voice decoding encoding or such type of capabalities.
If you want to do voice+stepper driving, first get speech to text programm in VB(u can get in net or so) and use parallel port to control the motor.
Tell what is ur exact idea
intelnside- 12-03-2006
acutally i'm doing the voice recogniton car., using atmel mega32 to control, but the code making me headche, i'm using C program to do it.
yogi- 12-06-2006
what is the need of atmega32. u just use vb or java for voice recognition and parallel port for giving control to the robot. Read about parallel port then u can do it easily
put your code here and tell what error has occured then i can help you. Giver your schematic and program so that i can explain you better
Bibin John
www.bibinjohn.tk