DC Motor Control using AVR (l293d)

August 17, 2009 by admin · 4 Comments
Filed under: Microcontrollers 

DC motor direction Control Using L293d and AVR


Hello all electro friends…

This is another tutorial for microcontroller interfacing series. This post is all about how to interface/control a simple DC motor using microcontrollers. Controlling a DC motor is nothing but controlling the direction and speed of a motor. It is very necessary to go through motor controlling concept, if you are designing an autonomous robot.

How DC Motor works ???

Lets start with how actually DC motor runs. Direction control of a DC motor is very simple, just reverse the polarity, means every DC motor has two terminals out. When we apply DC voltage with proper current to a motor, it rotates in a particular direction but when we reverse the connection of voltage between two terminals, motor rotates in another direction.

motor

Controlling using Micro Controllers !!!

I think you are now familiar how to change the direction of DC motor. Now let us consider how to control motor using Microcontroller provided:

1. Microcontroller provides us only digital logic (1 or a 0).

2. We cant provide polarity from microcontroller.

3. We cant connect motors to Controller as mostly motors runs on volatage higher that +5V, and motors demands high current (depends).

Now the solution to above limitations is use of a “H Bridge”.

basic-bridgeIt is a circuit which allows motor rotation in both directions. From four terminals of H bridge you can control a DC motor.

Using L293d Dual half H Bridge …

We can make our own H bridge using transistors but it will be better if we use a ready made IC named as L293d, its a dual half H brige IC.

ckt_mtr

Motors can be driven in clockwise direction or anti-clockwise direction according to our requirements. To do so we first need to connect motors and find out the commands we need to give to our microcontroller to perform the specific rotation.

Suppose we connect a DC motor to PORTC of AVR through L293d. First of all we need to connect L293D to PORTC. Lets use the bits C7 and C6 for connecting this motor, as shown in the diagram below.

l293d

Code WinAVR-GCC

Here is the code in WinAVR to drive this motor.

#include<avr/io.h>

#include<util/delay.h>

int main(void)

{DDRD=0x00;     // PORTD declared as input port

PORTD=0xFF;    // PORTD Pull Ups enabled initialized

DDRC=0XFF;       // PORTC declared as output port

PORTC=0x00;     // PORTC initialized

while(1)

{

if(!(PIND&0x01))            // PIN D0 is used as input

{

_delay_ms(500);    // Debounce time

while(!(PIND&0x01));

PORTC=0b00000010;       // motor rotates in one particular direction

}

else if(!(PIND&0x02))      // PIN D1 is used as another input

{

_delay_ms(500);  // debounce time

while(!(PIND&0x02));

PORTC=0b00000001;        // motor rotates in another direction

}

else

{

PORTC=0x00;           // Stop motor

}

}

}

we can drive a maximum of two DC motor and One stepper motor using  one L293d.

Decision Table will look like

IN1     IN2                  Motor1

0           1                      Rotates in one direction

1            0                    Rotates in other direction

Similar is true for another motor connected to Out3 and Out4 of L293d and can be controled through IN3 and IN4.

This is all about controlling direction of DC motor using L293d and AVR. Next we will try to explain Speed control using PWM.

For any kind of query or suggestion tell me :  sonal@electroons.com

Downloads-

L293d Datasheet

AVR code

LCD interfacing 8051 Microcontrollers 4-bit

August 16, 2009 by admin · Leave a Comment
Filed under: Microcontrollers 

Interfacing Standard Alphanumeric LCD using 4 bit mode in 8051 MCU

flag

In this article i will explain how to interface a standard 16×2 or 20×4 alphanumeric LCD to 8051 microcontrollers using 4 bit data mode. “LCD.h” file is available for free download from here.

LCD is the most common display device used by the designer for testing purpose and for displaying real time or stored data. It is very easy to drive a LCD module using a microcontroller.

Table below shows the pin details of LCD (16 pins)

lcd pins electroons.com

Circuit Diagram shows Connection of LCD to any PORT of Microcontroller.

8051lcd

While using LCD in 4 bit data mode it saves 4 bits of our total GPIO lines, that’s why it is most commonly used. MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left.

Below is the code which is compiled usiong Keil uVision3 Compiler.

/****************************************************************

File name : LCD.H for driving LCD in 4bit mode

Auther: Anupam Dubey (dubey.anupam@gmail.com)

*****************************************************************/

#define LCDPORT P2

#define RS P2_0

#define RW P2_1

#define E  P2_2

bit  status=0
#define lcd_delay 60

void delay(unsigned int j)

{

unsigned int i,k;

for(i=0;i<j;i++)

{

for(k=0;k<100;k++);

}

}

void _lcd_init_write(unsigned char a)

{

RS = 0;

RW = 0;

LCDPORT=a;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_com(unsigned char a){

unsigned char temp;

if(status){

status=0;

 goto a;

 }

RS=0;

a:

RW=0;

temp=a;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

temp=a<<4;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_data(unsigned char a){

status=1;

RS=1;

lcd_com(a);

}

void lcd_init(void){

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x20);

delay(lcd_delay);

lcd_com(0x28);

delay(lcd_delay);

lcd_com(4);

delay(lcd_delay);

lcd_com(0x85);

delay(lcd_delay);

lcd_com(6);

delay(lcd_delay);

lcd_com(1);

delay(lcd_delay);

}

void lcd_puts(char *aaa)

{

unsigned int i=0;

for(;aaa[i]!=0;i++)lcd_data(aaa[i]);

}

#define LCDPORT P2
#define RS P2_0
#define RW P2_1
#define E  P2_2
bit  status=0;
#define lcd_delay 60
void delay(unsigned int j)
{
unsigned int i,k;
for(i=0;i<j;i++)
{
for(k=0;k<100;k++);
}
}
void _lcd_init_write(unsigned char a)
{
RS = 0;
RW = 0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_com(unsigned char a){
unsigned char temp;
if(status){
status=0;
goto a;
}
RS=0;
a:
RW=0;
temp=a;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
temp=a<<4;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_data(unsigned char a){
status=1;
RS=1;
lcd_com(a);
}
void lcd_init(void){
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x20);
delay(lcd_delay);
lcd_com(0x28);
delay(lcd_delay);
lcd_com(4);
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6);
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
void lcd_puts(char *aaa)
{
unsigned int i=0;
for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
}

Download lcd.h Here

Next Page »