Challenge

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

*live batch student*

program:

#include <avr/io.h>
#define F_CPU 16000000L
#include<util/delay.h>

// lcd configuration //
#define RS_LOW PORTB &= ~(1<<2); //making rs bit low
#define RS_HIGH PORTB |=(1<<2); //making rs bit high
#define EN_LOW PORTB &= ~(1<<3); //making enable bit low
#define EN_HIGH PORTB |=(1<<3); //making enable bit high

// motor direction program //
void motor_direction(int status, int delay)
{
OCR0A = delay; // op comparator reg. giving delay
PORTB = 0x40; // enabling l293 connected to port b
if(status == 0) // off condition
{
PORTB &= ~(1<<0); //both pins are low
PORTB &= ~(1<<1);
}
if(status == 1) //forward direction
{
PORTB |= (1<<0); //making pin 0 high
PORTB &= ~(1<<1);
}
if(status == -1) //for reverse direction
{
PORTB &= ~(1<<0);
PORTB |= (1<<1); //making pin 1 high
}
}
void motor_timer0() // motor timer operation
{
DDRB = 0xff; //making d as output port
TCCR0A = 0b00000011; // select mode fast PWM
TCCR0B = 0b00000001; // setting the prescaler
OCR0A = 68; //68 is nothing but prescalar or delay
motor_direction(0, 150);
}

//command & data mode operation//
void lcd_cmd(unsigned char cmd)
{
PORTD = cmd;
RS_LOW; //setting command mode
EN_LOW; //giving clock signal
_delay_ms(5);
EN_HIGH;
}
void lcd_data(unsigned char data)
{
PORTD = data;
RS_HIGH; //setting data mode
EN_LOW; // giving clock signal
_delay_ms(5);
EN_HIGH;
}

//lcd operation//
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 cursor_position(char x, char y) //setting cursor position
{
if((x < 1 | x > 2)||(y < 1 | y > 16))
{
x=1;
y=1;
}
if(x==1)
{
lcd_cmd(0x7f+y);
}
else
{
lcd_cmd(0xbf+y);
}
}
void lcd_out(char a, char b, char *str) //printing in lcd
{
cursor_position(a,b);
while(*str)
{
lcd_data(*str);
str++;
}
}

int main(void)
{
DDRC = 0x00; //making portc pins 0&1 as input(pushbutton)
PORTC = 0xff ; //activate pull up resistor
lcd_init();
motor_timer0();
char g[20] = "motor stop";
lcd_out(1,1, &g);
_delay_ms(500);
while(1)
{
if((PINC & (0x01)) == 0)
{
motor_direction(1,80); // motor direction calling function
lcd_cmd(0x01); //clearing screen
lcd_cmd(0x01);
char d[20] = "rotate anti_cw"; //message printing to lcd
lcd_out(1,1, &d);
_delay_ms(500);
}
if((PINC & (0x02)) ==0)
{
motor_direction(-1,80); // motor direction calling function
lcd_cmd(0x01); //clearing screen
lcd_cmd(0x01);
char c[20] ="rotate cw"; //message printing to lcd
lcd_out(1,1,&c);
_delay_ms(500);
}
}
}

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.