Challenge

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

#include <avr/io.h>

#include <util/delay.h>

#define F_CPU 1600000UL

#define MOTOR_EN_PIN PD7

#define MOTOR_IN1_PIN PD3

#define MOTOR_IN2_PIN PD4

#define BUTTON_FORWARD_PIN PD5

#define BUTTON_REVERSE_PIN PD6

#define rs_high PORTD |= (1<<0)

#define rs_low PORTD &= ~(1<<0)

#define en_high PORTD |= (1<<2)

#define en_low PORTD &= ~(1<<2)

#define lcdport PORTB

void motor_int()

{

DDRD |= (1 << MOTOR_EN_PIN) | (1 << MOTOR_IN1_PIN) | (1 << MOTOR_IN2_PIN);

}

void motor_control(int direction, uint8_t speed)

{

if (direction == 1){

PORTD |= (1 << MOTOR_EN_PIN);

PORTD |= (1 << MOTOR_IN1_PIN);

PORTD &= ~(1 << MOTOR_IN2_PIN);

}

else if (direction == -1){

PORTD |= (1 << MOTOR_EN_PIN);

PORTD &= ~(1 << MOTOR_IN1_PIN);

PORTD |= (1 << MOTOR_IN2_PIN);

}else{

PORTD &= ~(1 << MOTOR_EN_PIN);

PORTD &= ~(1 << MOTOR_IN1_PIN);

PORTD &= ~(1 << MOTOR_IN2_PIN);

}

OCR2B = speed;

}

void lcd_cmd(unsigned char cmd){

lcdport=cmd;

rs_low;

en_high;

_delay_ms(1);

en_low;

}

void lcd_data(unsigned char data){

lcdport=data;

rs_high;

en_high;

_delay_ms(1);

en_low;

}

void lcd_int(){

lcd_cmd(0*38);_delay_ms(5);

lcd_cmd(0*02);_delay_ms(5);

lcd_cmd(0*01);_delay_ms(5);

lcd_cmd(0*0c);_delay_ms(5);

lcd_cmd(0*06);_delay_ms(5);

lcd_cmd(0*80);_delay_ms(5);

}

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.