Project 1 - Controlling a DC motor using PWM and monitoring its Running status
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…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.
code for this project is:
#include <avr/io.h> //using I/o register of AVR
#define F_CPU 16000000L //define cpu frequency
#include"avr/interrupt.h" //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_spad (PIND&(1<<2)) //d2 pin is declared as push button for speed control
#define pw (PINC&(1<<3)) //c3 is declared as push buttton for power on/off
#define lcd_port PORTB //portb are dedicated for lcd data
#define RS_high PORTD|=(1<<5) //d5 for RS
#define RS_LOW PORTD&=~(1<<5)
#define EN_high PORTD|=(1<<6) //d6 for EN
#define EN_LOW PORTD&=~(1<<6)
unsigned char i; //variable declared to increase 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); //masking 4 bits for 4 bit LCD mode
RS_high;
EN_high;
_delay_ms(1);
EN_high;
lcd_port=((data<<4)&(0xf0));
RS_high;
EN_high;
_delay_ms(1);
EN_high;
}
void lcd_init()
{
lcd_cmd(0x02);_delay_ms(5); //return cursor to home
lcd_cmd(0x01);_delay_ms(5); //clear screen
lcd_cmd(0x0C);_delay_ms(5); //display on/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 lcd_out(char x,char y,char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str ++;
}
}
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);
}
}
int main()
{
DDRB=0xFF; //declareing all 8 pins as output
DDRA|=((1<<0)|(1<<1)); //declareing A0 A1 pins as output
DDRD &=(1<<1); //declareing D1 pins as output
DDRD &=(1<<2); //declareing D2 pins as output
DDRD |=(1<<5); //declareing D5 pins as output
DDRD |=(1<<6); //declareing D6 pins as output
DDRC &=~(1<<3); //declareing C3 pins as output
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 motor with 25% duty cycle
TCCR0=0X61; //non inverting mode,PWM phase correct mode,NO prescalars
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 power push button is released
{
if(sw_dir==0) //direction 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,"clcockwise");
}
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,"anticlcockwise");
}
OCR0=i; //as i increases 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 push and corresponding value is to be
//loaded into OCR0 in while loop

}
Expected outcomes for this project is
- 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.

