Challenge

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

/*
* project 2.c
*
* Created: 28-08-2022 19:10:32
* Author : Sangeeth C Radh
*/


#include <avr/io.h> //using I/O of avr
#define F_CPU 1600000UL //define CPU/crystal frequency
#include <util/delay.h> //used for delay
#include <avr/interrupt.h> //using for external interrupt
#define sw_dir (PIND &(1<<1)) //declared as switch for direction control
#define sw_dspd (PIND &(1<<2)) // declare as switch for speed control
#define pw (PINC &(1<<3)) // port c pin 3 as ON OFF button

#define lcd PORTB
#define RS_H PORTD |=(1<<5) //D5 for resister select
#define RS_L PORTD &=~ (1<<5)
#define EN_H PORTD |=(1<<6) //D6 for enabling
#define EN_L PORTD &=~ (1<<6)
unsigned char i;


void lcd_cmd(char cmd)
{
lcd=(cmd & 0xf0); //4 bit LCD mode
RS_L;
EN_L;
_delay_ms (100); //after 1ms disable LCD
EN_H;
lcd=((cmd<<4) & (0xf0));
RS_L;
EN_L;
_delay_ms (100);
EN_H;
}


void lcd_data (char data) //send data for display
{
lcd=(data & 0xf0);
RS_H;
EN_L;
_delay_ms (100);
EN_H;
lcd=((data<<4) & (0xf0));
RS_H; // acces the data resister
EN_L;
_delay_ms (100);
EN_H;
}


void lcd_init()
{
lcd_cmd(0x02); _delay_ms(500);
lcd_cmd(0x01); _delay_ms(500);
lcd_cmd(0x0C); _delay_ms(500);
lcd_cmd(0x06); _delay_ms(500);
lcd_cmd(0x80); _delay_ms(500);
lcd_cmd(0x28); _delay_ms(500);
}


void cursorpos (char x, char y)
{
if ((x<1 || x>2) || (y>16) || (y<1)) //check 2rows and 16 columns
{
x=1;
y=1;
}
if(x==1)
{
lcd_cmd(0x7f+y); //starting 1st line with hexa decimal values
}

else
{
lcd_cmd(0xBF+y);
}

}

void lcd_print(char row, char coloumn, unsigned int value, int digits)
{
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) //command for row and column
{
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 adc_read(char channel)
{
unsigned int result,a;
ADMUX=channel;
ADCSRA=0x80; //control status resister to enable the adc conversion
ADCH=0X00;
ADCL=0X00;
ADCSRA|=(1<<6);
while ((ADCSRA & 0X40)!=0);
a=ADCL;
result=ADCH; //store higher values
result=result<<8;
result=result|a;
return result;
}

int main(void)
{
DDRB=(0xFF); // PORTB as output port
DDRA |= ((1<<0) || (1<<1)); //A0 & A1 as output
DDRD&=~(1<<1);
DDRD&=~(1<<2);
DDRD|=(1<<5);
DDRD|=(1<<6);
DDRC&=~(1<<3);
PORTD=(1<<2);
PORTC|=(1<<3);
i=64;
TCCR0=0x61; // non inverting & PWM phase correction mode
GICR=(1<<INT0);
MCUCR=0x02; //interrupt with negative edge trigger
sei();
lcd_init(); //initialized LCD

while (1)
{
while(pw==0) //continue till button is Closed
{
if (sw_dir==0)
{
PORTA|= (1<<0); //0b00000001; // 0 1 to 1A and 2A pins to rotate motor clockwise
lcd_out(1,1,"Motor Running ");
lcd_out(2,1,"Clockwise");

}
else
{
PORTA|=(1<<1); //0b00000010; // 1 0 to 1A and 2A pins to rotate motor anticlockwise
lcd_out(1,1,"Motor Running ");
lcd_out(2,1,"Anti-Clockwise");

}
OCR0=i;
}
PORTA=0x00; // 0 0 to stop motor
lcd_out(1,1,"Motor is OFF ");
}
}
ISR(INT0_vect)
{
i=i+20;

}

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.