Challenge

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

#define F_CPU 16000000
#define sw (PINB&(1<<0)) // defining switch meaning as macro
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#define lcdport PORTA // defining port A as LCD Port
#define rs_high PORTD|=(1<<0) //Seting only D0 as 1 for selecting Register select into Data mode
#define rs_low PORTD&=~(1<<0) //Seting only D0 as 0 for selecting Register select into command mode
#define en_high PORTD|=(1<<1) //Seting only D1 as 1 for making enable high
#define en_low PORTD&=~(1<<1) //Seting only D1 as 0 for making enable low
unsigned char i,j;

void lcd_cmd(unsigned char cmd) //lcd_cmd function for executing LCD commands passed as argument
{
lcdport=cmd;
rs_low;
en_low;
_delay_ms(1); //1ms enable pulse is created
en_high;
}

void lcd_data(unsigned char data) //lcd_data function for passing data to LCD
{
lcdport=data;
rs_high;
en_low;
_delay_ms(1); //1ms enable pulse is created
en_high;
}

void lcd_init() //initializing the LCD for first time like
{
lcd_cmd(0x38); //lcd command to use 2 lines & 5x7 matrix
_delay_ms(5);
lcd_cmd(0x02); //lcd command to return home
_delay_ms(5);
lcd_cmd(0x01); //clear display screen
_delay_ms(5);
lcd_cmd(0x0C); //display on, cursor off
_delay_ms(5);
lcd_cmd(0x06); //shift cursor to right
_delay_ms(5);
lcd_cmd(0x80); //force cursor to beginning of 1st line
_delay_ms(5);
}

int main(void)
{
DDRB&=~(1<<0); //configuring B0 as input pin to sense direction change input
DDRC=0xFF; //configuring port c as output port
i=0x40;
OCR0=i; //initializing initial duty cycle value
TCCR0=0x63; //configuring timer0 as non iverted phase correct pwm signal generator with 490 Hz frequency
DDRB|=(1<<PB3); //configuring B3 as output pin to fed phase correct pwm signal through OC0 pin
DDRD&=~(1<<PD2); //configuring D2 as input pin to sense speed change input
GICR=(1<<INT0);
MCUCR=0x02;
sei();
DDRA=0xFF; //configuring port A as output port to send data/commnands to lcd
DDRD|=(1<<0); //configuring D0 as output pin to control RS of lcd
DDRD|=(1<<1); //configuring D1 as output pin to control enable of lcd
lcd_init(); //calling lcd_init function
while(1)
{
_delay_ms(1000);
lcd_cmd(0x38);
_delay_ms(5);
lcd_cmd(0x80);
_delay_ms(5);
unsigned char print1[]="Motor rotates";
j=0;
while(print1[j]!='\0') //printing the message in 1st line
{
lcd_data(print1[j]);
j++;
}

if(sw==0)
{
PORTC=0x01; //to make motor rotate anti clockwise
lcd_cmd(0xC0); //to start from 2nd line
_delay_ms(5);
unsigned char print2[]="anti clock wise";
j=0;
while(print2[j]!='\0') //printing the message in 2nd line
{
lcd_data(print2[j]);
j++;
}
}
else
{
PORTC=0x02; //to make motor rotate clockwise
lcd_cmd(0xC0); //to start from 2nd line
_delay_ms(5);
unsigned char print3[]="clockwise "; //printing the message in 2nd line
j=0;
while(print3[j]!='\0')
{
lcd_data(print3[j]);
j++;
}
_delay_ms(2000);
}

OCR0=i;
}
return 0;
}
ISR(INT0_vect)
{
i=(i+(0x1A)); //increasing duty cycle

lcd_cmd(0x01); //clear lcd display
_delay_ms(5);
unsigned char print4[]="Speed changed";
j=0;
while(print4[j]!='\0')
{
lcd_data(print4[j]);
j++;
}
_delay_ms(1500);
}

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.