Challenge

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

/*

 * project 1.c

 *

 * Created: 9/23/2023 1:23:23 PM

 * Author : Sachin

 */ 

#include

#define F_CPU 16000000L

#include

#include

#define port PORTD

#define out1 1<<0>

#define out2 1<<1>

#define LCDPORT PORTB

#define RS_LOW PORTD&=~(1<<2>

#define RS_HIGH PORTD|=(1<<2>

#define EN_LOW PORTD&=~(1<<3>

#define EN_HIGH PORTD|=(1<<3>

void motor(int a,int value)

{

OCR1A=(256/100)*value;

if (a== 1)

{

port |=(out2);

port &=~(out1);

}

if (a== 0)

{

port &=~(out2);

port &=~(out1);

}

if (a== -1)

{

port |=~(out2);

port &=~(out1);

}

}

void motor_timer0()

{

DDRD =0xff;

TCCR1A = 0b10000011;

TCCR1B = 0b00000001;

OCR1A =50;

motor(0,100);

}

void lcd_cmd(unsigned char cmd)

{

LCDPORT = cmd;

RS_LOW;

EN_LOW;

_delay_ms(1);

EN_HIGH;

}

void lcd_data(unsigned char data)

{

LCDPORT = data;

RS_HIGH;

EN_LOW;

_delay_ms(1);

EN_HIGH;

}

void lcd_init()

{

lcd_cmd(0x38);_delay_ms(5);

lcd_cmd(0x38);_delay_ms(5);

lcd_cmd(0x38);_delay_ms(5);

lcd_cmd(0x38);_delay_ms(5);

lcd_cmd(0x0c);_delay_ms(5);

lcd_cmd(0x01);_delay_ms(5);

lcd_cmd(0x06);_delay_ms(5);

lcd_cmd(0x80);_delay_ms(5);

}

void cursopos(char a,char y)

{

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

{

a=1;

y=1;

}

if(a==1)

{

lcd_cmd(0x7f+y);

}

else

{

lcd_cmd(0xbf+y);

}

}

void lcd_out(char a,char b,char *str)

{

cursorpos(a,b);

while(*str)

{

lcd_data(*str);

str++;

}

}

int main(void)

{

DDRC = 0x00;

PORTC=0xff;

motor_timer0();

lcd_init();

int pwm = 50;

while (1)

{

if((PINC & (0b000000010))==0)

{

motor(1,pwm);

lcd_cmd(0x01);

lcd_out(1,1,"motor direction");

lcd_out(2,1,"forward");

_delay_ms(250);

}

if((PINC&(0b00000010))==0)

{

motor(-1,pwm);

lcd_cmd(0x01);

lcd_out(1,1,"motor direction");

lcd_out(2,1,"reverse");

_delay_ms(250);

}

}

}

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.