Challenge

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

/*

* main.c

*

* Created: 12/31/2023 2:12:57 PM

* Author: GOURAV SANKADIA

*/

#include <avr/io.h>

#define F_CPU 16000000L

#include<util/delay.h> //timer delay

//********//lcd rs and en declaration//**********

#define rs_high() PORTA |=(1<<2);

#define rs_low() PORTA &= ~(1<<2);

#define en_high() PORTA |= (1<<3);

#define en_low() PORTA &= ~(1<<3);

//*****lcd data pin configuration//******

#define lcdport PORTB

//******lcd in command mode//*********

//#include<avr/interrupt.h>

void lcd_command(unsigned char cmd)

{

lcdport = cmd;

rs_low();

en_high();

_delay_ms(1);

en_low();

}

//**********lcd in data mode//*******

void lcd_data(unsigned char data)

{

lcdport = data;

rs_high();

en_high();

_delay_ms(1);

en_low();

}

//********initialisation of lcd//********

void lcd_init()

{

DDRA=0xFF;

DDRB=0xFF;

lcd_command(0x38);_delay_ms(5);//2 lines

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

lcd_command(0x01);_delay_ms(5);//clearscreen

lcd_command(0x0c);_delay_ms(5);//display on

lcd_command(0x06);_delay_ms(5);//increament cursor

lcd_command(0x80);_delay_ms(5);//force cursor begin to first line

lcd_command(0xC0);_delay_ms(5);//force cursor to second line

}

//********cursor position//*********

void cursorpos(char x, char y)//row=x,column=y

{

if( (x < 1 || x > 2) || (y < 1 || y > 16) )

{

x = 1;

y = 1;

}

if(x == 1)

lcd_command(0x80+y);

else

lcd_command(0xC0+y);

}

//******lcd print function******

void lcd_print(char row, char coloumn, float value)

{

if(row==0||coloumn==0)

{

cursorpos(1,1);

}

else

{

cursorpos(row,coloumn);

}

}

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

{

cursorpos(x,y);

while(*str)

{

lcd_data(*str);

str++;

}

}

int main(void)

{

DDRC= 0xff;

while(1)

{

int i;

i = (PIND&0b00000100);

if(i==0b00000100)

{

lcd_init();

lcd_out(1,1,"Motor is on");

PORTC=0b00000001;

_delay_ms(5);

PORTC=0b00000010;

_delay_ms(5);

}

else if(i==0b00000000)

{

lcd_init();

lcd_out(1,1,"Motor is Off");

PORTC=0b00000000;

}

}

}

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.