Challenge

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

project 1.c
*
* Created: 16-12-2022 20:08:28
* Author : 91948
*/

#include <avr/io.h>
#define F_CPU 16000000L //frequency//
#include<util/delay.h> //delay fuction //
#include "avr/interrupt.h"
#define sw_dir (PIND&(1<<1)) // d1 pin declared push button for direction //
#define sw_spd (PIND&(1<<2)) // d2 pin declared push button for speed //
#define pw (PINC&(1<<3)) // c3 pin declared push button for power ON //
#define lcd_port PORTB
#define Rs_high PORTD|=(1<<5)
#define Rs_low PORTD &=~(1<<5)
#define EN_high PORTD|=(1<<6)
#define EN_low PORTD &=~(1<<6)
unsigned char i; // declared to incresae the duty cycle
void lcd_cmd(char cmd)
{

lcd_port=(cmd&0Xf0); // 4 bits LCD mode //
Rs_low;
EN_low;
_delay_ms(1);
EN_high;
lcd_port=((cmd<<4)&(0xf0));
Rs_low;
EN_low;
_delay_ms(1);
EN_high;

}
void lcd_data(char data)
{

lcd_port=(data &0xf0);
Rs_high;
EN_low;
_delay_ms(1);
EN_high;
lcd_port=((data<<4)&(0Xf0));
Rs_high;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_init()
{

lcd_cmd(0X02);_delay_ms(5); // return the cursor to home //
lcd_cmd(0X01);_delay_ms(5); // clear the screen //
lcd_cmd(0X0C);_delay_ms(5); // display ON cursor OFF //
lcd_cmd(0X06);_delay_ms(5); // increment cursor //
lcd_cmd(0X80);_delay_ms(5); // force cursor to begion 1st line //
lcd_cmd(0X28);_delay_ms(5);// 4 bit lcd mode //

}
void cursorpos(char x,char y)
{
if((x<1 || x>2)|| (y>16 || y<1))
{
x=1;
y=1;

}
if(x==1)
{
lcd_cmd(0x7f+y);
}
else
{
lcd_cmd(0xBF+y);

}

}
void lcd_out(char x,char y, char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;

}
}
int main()
{
DDRB=0xFF; //B pins as output //
DDRA|=((1<<0)|(1<<1)); //A0,A1 pins are output //
DDRD&=~(1<<1); // d1 pin input //
DDRD&=~(1<<2); // d2 pin input //
DDRD|=(1<<5); // d5 pin output //
DDRD|=(1<<6); // d6 pin output //
DDRC&=~(1<<3); //c3 pin input //
PORTD|=(1<<2); // enabling internal pull up resister d2 //
PORTC|=(1<<3); // enabling internal pull up resister d3 //
i=64; // initalize i=64 because we are runing the motor with 25% duty cycle //
TCCR0=0x61; // non inverting mode pwm phase correct mode ,No prescalars //
GICR=(1<<INT0); // INT0 for interrupt //
MCUCR=0X02; // make interrupt negative edge triggered //
sei(); // enable global interrupt //
lcd_init(); // initialized lcd //

/* Replace with your application code */
while (1)
{
while(pw==0) // continue till the power push buttion released //
{
if(sw_dir==0) // the direction control push buttion released
{
PORTA=0b00000001; // 1,0 to respective 1A 2Apins to rotate in anti clockwise direction //
lcd_out(1,1,"MOTOR IS RUNNING");
lcd_out(2,3,"CLOCKWISE");
}
else
{
PORTA=0b00000010; // 0,1 to respective 1A,2A pins to roate in anti clockwise direction //
lcd_out(1,1,"MOTOR IS RUNNING");
lcd_out(2,3,"ANTI CLOCKWISE");
}
OCR0=i; // i increase in ISR the corresponding value is to be loaded into OCR0 //
}
PORTA=0X00;
lcd_out(1,1,"MOTOR IS OFF"); // 0 to respective 1A,2A pins to step rotation //
lcd_out(2,3,"CAUTION !!!!");
}
}

ISR(INT0_vect) // interrupt service routine //
{
i=i+20; //i increase the duty cycle also increase ,i increase by 20 in each and the corresponding value is to be loaded //


}

out put

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.