Challenge

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

Embedded Offline Batch-2

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

#define sw_dir (PIND&(1<<1))
#define sw_spd (PIND&(1<<2))
#define pw (PINC&(1<<3))
#define RS_high PORTD |= (1<<5)
#define RS_low PORTD &=~ (1<<5)
#define EN_high PORTD |= (1<<6)
#define EN_low PORTD &=~ (1<<6)
#define lcdport PORTB

void lcd_command(unsigned char cmd);
void lcd_data(unsigned char dat);
void lcd_init();
void cursorpos(char X,char Y);
void lcd_print(char row,char coloumn,unsigned int value,int digits);
void lcd_out(char x,char y,char *str);
int adc_read(char channel);
unsigned char i;


int main(void)
{
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);
i=64;
TCCR0=0x61;
GICR=(1<<INT0);
MCUCR=0x02;
sei();
lcd_init();

while (1)
{
while(pw==0)
{
if(sw_dir==0)
{
PORTA=0b00000001;
lcd_out(1,1,"MOTOR IS ON");
lcd_out(2,3,"clockwise");
}
else
{
PORTA=0b00000010;
lcd_out(1,1,"MOTOR IS OFF");
lcd_out(2,3,"anti clockwise");
}
OCR0=i;
}
PORTA=0x00;
lcd_out(1,1,"motor is off");
lcd_out(2,3,"caution");
}
}
ISR(INT0_vect)
{
i=i+20;
}
void lcd_command(unsigned char cmd)
{
lcdport=(cmd&0xf0);
RS_low;
EN_low;
_delay_ms(1);
EN_high;
lcdport=((cmd<<4)&(0xf0));
RS_low;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_data(unsigned char dat)
{
lcdport=(dat &0xf0);
RS_high;
EN_low;
_delay_ms(1);
EN_high;
lcdport=(dat<< 4)&(0xf0);
RS_high;
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_init()
{
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);
lcd_command(0x28);_delay_ms(5);

}
void cursorpos(char x,char y)
{
if((x<1||x>2)||(y>16||y<1))
{
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++;
}
}
int adc_read(char channel)
{
unsigned int a,result;
ADMUX =channel;
ADCSRA=0x80;
ADCH = 0x00;
ADCL =0x00;
ADCSRA|=(1<<6);
while((ADCSRA & 0x40)!=0);
a=ADCL;
result =ADCH;
result =result<<8;
result =result | a;
return result;

}
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)
{
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/10000;
thousand =temp%10+48;
lcd_data(thousand);
flag=1;
}
}

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.