Challenge

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


#include <avr/io.h>
#define F_CPU 16000000ul
#include "avr/interrupt.h"
#include <util/delay.h>
#define switch_direction (PIND&(1<<1))
#define switch_speed (PIND&(1<<2))
#define power (PINC&(1<<3))
#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 k;
void lcd_cmd(char cmd)
{
lcd_port=(cmd & 0xf0);
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 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 begin 1st line //
lcd_cmd(0x28);_delay_ms(5); // used for 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;
DDRA|=((1<<0)|(1<<1));
DDRD&=~(1<<1);
DDRD&=~(1<<2);
DDRD|=(1<<5);
DDRD|=(1<<6);
DDRC&=~(1<<3);
PORTD|=(1<<2);
PORTC|=(1<<3);
k=64;
TCCR0=0X61;
GICR=(1<<INT0); // using INT0 for interrupt//
MCUCR=0x02; // make interrupt negative edge triggered//
sei(); // Enable global interrupt //
lcd_init(); // initialize lcd//
while (1)
{
while(power==0) // continue till the power push button is released //
{
if (switch_direction==0) // if the direction control push button is released //
{
PORTA=0b00000001;
lcd_out(1,1,"Motor is Running");
lcd_out(2,2," clockwise");
}
else
{
PORTA=0b00000010;
lcd_out(1,1,"Motor is Running");
lcd_out(2,3,"anticlockwise");
}

OCR0=k;
}
PORTA=0X00; // 0 0 to respective 1A 2A pins to stop rotation//
lcd_out(1,1," Motor is OFF ");
lcd_out (2,2, " ");

}
}
ISR(INT0_vect) // interrupt service routine //
{
k=k+20; // As i increases the duty cycle also increases. i increases by 20 in each push//
} // and the corresponding value is to be loaded into OCR0 in while loop //

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.