Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Project 1 - 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.
Goals:
- Implement a system on simulator for Controlling the DC motor using L293 motor driver.
- Displaying the running status of DC 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.
Deliverables of this project:
- Implement the system for controlling DC motor.
- Controlling the speed of the DC motor using PMW.
- Displaying the present running status of DC machine on LCD display.
Expected Outcomes:
- Implement a system on simulator to control a DC motor using PMW
- Monitoring the running status of the DC motor using L293 motor driver which allows the DC motor to drive on any direction.
- Running status i.e. on which direction the DC motor is running is to be displayed on the LCD screen.
Code :
#include <avr/io.h> //For input/output .
#define F_CPU 16000000ul //Setting 16Mhz as frequency
#include "avr/interrupt.h" //for using external interrupt pin.
#include <util/delay.h> //To enable delay functions.
#define sw_dir (PIND&(1<<1)) //D1 enabled for direction control
#define sw_spd (PIND&(1<<2)) //D2 enabled for speed controled.
#define pw (PINC&(1<<3)) //3rd pin enabled as input for on/off.
#define lcd_port PORTB //Port b is synced with lcd ports.
#define RS_high PORTD|=(1<<5) //D5 used for register select.
#define RS_low PORTD&=~(1<<5) //D5 used for RS (to clear data)
#define EN_high PORTD|=(1<<6) //D6 used for enable
#define EN_low PORTD&=~(1<<6) //D6 used to clear data
unsigned char i; //global declaration if i.
void lcd_cmd(char cmd) //functoin for commands of lcd display
{
lcd_port=(cmd&0xf0); //Making 4 bits for 4 bit LCD mode//
RS_low; //Keywords RS=0.
EN_low; //EN = 0.
_delay_ms(1); //1ms delay
EN_high; //EN = 1
lcd_port=((cmd<<4)&(0xf0)); //lcd port
RS_low; //RS = 0.
EN_low; //EN = 0
_delay_ms(1); //delay if 1ms
EN_high; //en = 1.
}
void lcd_data(char data) //Function for lcd data
{
lcd_port=(data &0xf0); //making 4 bits for bit LCD mode//
RS_high; //RS = 1.
EN_low; //EN =0
_delay_ms(1); //delay
EN_high; //EN =1
lcd_port=((data<<4)&(0xf0)); //lcd port
RS_high; //access data register
EN_low; //EN = 0
_delay_ms(1); //delay
EN_high; //en=1
}
void lcd_init() //functions to do task of lcd
{
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 bits lcd mode
}
void lcd_out(char x, char y, char *str) //For checking the poition of printing
{
cursorpos(x,y);
while (*str)
{
lcd_data(*str);
str++;
}
}
void cursorpos(char x, char y)
{
if ((x<1||x>2)||(y>16)||(y<1)) //To check the display rows and coloumns.
{
x=1; //x is row
y=1; //y is coloumn
}
if (x==1)
{
lcd_cmd(0x7f+y);//for sarting the first line
}
else
{
lcd_cmd(0xBF+y); //for starting the second line.
}
}
int main() //main function
{
DDRB=0xFF; //data registor of portb is enabled
DDRA|=((1<<0)|(1<<1)); //pin 0 and 1 of Data register of porta is enabled as output.
DDRD&=~(1<<1); //for clearing the data in 1st bit of data register d o
DDRD&=~(1<<2); //for clearing data register of 2nd bit of portd
DDRD|=(1<<5); //5th bit of DDR of port D is made as output
DDRD|=(1<<6); //6th bit as output
DDRC&=~(1<<3); //for clearing data in 3.
PORTD|=(1<<2); //2nd pin of PORTb as output
PORTC|=(1<<3); //3rd pin of portc as output
i=64; //i assigned with 64
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(); //function calling for initializing lcd
while (1)
{
while(pw==0) //continue till pushbutton is released
{
if (sw_dir==0) //if the direction control push button is released.
{
PORTA=0b00000001; //to rotate in clockwise
lcd_out(1,1,"Motor is Running"); //for printing
lcd_out(2,3,"clockwise"); //for printing
}
else
{
PORTA=0b00000010; //for anti clockwise
lcd_out(1,1,"Motor is Running"); //printing
lcd_out(2,3,"anticlockwise"); //for printing
}
OCR0=i; //assigning value of i to ocro
}
PORTA=0X00; //to stop rotation
lcd_out(1,1," Motor is OFF"); //for printing
lcd_out(2,3,"CAUTION !!!"); //for printing
};
}
ISR(INT0_vect) //isr
{
i=i+20; //if i increases, duty cycle also increases. here i increased by 20.
}
Output:
here input to PIN is 0, thus it displays motor is in off state.

here pinc is enabled. thus motor is in on state. direction switch is in off state, thus motor rotates in clockwise direction

Here the pinc is enabled thus motor is on and direction switch in in on position, thus motor rotates in anti-clockwise direction.
