Challenge

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

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

#define rs_high() PORTC |=(1<<5); //Reset pin of LCD is high 
#define rs_low() PORTC &= ~(1<<5); //Reset pin of LCD is low
#define en_high() PORTC |= (1<<3); //Enable pin of LCD is high
#define en_low() PORTC &= ~(1<<3); //Enable pin of LCD is low
#define lcdport PORTA


#define SW_2 (!(PIND & 0x20)) //Switch 1 is defined
#define SW_1 (!(PIND & 0x08)) // Switch 2 is defined
int i = 0;
int j = 0;

void lcd_command(unsigned char cmd) //lcd command function
{
	
	lcdport = cmd;
	rs_low();
	en_high();
	_delay_ms(1);
	en_low();
}
void lcd_data(unsigned char dat) //lcd data function
{
	
	lcdport = dat;
	rs_high();
	en_high();
	_delay_ms(1);
	en_low();
}

void lcd_init() //lcd initialization function
{
	
	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);
	
}

void cursorpos(char x, char y) //lcd set cursor function
{
	
	if( (x < 1 || x > 2)  || (y < 1 || y > 16) )
	{
		x = 1;
		y = 1;
	}
	if(x == 1)
	lcd_command(0x7F+y);
	else
	lcd_command(0xBF+y);
	
}

void lcd_print(char row, char coloumn, unsigned int value, int digits) //lcd function to set row coloum and string to print
{
	unsigned int temp;
	unsigned int unit;
	unsigned int tens;
	unsigned int hundred;
	unsigned int thousand;
	unsigned int million;
	
	unsigned char flag=0;
	
	if(row==0||coloumn==0)
	{
		cursorpos(1,1);
	}
	else
	{
		cursorpos(row,coloumn);
		
	}
	if(digits==5|| flag==1)
	{
		
		million=value/10000+48;
		lcd_data(million);
		flag=1;
	}
	if(digits==4 || flag==1)
	{
		
		temp = value/1000;
		thousand = temp%10 +48;
		lcd_data(thousand);
		flag=1;
	}
	if(digits==3 || flag==1)
	{
		
		temp = value/100;
		hundred = temp%10 +48;
		lcd_data(hundred);
		flag=1;
	}
	if(digits==2 || flag==1)
	{
		
		temp = value/10;
		tens= temp%10 +48;
		lcd_data(tens);
		flag=1;
	}
	if(digits==1 || flag==1)
	{
		
		unit = value%10 + 48;
		lcd_data(unit);
	}
	if(digits>5)
	{
		lcd_data('e');
	}
}
void lcd_out(char x,char y,char *str)
{
	
	cursorpos(x,y);
	while(*str)
	{
		lcd_data(*str);
		str++;
	}
}




int main(void)
{
	DDRA = 0xFF; //Defining PORTA as OUTPUT
	DDRB = 0xFF; //Defining PORTB as OUTPUT
	DDRD &= ~(1<<3); //Defining PORTD as INPUT
	PORTD |= (1<<3); //Enabling pull up resistor for switch 1
	PORTD |= (1<<5); //Enabling pull up resistor for switch 2
	TCCR0 = 0x71; //Setting Tiner counter 0 register
	lcd_init(); // Initializing the LCD
	lcd_out(1,1,"Motor off");
	while (1)
	{
		
		if (SW_1)
		{
			lcd_out(1,1,"Motor forward");
			i++;
			switch (i)
			{
			case 1 : OCR0 = 250;
			         PORTB |= (1<<1);
			         PORTB &= ~(1<<0);
					 lcd_out(2,1,"Duty cycle 20");
					 _delay_ms(2000);
				     break;
			case 2 : OCR0 = 187;
			         PORTB |= (1<<1);
			         PORTB &= ~(1<<0);
					 lcd_out(2,1,"Duty cycle 40");
					 _delay_ms(2000);
			         break;
			case 3 : OCR0 = 124;
			         PORTB |= (1<<1);
			         PORTB &= ~(1<<0);
					 lcd_out(2,1,"Duty cycle 60");
			         _delay_ms(2000);
			         break;
			case 4 : OCR0 = 61;
			         PORTB |= (1<<1);
			         PORTB &= ~(1<<0);
					 lcd_out(2,1,"Duty cycle 80");
			         _delay_ms(2000);
			         break;
			case 5 : OCR0 = 0;
			         PORTB |= (1<<1);
			         PORTB &= ~(1<<0);
					 lcd_out(2,1,"Duty cycle 100");
			         _delay_ms(2000);
			         break;
					 
			
			}
		}
	
	if (SW_2)
	{
		lcd_out(1,1,"Motor reverse");
		j++;
		switch (j)
		{
			case 1 : OCR0 = 250;
			         PORTB |= (1<<0);
			         PORTB &= ~(1<<1);
					 lcd_out(2,1,"Duty cycle 20");
			         _delay_ms(2000);
			         break;
			case 2 : OCR0 = 187;
			         PORTB |= (1<<0);
			         PORTB &= ~(1<<1);
					 lcd_out(2,1,"Duty cycle 40");
			         _delay_ms(2000);
			          break;
			case 3 : OCR0 = 124;
			         PORTB |= (1<<0);
			         PORTB &= ~(1<<1);
					 lcd_out(2,1,"Duty cycle 60");
			         _delay_ms(2000);
			         break;
			case 4 : OCR0 = 61;
			         PORTB |= (1<<0);
			         PORTB &= ~(1<<1);
					 lcd_out(2,1,"Duty cycle 80");
			         _delay_ms(2000);
			         break;
			case 5 : OCR0 = 0;
			         PORTB |= (1<<0);
			         PORTB &= ~(1<<1);
					 lcd_out(2,1,"Duty cycle 100");
			         _delay_ms(2000);
			         break;
			
			
		}

		
	
			
		}
		
	}
}

Output:

1)When No switch pressed

2)When Switch 1 pressed 2 times.

3)When Switch 2 pressed 3 times.

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.