Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Live Session(Preeti Mam)
AIM & OBJECTIVE :
Implement a system on the simulator for controlling a DC motor using L293 motor driver, monitor its status and print the running status of motor on the LCD Display.
Key Highlights of the Project:
- The DC motor can be controlled by using L293 motor driver.
- The speed of a DC motor can be controlled by varying its input voltage, for doing this is to use PWM (Pulse Width Modulation).
- L293D IC is a typical Motor Driver IC which allows the DC motor to drive on any direction.
About L293D Driver :
The L293D is designed to provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V.
The L293D is a 16-pin Motor Driver IC which can control a set of two DC motors simultaneously in any direction. The L293D is designed to provide bidirectional drive currents of up to 600 mA (per channel) at voltages from 4.5 V to 36 V .

Source Code :
#include <avr/io.h> // For I/o registers of AVR//
#include <util/delay.h> // For the delay function //
#define F_CPU 16000000 // For the CPU frequency//
#include "avr/interrupt.h" // For using external interrupt pin//
#define sw_dir (PIND&(1<<1)) // d1 pin is declared as push button for direction control//
#define sw_spd (PIND&(1<<2)) // d2 pin is declared as push button for speed control//
#define pw (PINC&(1<<3)) // c3 is declared as push button for power on/off //
#define lcd_port PORTB // port b pins dedicated for LCD data //
#define RS_high PORTD|=(1<<5) // d5 pin is used for RS //
#define RS_low PORTD&=~(1<<5)
#define EN_high PORTD|=(1<<6) // d6 pin is used for EN //
#define EN_low PORTD&=~(1<<6)
unsigned char i; // variable declared to increase the duty cycle in PWM //
void lcd_cmd(char cmd)
{
lcd_port=(cmd & 0xf0); // Masking 4 bits for bit LCD mode //
RS_low;
EN_low;
_delay_ms(1);
EN_high;
lcd_port=((cmd<<4)&(0xf0));
RS_low;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_data(char data)
{
lcd_port=(data & 0xf0); // Masking 4 bits for 4 bit LCD mode //
RS_high;
EN_low;
_delay_ms(1);
EN_high;
lcd_port=((data<<4)&(0xf0));
RS_high;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_init()
{
lcd_cmd(0x02);_delay_ms(5); // return the cursor to home //
lcd_cmd(0x01);_delay_ms(5); // clear screen //
lcd_cmd(0x0C);_delay_ms(5); // display ON cursor off//
lcd_cmd(0x06);_delay_ms(5); // increment cursor //
lcd_cmd(0x80);_delay_ms(5); // force cursor to begin line //
lcd_cmd(0x28);_delay_ms(5); // used for 4 bit lcd mode //
}
void cursorpos(char x, char y)
{
if ((x<1||x>2)||(y>16||y<1))
{
x=1;
y=1;
}
if (x==1)
{
lcd_cmd(0x7f+y);
}
else
{
lcd_cmd(0xBF+y);
}
}
void lcd_out(char x, char y, char *str)
{
cursorpos(x,y);
while (*str)
{
lcd_data(*str);
str++;
}
}
int main()
{
DDRB=0xFF; // Configuring all B pins as outputs//
DDRA|=((1<<0)|(1<<1)); // Configuring A pins as outputs//
DDRD&=~(1<<1); // Configuring D1 pin as input//
DDRD&=~(1<<2); // Configuring D2 pin as input//
DDRD|=(1<<5); // Configuring D5 pin as output//
DDRD|=(1<<6); // Configuring D6 pin as output//
DDRC&=~(1<<3); // Configuring C3 pin as input//
PORTD|=(1<<2);
PORTC|=(1<<3);
i=64; // 25% duty cycle as per the project requirement//
TCCR0=0x61;
GICR=(1<<INT0); // Interrupt Configuration
MCUCR=0x02; // Making interrupt negative edge triggered//
sei();
lcd_init();
while(1)
{
while(pw==0)
{
if (sw_dir==0) // if the direction control push button is released//
{
PORTA=0b00000001; //For setting Pins of Port A in clockwise direction//
lcd_out(1,1,"Motor is Running");
lcd_out(2,2,"Clockwise");
}
else
{
PORTA=0b00000010; //For setting Pins of Port A in Anticlockwise direction//
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"Anticlockwise");
}
OCR0=i; // Max value of i to be loaded into OCR0 //
}
PORTA=0x00; // Configuring pins of PORT A to stop rotating//
lcd_out(1,1," Motor is OFF");
lcd_out (2,2, " ");
}
}
ISR(INT0_vect) //ISR function for incrementing the value of i by 20 every time the button is pushed
{
i=i+20;
}
OutPut on Simulide :

