Challenge

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.

Circuit Simulation:

Program (Microchip Studio):

/*
* Project_1_final.c
*
* Created: 16-02-2023 20:22:33
* Author : Rishabh Dev Shukla
*/

#include <avr/io.h> //for accessing the I/O registers of AVR
#define F_CPU 16000000 // Defining the CPU frequency is equal to 16MHz
#include "avr/interrupt.h" // To access the external interrupt pin
#include <util/delay.h> // Header file to give delay
#define direction_control (PIND & (1<<1)) // D1 pin is declared as input for direction control.
#define speed_control (PIND & (1<<2)) // D2 pin is declared as input for speed control.
#define power (PINC & (1<<3)) //C3 pin is declared as input for power ON/OFF

#define lcd_port PORTB //PORTB pins are used for LCD data
#define rs_high PORTD |= (1<<5) // Set High to D5 pin for LCD Register Select(RS)
#define rs_low PORTD &=~ (1<<5) // Set Low to D5 pin for LCD Register Select(RS)
#define en_high PORTD |= (1<<6) // Set High to D6 pin for LCD Enable (En)
#define en_low PORTD &=~ (1<<6) // Set Low to D6 pin for LCD Enable (En)
unsigned char i;

void lcd_cmd(char cmd) //Defining function for command mode of LCD display
{
lcd_port = (cmd & 0xf0); //4 bits for 4 bit (D7,D6,D5,D4) LCD mode
rs_low; //LCD register select low
en_low; //LCD enable low
_delay_ms(1); //delay of 100ms
en_high; //LCD enable pin high
lcd_port = ((cmd<<4) & (0xf0)); //LCD port
rs_low; //LCD register select low
en_low; //LCD enable low
_delay_ms(1); //after 100ms disable LCD
en_high; //LCD enable pin high
}

void lcd_data (char data) //Defining the function to send the data to LCD
{
lcd_port = (data & 0xf0); //Masking 4 bits for 4 bit (D7,D6,D5,D4) LCD mode
rs_high; //enabling the register select
en_low; //LCD enable low
_delay_ms(1); //delay of 100ms
en_high; //LCD enable pin high
lcd_port = ((data<<4) & (0xf0)); //giving Data to LDC 4 bits for 4 bit (D7,D6,D5,D4)
rs_high; //LCD register select high
en_low; //LCD enable pin low
_delay_ms(1); //delay of 100ms
en_high; //LCD enable pin high
}

void lcd_init() //Defining the function to initiate the LCD
{
lcd_cmd(0x28);_delay_ms(5); // To initialize LCD in 2Lines and 5x7 matrix LCD mode
lcd_cmd(0x02);_delay_ms(5); // To initialize LCD for returning home
lcd_cmd(0x01);_delay_ms(5); // To initialize LCD for clearing screen
lcd_cmd(0x0C);_delay_ms(5); // To initialize LCD for display on cursor off
lcd_cmd(0x06);_delay_ms(5); // To initialize LCD for increment cursor
lcd_cmd(0x80);_delay_ms(5); // To initialize LCD for force cursor to begin from 1st line


}

void cursor(char x, char y) //Defining the function for cursor position
{
if((x<1 || x>2) || (y>16 || y<1 )) //This condition will checks the 2 rows and 16 columns
{
x=1; //x is row start with the 1st row
y=1; //y is column start with the 1st column
}
if(x==1) //condition will checks x (row) is 1st or not
{
lcd_cmd(0x80 + y); //80Hex for starting the 1st line. y we r using means we need to start from that column}
}
else //(x==2) for 2nd row
{
lcd_cmd(0xC0 + y); //0xC0 for forcing cursor begin from the 2nd line/row.
}
}

void lcd_out(char x, char y, char *str) //Defining the function to select which position we want to print
{
cursor(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}

int main(void)
{
DDRB = 0XFF; //Initializing all of port B 8-pins as output i.e. all high/set
DDRA |= ((1<<0)|(1<<1)); //Initializing A0 & A1 pins as output.A0=1 and A1=1
DDRD &=~ (1<<1); //Initializing D1 pin as input i.e. making D1=0(low).
DDRD &=~ (1<<2); //Initializing D2 pin as input i.e. making D2=0(low).
DDRD |= (1<<5); //Initializing D5 pin as output i.e. making D5=1(high).
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 by setting to High to hold the status.
PORTC |= (1<<3); //Enabling internal pull up resistor for D3 by setting to High to hold the status.
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; //makeing the interrupt negative edge triggered
sei(); //To enable global interrupt
lcd_init(); //Initialization of LCD
while (1)
{
while(power == 0) //Continue if power switch is not pressed i.e. continue till the push_button is released.
{
if(direction_control == 0) //if the direction control push_button is released.
{
PORTA = 0b00000001; // A0=1 connected to 1A pin L293 & A1=0 connected to 2A pins L293 to rotate in clockwise direction.
lcd_out(1,1,"Motor run CLK"); //To print the statement in LCD 1st low 1st column "Motor is Running".
}
else
{
PORTA = 0b00000010; // A0=0 & A1=1 to respective 1A 2A pins to rotate in Anti-Clockwise direction.
lcd_out(1,1,"Motor-run ACLK");//To print the statement in LCD 1st low 1st column "Motor is Running".
lcd_out(2,3, "Anti-clockwise");//To print the statement in LCD 2nd low 3rd column "Anti-clockwise".
}
OCR0 = i; //i increases in ISR call by pressing push_button, 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 TURNOFF");
}
}
ISR(INT0_vect) //interrupt service routine call
{
i=i+10; // To increase the duty cycle in the steps of 10 in each push of push_button connected D1 pin//
}

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.