Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Online class live batch-9
Project:
Controlling a DC motor using PWM and monitoring its Running status
Overview of the Project:
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.
Program code:
#include <avr/io.h> //we are using the I/O registers of AVR
#define F_CPU 16000000ul //define CPU frequency
#include "avr/interrupt.h" //we are using external interrupt pin
#include <util/delay.h> //delay function
#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 // PORTB pins are 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 4 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);
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 1st 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; //Declaring all B pins as outputs
DDRA|=((1<<0)|(1<<1)); //Declaring A0 A1 pins as outputs
DDRD&=~(1<<1); //Declaring D1 pin as input
DDRD&=~(1<<2); //Declaring D2 pin as input
DDRD|=(1<<5); //Declaring D5 pin as output
DDRD|=(1<<6); //Declaring D6 pin as output
DDRC&=~(1<<3); //Declaring C3 pin as input
PORTD|=(1<<2); //Enabling internal pull up resistor for D2
PORTC|=(1<<3); //Enabling internal pull up resistor for D3
i=64; //Initializing i=64 because we are running the motor with 25% duty cycle
TCCR0=0x61; //Non inverting mode, PWM phase correct mode, NO prescalar
GICR=(1<<INT0); //Using INT0 for interrupt
MCUCR=0X02; //make interrupt negative edge triggered
sei(); //Enable global interrupt
lcd_init(); //Initialize LCD
while (1)
{
while(pw==0) //continue till the power push button is released
{
if (sw_dir==0) //if the direction control push button is released
{
PORTA=0b00000001; //1 0 to respective 1A 2A pins to rotate in clockwise direction
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"Clockwise");
}
else
{
PORTA=0b00000010; //0 1 to respective 1A 2A pins to rotate in anti clockwise direction
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"Anticlockwise");
}
OCR0=i; //As 'i' increase in ISR , The corresponding value is to be loaded into OCR0
}
PORTA=0x00; // 0 0 to respective 1A 2A pins to stop rotation
lcd_out(1,1,"Motor is Off");
lcd_out(2,3,"Caution !!!!!");
}
}
ISR(INT0_vect) //interrupt service routine
{
i=i+20; //As 'i' increases the duty cycle also increases. 'i' increases by 20 in each puss
//and the corresponding value is to be loaded into OCR0in while loop
}
Over view of Block diagram:
- We Implement the system on simulator for Controlling the DC motor using L293 motor driver.
- Displaying the running status of DC motor on the LCD display.
- 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.
Block diagram:

Motor is running clockwise direction:
- The power push button is released, its controlled direction
- Then L293 motor driver perfomed as inputs 1 0 to respective 1A 2A pins
- DC motor is running clockwise direction.
- Displaying the present running status of DC machine on LCD display.

Motor is running anti clockwise direction:
- The power push button is enabled, its controlled direction
- Then L293 motor driver perfomed as inputs is 0 1 to respective 1A 2A pins
- DC motor is running anticlockwise direction.
- Displaying the present running status of DC machine on LCD display.

DC motor OFF caution:
- As 'i' increase in ISR , The corresponding value is to be loaded into OCR0
- Then L293 motor driver perfomed as inputs is 0 0 to respective 1A 2A pins
- DC motor is OFF Caution!!!!!.
- Displaying the present running status of DC machine on LCD display.

Thank you......