Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Aim : Controlling a DC motor using PWM and Monitoring Its Running Status.
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.
Code -
#include <avr/io.h> //using I/O of avr
#define F_CPU 1600000UL //define CPU/crystal frequency
#include <util/delay.h> //used for delay
#include <avr/interrupt.h> //using for external interrupt
#define sw_dir (PIND &(1<<1)) //declared as switch for direction control
#define sw_dspd (PIND &(1<<2)) // declare as switch for speed control
#define pw (PINC &(1<<3)) // port c pin 3 as ON OFF button
#define lcd_port PORTB
#define RS_high PORTD |=(1<<5) //D5 for resister select
#define RS_low PORTD &=~ (1<<5)
#define EN_high PORTD |=(1<<6) //D6 for enabling
#define EN_low PORTD &=~ (1<<6)
unsigned char i;
void lcd_cmd(char cmd)
{
lcd_port=(cmd & 0xf0); //4 bit LCD mode
RS_low;
EN_low;
_delay_ms (100); //after 1ms disable LCD
EN_high;
lcd_port=((cmd<<4) & (0xf0));
RS_low;
EN_low;
_delay_ms (100);
EN_high;
}
void lcd_data (char data) //send data for display
{
lcd_port=(data & 0xf0);
RS_high;
EN_low;
_delay_ms (100);
EN_high;
lcd_port=((data<<4) & (0xf0));
RS_high; // acces the data resister
EN_low;
_delay_ms (100);
EN_high;
}
void lcd_init()
{
lcd_cmd(0x02); _delay_ms(500);
lcd_cmd(0x01); _delay_ms(500);
lcd_cmd(0x0C); _delay_ms(500);
lcd_cmd(0x06); _delay_ms(500);
lcd_cmd(0x80); _delay_ms(500);
lcd_cmd(0x28); _delay_ms(500);
}
void cursorpos (char x, char y)
{
if ((x<1 || x>2) || (y>16) || (y<1)) //check 2rows and 16 columns
{
x=1;
y=1;
}
if(x==1)
{
lcd_cmd(0x7f+y); //starting 1st line with hexa decimal values
}
else
{
lcd_cmd(0xBF+y);
}
}
void lcd_print(char row, char coloumn, unsigned int value, int digits)
{
unsigned int temp;
unsigned int unit;
unsigned int tens;
unsigned int hundred;
unsigned int thousand;
unsigned int million;
unsigned char flag=0;
if(row==0||coloumn==0) //command for row and column
{
cursorpos(1,1);
}
else
{
cursorpos(row,coloumn);
}
if(digits==5 || flag==1)
{
million=value/10000+48;
lcd_data(million);
flag=1;
}
if(digits==4 || flag==1)
{
temp = value/1000;
thousand = temp%10 + 48;
lcd_data(thousand);
flag=1;
}
if(digits==3 || flag==1)
{
temp = value/100;
hundred = temp%10 + 48;
lcd_data(hundred);
flag=1;
}
if(digits==2 || flag==1)
{
temp = value/10;
tens = temp%10 + 48;
lcd_data(tens);
flag=1;
}
if(digits==1 || flag==1)
{
unit = value%10 + 48;
lcd_data(unit);
}
if(digits>>5)
{
lcd_data('e');
}
}
void lcd_out (char x, char y, char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}
int adc_read(char channel)
{
unsigned int result,a;
ADMUX=channel;
ADCSRA=0x80; //control status resister to enable the adc conversion
ADCH=0X00;
ADCL=0X00;
ADCSRA|=(1<<6);
while ((ADCSRA & 0X40)!=0);
a=ADCL;
result=ADCH; //store higher values
result=result<<8;
result=result|a;
return result;
}
int main(void)
{
DDRB=(0xFF); // PORTB as output port
DDRA |= ((1<<0) || (1<<1)); //A0 & A1 as output
DDRD&=~(1<<1);
DDRD&=~(1<<2);
DDRD|=(1<<5);
DDRD|=(1<<6);
DDRC&=~(1<<3);
PORTD=(1<<2);
PORTC|=(1<<3);
i=64;
TCCR0=0x61; // non inverting & PWM phase correction mode
GICR=(1<<INT0);
MCUCR=0x02; //interrupt with negative edge trigger
sei();
lcd_init(); //initialized LCD
while (1)
{
while(pw==0) //continue till button is Closed
{
if (sw_dir==0)
{
PORTA|= (1<<0); //0b00000001; // 0 1 to 1A and 2A pins to rotate motor clockwise
lcd_out(1,1,"Motor is Running");
lcd_out(2,1,"Clockwise");
}
else
{
PORTA|=(1<<1); //0b00000010; // 1 0 to 1A and 2A pins to rotate motor anticlockwise
lcd_out(1,1,"Motor is Running");
lcd_out(2,1,"Anti-Clockwise");
}
OCR0=i;
}
PORTA=0x00; // 0 0 to stop motor
lcd_out(1,1,"Motor is OFF");
lcd_out(2,1,"Caution");
}
}
ISR(INT0_vect)
{
i=i+20;
}
Buid and Run the code in Microchip Studio -
Simulation Output -
1) when power switch is off & motor is also off
2) When power switch is clossed and motor runnung clockwise
3) When Direction control switch is clossed and motor running anti-clockwise
