Challenge

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

/*

* 1)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.

*

* Created: 11-02-2024

* Author : Vishnu V Nair

*/

#include <avr/io.h>

#define F_CPU 8000000UL

#include <util/delay.h>

#define EN 6

#define RS 5

//Function for LCD command

void lcd_cmd(unsigned char cmd)

{

PORTD = cmd;

PORTB &= ~(1<<RS);

PORTB &= ~(1<<EN);

_delay_ms(100);

PORTB |= (1<<EN);

}

//Function for LCD command

void lcd_data(unsigned char data)

{

PORTD = data;

PORTB |= (1<<RS);

PORTB &= ~(1<<EN);

_delay_ms(100);

PORTB |= (1<<EN);

}

//Function to display string in LCD

void lcd_display(unsigned char *p)

{

while(*p != "\0")

{

lcd_data(*p);

p++;

}

}

//To initialize LCD

void lcd_init()

{

lcd_cmd(0x38); //2 line & 5*7 matrix

lcd_cmd(0x02); //Return home

lcd_cmd(0x01); //Clear display screen

lcd_cmd(0x0c); //Display ON, cursor OFF

lcd_cmd(0x06); //Increment cursor to right

lcd_cmd(0x80); //Force cursor to the begenning of first line

}

int main(void)

{

//LCD part

DDRD = 0xff; //LCD data pin

DDRB |= (1<<5); //RS pin

DDRD |= (1<<6); //EN pin

lcd_init(); //Initialize LCD

//PWM part

DDRB |= (1<<3); //Pin 3(OC0) of port B as o/p

DDRA |= (1<<1) | (1<<0); //Motor as o/p

TCCR0 = 0x61; //Phase correct & no prescalar(WGM00, COM01, CS00 -> High)

OCR0 = (90 * 255)/100; //90% duty cycle

//Push button part

DDRC |= (0<<7) | (0<<1); //Pin 7(c7) & 1(c1) of port C as i/p

PORTC |= (1<<7) | (1<<1); //Initialize pull-up resistor

while(1)

{

if((PINC & 0x80) == 0) //If Pin C7 is pressed rotate motor Clockwise

{

PORTA = 0x01; //00000001

_delay_ms(1000);

lcd_display("Clockwise");

}

if((PINC & 0x02) == 0) //If Pin C1 is pressed rotate motor Anti-Clockwise

{

PORTA = 0x02; //00000010

lcd_display("Anti-Clockwise");

_delay_ms(1000);

}

else

{

//Stops Motor

PORTA = 0x03;

_delay_ms(100);

}

}

}

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.