Challenge

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

Ans:

#include <avr/io.h> // processor for AVR

#define F_CPU 16000000L // clock frequency

#include <util/delay.h> // for delay function

#define rs_low PORTD &=~(1<<1); // bit 1 of portD resister select bit low for passing command to display

#define rs_high PORTD |=(1<<1); // bit 1 of portD resister select bit high for passing data to display

#define en_low PORTD &=~(1<<2); //bit 2 of portD set low for enable low

#define en_high PORTD |=(1<<2); //bit 2 of portD set high for enable high

#define lcd_port PORTB // all pin of PORTB connect to lcd display

void lcd_cmd( char cmd)

{

lcd_port=cmd; // pass command to lcd port

rs_low; // select resister low for passing command

en_high; // enable high and after some delay enable should be low

_delay_ms(1);

en_low;

}

void lcd_data( char data)

{

lcd_port=data; // pass data to lcd port

rs_high; // select resister high for passing data

en_high; // enable high and after some delay enable should be low

_delay_ms(1);

en_low;

}

void lcd_init()

{

lcd_cmd(0x38);_delay_ms(5); // 2 lines 5x7 matrix

lcd_cmd(0x02);_delay_ms(5); // return home

lcd_cmd(0x01);_delay_ms(5); // clearing the screen

lcd_cmd(0x0C);_delay_ms(5); // display on cursor off

lcd_cmd(0x06);_delay_ms(5); //Incrementing cursor

lcd_cmd(0x80);_delay_ms(5); // force cursor being 1st line

}

void lcd_curserpos(char x, char y)

{

if(( x<1||x>2)||(y<1||y>16)) //condition will check the 2 rows and 16 columns

{

x=1;

y=1;

}

if(x==1)

lcd_cmd(0x7F+y); // cursor force to start from 1st row

else

lcd_cmd(0xBF+y);// cursor force to start from 2nd row

}

void lcd_out(char x, char y, char *str)

{

lcd_curserpos(x,y);

while(*str)

{

lcd_data(*str);

str++;

}

}

int main(void)

{

DDRB|=0xff; // configuring all portB as an output pin

DDRD|=(1<<1); //pin 1 of portD set as output

DDRD|=(1<<2); //pin 2 of portD set as output

DDRD|=(1<<5); //OC1A pin used set ad output

DDRA|=(1<<0); // pin A0 and A1 pass to motor driver to provide direction to the motor

DDRA|=(1<<1);

lcd_init(); // initializing the 16x2 lcd display

while(1)

{

TCCR1A=0b10000001; // Clear OC1A/OC1B on compare match OC1A/OC1B at TOP, fast PWM 8 bit

TCCR1B=0b00001101;// fast PWM wage generation, prescaler 1024

OCR1A=128; // 50%duty cycle

PORTA=0x01;//to run motor in clockwise direction

lcd_out(1,1,"MOTOR_CL_Dir"); // row, column, string

_delay_ms(5000);

lcd_cmd(0x01);// clearing the display

_delay_ms(5);

PORTA=0x00;

lcd_out(1,1,"MOTOR OFF");

_delay_ms(5000);

PORTA=0x02;// to run motor anti clock wise

lcd_out(1,1,"MOTOR_ACL_Dir");

_delay_ms(5000);

PORTA=0x00;

lcd_cmd(0x01); // clearing the display

_delay_ms(5);

lcd_out(1,1,"MOTOR OFF");

_delay_ms(5000);

}

}

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.