Challenge

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

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

#define lcdport PORTC //PORTC AS LCD
#define rs_high() PORTD |= (1<<6); // register select data mode as high
#define rs_low() PORTD &= ~(1<<6); // register select command mode as low
#define en_high() PORTD |= (1<<5); // enabling data mode as high
#define en_low() PORTD &= ~(1<<5); // enabling command mode as low

#define direction (PIND & (1<<1)) // pin d1 is used as direction control
#define power_button (PIND & (1<<4)) // pin d4 is used as power control
#define speed (PIND & (1<<3)) // pin d2 is used as speed control
unsigned char i;

void lcd_cmd (unsigned char cmd) // function using command mode
{
lcdport=cmd;
rs_low(); // rs_low is command mode
en_high(); // enabling data as high
_delay_ms(1);
en_low(); // enabling command as low
}
void lcd_data (unsigned char data) // function using data mode
{
lcdport=data;
rs_high(); // rs_high is data mode
en_high(); // enabling data as high
_delay_ms(1);
en_low(); // enabling command as low
}
void lcd_init()
{
lcd_cmd(0x38); // 2 lines 5*7 matrix
_delay_ms(5);
lcd_cmd(0x02); //return home
_delay_ms(5);
lcd_cmd(0x01); //clear screen
_delay_ms(5);
lcd_cmd(0x0c); // Display on, Cursor off
_delay_ms(5);
lcd_cmd(0x06); // increment cursor
_delay_ms(5);

}
void cursor_set(char r, char c) // cursor position
{
if (r==1)
{

lcd_cmd(0x80); // force Cursor begin at 1st line
}
else //0xC0
{
lcd_cmd(0xC0); // force Cursor begin at 2nd line
}
}

void lcd_print(char r, char c, char *str) // row, column, string to be printed
{
cursor_set(r,c);
while(*str)
{
lcd_data(*str);
str++;
}
}

int main(void)
{

DDRC = 0xFF; // set output all PORTC pin
DDRB = 0xFF; // set output all PORTB pin
PORTB = 0xFF; // set output all PORTB pin for lcd diaplay connection
DDRA |= (1<<0) | (1<<1); // set 1 to A0 and A1 pin
DDRD &=~ (1<<4); // set input for PORTD PD4 pin
DDRD &=~ (1<<1); // set input for PORTD PD1 pin
DDRD |= (1<<5)|(1<<6); //set 1 to pin PD5 and PD6
PORTD |= (1<<1);// to enable pull up for direction button
PORTD |= (1<<4);// to enable pull up for power button
PORTD |= (1<<3); // to enable pull up for interrupt
sei(); // globally interrupt
GICR |= (1<<7);
i = 64;
TCCR0 = 0x64;
lcd_init(); // commands for lcd initialization

while(1)
{
while(power_button == 0)
{
if(direction == 0)
{
PORTA = 0x01;
lcd_cmd(0x01);
lcd_print(1,1,"Motor running");
lcd_print(2,1,"in Clockwise");
_delay_ms(500);
}
else
{
PORTA = 0x02;
lcd_cmd(0x01);
lcd_print(1,1,"Motor running");
lcd_print(2,1,"in Anticlockwise");
_delay_ms(500);
}
OCR0 = i;
}
lcd_cmd(0x01);
PORTA = 0x00;
lcd_print(1,1,"Motor OFF");
_delay_ms(500);
}
}

ISR(INT1_vect) // interrupt function
{
i = 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.