Challenge

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

#Project 1

/*
* Project 01.c
*
* Created: 10-04-2023 11:07:02
* Author : night(Ambrose Joseph)
*/

#include <avr/io.h>
#define F_CPU 16000000L
#include <avr/interrupt.h>
#include <util/delay.h>

#define lcd PORTC
#define RSH() PORTD |=(1<<5); //rs value set as high
#define RSL() PORTD &=~(1<<5); //rs value set as low
#define ENL() PORTD &=~(1<<6); //en value set as low
#define ENH() PORTD |=(1<<6); //en value is set as high

#define direction (PIND & (1<<1)) //direction for motor
#define speed (PIND & (1<<2)) //speed of a motor
#define power_switch (PIND & (1<<7)) //power_switch of a motor

unsigned char var; //declared for duty cycle

void lcd_cmd(unsigned char cmd)
{
lcd = cmd;
RSL();
ENH();
_delay_ms(1);
ENL();
}

void lcd_data(unsigned char data)
{
lcd = data;
RSH();
ENH();
_delay_ms(1);
ENL();
}

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);
}

void cursor_pos(char x, char y)
{
if((x<1||x>2)||(y<1||y>16))
{
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)
{
cursor_pos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}

int main(void)
{
DDRC = 0xFF; //Set as output for LCD display
DDRA |=((1<<0)|(1<<1)); //Enabling pins A0 and A1 which connected to the driver for directions
DDRD&=~(1<<1);
DDRD&=~(1<<2);
DDRD|=(1<<5);
DDRD|=(1<<6);
DDRD&=~(1<<7);
PORTD|=(1<<2);
PORTD|=(1<<7);
var = 64; //25% duty cycle
TCCR0 = 0x61; //Non-inverting mode, phase corrected PWM and no pre-scalar
GICR = (1<<2)|(1<<1); //enabling interrupt
MCUCR = 0x02; //for negative edge
sei(); //Enabling global interrupt
lcd_init(); //Call function to initialize the LCD display
while (1)
{
while(power_switch==0)
{
if(direction==0)
{
PORTA=0b00000001;
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"Clockwise motion");
}
else
{
PORTA=0b00000010;
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"Anti-Clockwise m.");
}

OCR0=var;
}
PORTA=0x00;
lcd_out(1,1,"Motor is OFF");
lcd_out(2,3,"Cautions !!!");
}
}

ISR(INT0_vect)
{
var=var+10;
}

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.