Challenge

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

/* Project.1: Change the Dc_Motor speed using PWM and directio also with display speed on LCD display *//

#include

#include //header file

#define F_CPU 8000000UL // CPU frequency

#include

#define sw (PINB&(1<<0>

#define rs_high PORTD|=(1<<7>

#define rs_low PORTD &=~(1<<7>

#define en_high PORTD |=(1<<6>

#define en_low PORTD &=~(1<<6>

#define lcdport PORTC // port C as input to LCD

unsigned char i;

void lcd_cmd(unsigned char data) // command to LCD

{

lcdport=data;

rs_low;

en_high;

_delay_ms(5);

en_low;

}

void lcd_data(unsigned char _data) // Data to LCD

{

lcdport=_data;

rs_high;

en_high;

_delay_ms(5);

en_low;

}

lcd_cursorpos(char x,char y) // Fuction for set cursor position

{

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

{

x=1;

y=1;

}

if(x==1) {lcd_cmd(0x7F+y);}

else {lcd_cmd(0xBF+y);}

}

void lcd_print(char x,char y,char *str) // Fuction for tranffering data to LCD to print

{

lcd_cursorpos(x,y);

while(*str)

{

lcd_data(*str);

str++;

}

}

void lcd_init() // set LCD for working

{

lcd_cmd(0x38); _delay_ms(5);

lcd_cmd(0x02); _delay_ms(5);

lcd_cmd(0x01); _delay_ms(5);

lcd_cmd(0x0c); _delay_ms(5);

lcd_cmd(0x06); _delay_ms(5);

lcd_cmd(0x80); _delay_ms(5);

}

int main(void)

{

DDRB |= (1<<0>

PORTB |= (1<<0>

DDRA = (1<<0>

DDRA = (1<<1>

DDRD |=(0<<2>

PORTD |= (1<<2>

DDRB |= (1<<3>

PORTC=0xff; // port C as data and command line for LCD

lcd_init();

lcd_print(1,1,"Speed ");

TCCR0=0x64; // Register set for ADC

i=64;

OCR0=i;

GICR=(1<

MCUCR=0x02;

char buffer[10];

sei();

while(1)

{

if(sw==0) // if switch is low then Motor Rotate forward direction

{

PORTA |= (1<<1>

PORTA &=~(1<<0>

}

else // if switch is high then Motor Rotate reverse direction

{

PORTA |= (1<<0>

PORTA &=~(1<<1>

}

OCR0=i;

lcd_print(1, 8, " "); // Clear the previous value

lcd_print(1, 8, itoa(i, buffer, 10)); // Display the updated value of i

}

return 0;

}

ISR(INT0_vect)

{

i+=25;

}

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.