Challenge

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

Program:

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

#define sw_dirction (PIND &(1<<5))
#define sw_speed (PIND &(1<<6))

#define power (PINC &(1<<6))

#define rs_high() PORTD |= (1<<2); //rs in LCD connected to D2
#define rs_low() PORTD &= ~(1<<2);

#define en_high() PORTD |= (1<<3);
#define en_low() PORTD &= ~(1<<3); // En in LCD connected to D3

#define lcdport PORTB
unsigned char i;

//function to write command on LCD
void lcd_command(char cmd)

{
lcdport=(cmd & 0xf0); // Masking 4 bits for 4 bit LCD mode//
rs_low();
en_low();
_delay_ms(1);
en_high();
lcdport=((cmd<<4)&(0xf0));
rs_low();
en_low();
_delay_ms(100);
en_high();
}

//function to write data on LCD
void lcd_data(char dat)
{
lcdport=(dat & 0xf0); // Masking 4 bits for 4 bit LCD mode//
rs_high();
en_low();
_delay_ms(100);
en_high();
lcdport=((dat<<4)&(0xf0));
rs_high();
en_low();
_delay_ms(100);
en_high();
}

//function for cursor position
void cursorpos (char x,char y)
{

if((x < 1 || x > 2) || (y < 1 || y > 2))
{
x = 1;
y = 1;
}
if(x==1)
lcd_command(0x7f+y);
else
lcd_command(0xBf+y);
}

void lcd_out(char x,char y,char *str)
{

cursorpos(x,y);
while(*str)
{

lcd_data(*str);
str++;
}
}
void lcd_init()
{

lcd_command(0x38);_delay_ms(5);
lcd_command(0x02);_delay_ms(5);
lcd_command(0x01);_delay_ms(5);
lcd_command(0x0C);_delay_ms(5);
lcd_command(0x06);_delay_ms(5);
lcd_command(0x80);_delay_ms(5);
}

int main(void)
{
DDRB=0xff;
DDRA=((1<<0)|(1<<1));

DDRD&=~(1<<5);
DDRD&=~(1<<6);

DDRD|=(1<<2);
DDRD|=(1<<3);

DDRC&=~(1<<6);

PORTD|=(1<<6);
PORTC|=(1<<6);

i=64;
TCCR0=0x61;
GICR=(1<<INT0);

MCUCR=0x02;
sei();
lcd_init();

while(1)
{
while(power==0)
{
if (sw_dirction==0)
{
PORTA=0b00000001;
lcd_out(1,1,"motor is running");
lcd_out(2,3,"clockWise");
}
else
{
PORTA=0b00000010;
lcd_out(1,1,"Motor is running");
lcd_out(2,3,"Anti-ClockWise");
}
OCR0=i;
}
PORTA=0X00;
lcd_out(1,1,"Motor OFF");
}
}
ISR(INT0_vect)
{
i=i+20;
}

circuit:

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.

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.