Challenge

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

#include <avr/io.h> // including IO header filr for using input and output operations
#define F_CPU 16000000ul // defining the crystal frequency
#include "avr/interrupt.h" // including interrupt header file for using external interrupt
#include <util/delay.h> // including file for delay function purpose
#define port PORTD //portD connecting to motor driver
#define out1 1<<0 //portD pin-0 as input to motor driver
#define out2 1<<1 //portD pin-1 as input to motor driver
#define LCDPORT PORTB //portB pins are set for LCD data
#define RS_high PORTD |= (1<<2) //portD pin-2 is used for register select(rs)
#define RS_low PORTD &= ~(1<<2) //portD pin-2 is used for register select(rs)
#define EN_high PORTD |= (1<<3) //portD pin-3 is used for enable(en)
#define EN_low PORTD &= ~(1<<3) //portD pin-3 is used for enable(en)
void motor(int ,int ); //function prototypes
void motor_timer0();
void lcd_cmd(unsigned char );
void lcd_data(char );
void lcd_init();
void cursorpos(char , char );
void lcd_out(char , char , char*);
void motor(int a,int value) //function for direction and speed
{
OCR0A = (256/100)*value;
if(a == -1) //reverse direction
{
port |= (out1);
port &= ~(out2);
}
if(a == 0) //off state
{
port &= ~(out1);
port &= ~(out2);
}
if(a == 1) //forward direction
{
port |= (out2);
port &= ~(out1);
}
}
void motor_timer0()
{
DDRD = 0xff; //portD pin-6 as enable bit for motor driver
TCCR0A = 0b10000011; //prescaler setting
TCCR0B = 0b00000001;
OCR0A = 50;
motor(0,100);
}
void lcd_cmd(unsigned char cmd) // command for LCD display
{
LCDPORT = cmd; //Masking all bits to LCD command mode
RS_low; // accessing command register
EN_low;
_delay_ms(1);
EN_high;
}
void lcd_data(char data) //send the data to LCD
{
LCDPORT = data; //masking all bits LCD mode
RS_high; //accessing data register
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(0x02);
_delay_ms(5); //return the cursor to home
lcd_cmd(0x01);
_delay_ms(5); //clear screen
lcd_cmd(0x0C);
_delay_ms(5); //display ON cursor OFF
lcd_cmd(0x06);
_delay_ms(5); //increment cursor
lcd_cmd(0x80);
_delay_ms(5); //force cursor to begin 1st line
}
void cursorpos(char x, char y)
{
if ((x<1||x>2) || (y>16)||(y<1)) //condition will check the 2 rows and 16 columns
{
x = 1; //x=rows
y = 1; //y=columns
}
if (x == 1)
{
lcd_cmd(0x7F+y); //{80-1=0B}for starting the first list hex decimal value(80){y we are the 7F+y means we need to start from that column
}
else //(x=2)
{
lcd_cmd(0xBF+y); //{0C-1=0B} for starting the second line, 2nd row
}
}
void lcd_out(char x, char y, char *str) //here we are checking which position we want to print
{
cursorpos(x,y); //function call
while (*str)
{
lcd_data(*str); //string input
str++;
}
}
int main(void)
{
DDRC = 0x00;
PORTC = 0xff;
motor_timer0();
lcd_init();
int pwm = 50;
while(1)
{
if((PINC & (0b00000001)) == 0) //push button 1 is pressed
{
motor(1,pwm);
lcd_cmd(0x01);
lcd_out(1,1,"motor direction");
lcd_out(2,1,"forward");
_delay_ms(250);
}
if((PINC & (0b00000010)) == 0) //push button 2 is pressed
{
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.