Project 1 - Controlling a DC motor using PWM and monitoring its Running status
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:-
In this project we will learn how to interface DC motor with Atmega32. And also we will learn what L293 is and how to use IC L293 with Atmega32 Using SIMULIDE.
What is a DC Motor?
A DC motor is defined as a class of electrical motors that convert direct current electrical energy into mechanical energy.
WHAT IS L293 Motor driver?
- L293 is a most popular and less expensive built-in H-bridge in a small integrated circuit used for low current motors.
- H-bridge is a motor driving unit used to control the direction of two motors at a time either clockwise or anticlockwise direction.
- It is a16 pin IC in which pins out1, out2, out3 and out4 are connected to two motors.
- Pin EN1 & EN2 are PWM pins while IN1, IN2, IN3 & IN4 are used to provide signals to the motors.


#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#define lcdport PORTC
#define rs_high PORTD|=(1<<0);
#define rs_low PORTD &=~(1<<0);
#define en_high PORTD|=(1<<2);
#define en_low PORTD &=~(1<<2);
#define sw (PIND&(1<<1))
unsigned char i;
void lcd_cmd(unsigned char cmd){
lcdport=cmd;
rs_low;
en_low;
_delay_ms(1);
en_high;
}
void lcd_data(unsigned char data){
lcdport=data;
rs_high;
en_low;
_delay_ms(1);
en_high;
}
void lcd_init(){
lcd_cmd(0x38);_delay_ms(5);
lcd_cmd(0x02);_delay_ms(5);
lcd_cmd(0x01);_delay_ms(5);
lcd_cmd(0x0c);_delay_ms(5);
lcd_cmd(0x06);_delay_ms(5);
lcd_cmd(0x80);_delay_ms(5);
}
int main(void)
{
DDRB=0xff;// Define PORTB As an output port
DDRD|=(1<<0);//PD0 output pin for lcd reset
DDRD|=(1<<2);//PD2 output pin for lcd enable
DDRD&=~(1 << 1); // PD1 input pin for direction control
PORTD|=(1 << 1); // Activate Pull up
DDRC=0xff; // Define PORTC As an output port
i=255;
OCR0=i; // for 100% duty cycle in non inverted phase correct PWM
TCCR0=0x61; // Phase correct PWM, Non inverted 31.372Khz, 16Mhz crystal, so N=1
DDRB|=(1 << PB3); // Make PB3 o/p
while (1)
{
if(sw == 0){
PORTB=0x01; // 0000 0001 for motor clockwise rotation
lcd_init();
lcd_data('c');
lcd_data('l');
lcd_data('o');
lcd_data('c');
lcd_data('k');
lcd_data('-');
lcd_data('w');
lcd_data('i');
lcd_data('s');
lcd_data('e');
}
else{
PORTB=0x02; // 0000 0010 for motor anticlockwise rotation
lcd_init();
lcd_data('A');
lcd_data('n');
lcd_data('t');
lcd_data('i');
lcd_data('c');
lcd_data('l');
lcd_data('o');
lcd_data('c');
lcd_data('k');
}
OCR0=i;
}
}