View Full Version: DC motor interfacing with AVR microcontroller(L293D H-bridge

booksbybibin >>Microcontrollers >>DC motor interfacing with AVR microcontroller(L293D H-bridge


yogi- 06-28-2006
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-booksbybibin http://groups.yahoo.com/group/booksbybibin/ comments about this book on www.booksbybibin.blogspot.com L293D 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

intelnside- 12-14-2006

i'm found the code from this website, http://instruct1.cit.cornell.edu/courses/ee476/FinalProjects/s2006/avh8_css34/avh8_css34/srecog.c but the code is controlling the servo, but now mine is controlling the DC H bridge motor. can u help me , because i got an error when modified the code. help me please...i'm already stuck alot of days. :(

yogi- 12-15-2006

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

intelnside- 12-21-2006

file:///C:/Documents%20and%20Settings/cry8383/Local%20Settings/Temporary%20Internet%20Files/Content.IE5/YT38P832/steer-hbridge%5B1%5D.jpg this is the H brige cct

Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.