Challenge

Project 1 - Controlling a DC motor using PWM and monitoring its Running status

LIVE BATCH (PREETI)

Question 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.

#include <avr/io.h>//PREPROCESSOR FOR AVR
#include <util/delay.h>//TIME DELAY
#define F_CPU 16000000//CPU FREQUENCY
#include "avr/interrupt.h"//EXTERNAL INTERRUPTS

#define direction (PIND &(1 << 1))//INTERRUPTS PIN PORTD1 AS HIGH
#define speed (PIND &(1 << 2))//INTERRUPTS PIN PORTD2 AS HIGH
#define power (PINC &(1 << 3))//INTERRUPTS PIN PORTC2 AS HIGH

#define lcd_port PORTB//LCD PORTB FOR COMMUNICATION

#define RS_high PORTD |= (1<<5)//REGISTER PIN PORTD5 AS HIGH
#define RS_low PORTD &=~ (1<<5)//REGISTER PIN PORTD5 AS LOW
#define EN_high PORTD |= (1<<6)//ENABLE INPUT PIN PORTD6 AS HIGH
#define EN_low PORTD &=~ (1<<6)//ENABLE INPUT PIN PORTD6 AS LOW

unsigned char i;//UNSIGNED CHAR RANGE 0-255,DECLARING AS I
void lcd_cmd(char cmd)//ASSINGING LCD DISPLAY COMMAND
{
lcd_port = (cmd & 0xf0);//MASKING 4 BITS FOR 4 BITS LCD MODE
RS_low;
EN_low;
_delay_ms(1);
EN_high;//ENABLE THE LCD
lcd_port = ((cmd << 4)&(0xf0));//LCD PORT
RS_low;
EN_low;
_delay_ms(1);
EN_high;//ENABLE THE LCD
}
void lcd_data(char data)//ASSIGING DATA TO LCD
{
lcd_port = (data & 0xf0);//MASKING 4 BITS FOR 4 BITS LCD MODE
RS_high;
EN_low;
_delay_ms(1);
EN_high;//ENABLE THE LCD
lcd_port = ((data << 4)&(0xf0));//LCD PORT
RS_high;
EN_low;
_delay_ms(1);
EN_high;//ENABLE THE LCD
}
void lcd_init()//INITIALIZE LCD COMMAND
{
lcd_cmd(0x02);//MOVE THE CURSOR RETURN TO HOME
_delay_ms(5);
lcd_cmd(0x01);//CLEAR DISPLAY
_delay_ms(5);
lcd_cmd(0x0C);//DISPLAY ON,CURSOR OFF
_delay_ms(5);
lcd_cmd(0x06);//INCREMENT CURSOR
_delay_ms(5);
lcd_cmd(0x80);//FORCE THE CURSOR BEGIN TO FIRST LINE
_delay_ms(5);
lcd_cmd(0x28);//8 BIT,2 LINE
_delay_ms(5);
}

void cursorpos(char x, char y)//FUNCTION WITH ARGUMENTS WITH RETURN VALUES
{
if((x<1||x>2||y>16||y<1))//FOR CHECKING ROWS AND COLUMNS
{
x = 1;//X AS ROWS
y = 1;//Y AS COLUMNS
}
if(x==1)
{
lcd_cmd(0x7f+y);//LCD RECEIVES THE HEXA-DECIMAL VALUE WITH Y VARIABLES
}
else//(X==2)
{
lcd_cmd(0xBF+y);//LCD RECEIVES THE HEXA-DECIMAL VALUE WITH Y VARIABLES
}
}
void lcd_out(char x, char y, char *str)//CHECKING THE WHICH POSITION TO PRINT
{
cursorpos(x,y);//CALLING FUNCTION
while(*str)
{
lcd_data(*str);
str++;
}
}
int main()//MAIN FUNCTION
{

DDRB = 0xFF;//ENABLE ALL PORTB PINS
DDRA |= ((1<<0)|(1<<1));//ENABLE ANALOG PINS 0 & 1 FOR OUTPUTS
DDRD &=~(1<<1);//ENABLE PORTD D1 INPUT
DDRD &=~(1<<2);//ENABLE PORTD D2 INPUT
DDRD |=(1<<5);//ENABLE PORT D5 AS OUTPUT
DDRD |=(1<<6);//ENABLE PORT D6 AS OUTPUT
DDRC &=~(1<<3);//ENABLE PORTC C3 AS INPUT
PORTD|=(1<<2);//ENABLE PULLUP REGISTER PORTD D2 HIGH ONLY
PORTC|=(1<<3);//ENABLE PULLUP REGISTER PORTC C3 HIGH ONLY
i=128;//INITIALIZING i=128 ,WE ARE RUNNING MOTOR WITH 50% DUTY CYCLE
TCCR0=0x61;//NON INVERTING MODE & NO PRESCALAR
GICR = (1<<INT0);//ENABLE INTERRUPTS
MCUCR=0x02;//TRIGGERED AS NEGATIVE INTERRUPT
sei();//ENABLE GLOBAL INTERUPT
lcd_init();//INITIALIZING LCD
while(1)
{
while(power==0)//WHILE POWER IS OFF CONDITION
{
if(direction==0)//DIRECTION IS CHANGED,WHEN SET TO 1
{
PORTA=0b00000001;//PORTA0 REPRESENTS THE L293(A1 A2) AS MOTOR IS RUNNING CLOCKWISE
lcd_out(1,1,"Motor is Running");//DISPLAY 1ST ROW / 1ST COLUMN IN LCD
lcd_out(2,2," Clockwise ");//PRINTING 2ND ROW / 2ND COLUMN IN LCD
}
else
{
PORTA=0b00000010;//PORTA1 REPRESENTS THE L293(A1 A2) AS MOTOR IS RUNNING ANTI-CLOCKWISE
lcd_out(1,1,"Motor is Running");//DISPLAY 1ST ROW / 1ST COLUMN IN LCD
lcd_out(2,2, "Anti Clockwise ");//PRINTING 2ND ROW / 2ND COLUMN IN LCD
}
OCR0=i;//I VALUE IS TO BE LOADED TO OCR0
}
PORTA=0x00;//PORTA IS LOW
lcd_out(1,1,"Motor is OFF ");//PRINTING 1ST ROW / 1ST COLUMN IN LCD
lcd_out(2,2, " ");//PRINTING 2ND ROW / 2ND COLUMN IN LCD
}
}
ISR(INT0_vect)//INTERRUPT SERVICE ROUTINE
{
i=i+20;//HERE I INCREASE,DUTY CYCLE ALSO INCREASE/I INCREASE BY 20 PULSE EACH CYCLE
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.