Challenge

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

ONLINE LIVE BATCH _ 8

1.

/*
* AVR_PROJECT_1.c
*
* Created: 23-08-2022 23:44:26
* Author : user
*/

#include <avr/io.h> //we are using I/O registers of AVR
#define F_CPU 16000000L // define the crystal frequency
#include "avr/interrupt.h" // Library for interrupt function
#include <util/delay.h> // Library for delay
#define sw_dir (PIND & (1<<3)) // d3 pin is declared as push button for direction control.
#define sw_dspd (PIND & (1<<2)) // d2 pin is declared as push button for speed control.
#define pw (PIND & (1<<4)) // d4 is declared as push button for power ON/OFF

#define lcd_port PORTC //PORTB pins are for LCD data
#define RS_high PORTD |= (1<<0) // d5 pin is used for Register Select(RS)
#define RS_low PORTD &=~ (1<<0) // d5 pin is used for RS
#define EN_high PORTD |= (1<<1) // d6 pin is used for Enable
#define EN_low PORTD &=~ (1<<1) // d6 pin is used for En.
int i = 0;
int duty = 0;

//command for lcd display
void lcd_cmd(char cmd)
{
lcd_port = cmd; // Send command to LCD
RS_low; // Pull down to set to cpmmand mode

// Reset the LCD display
EN_high;
_delay_ms(1);
EN_low;
}

//send the data to lcd
void lcd_data (char data)
{
lcd_port = data; // Send data to LCD
RS_high; // Pull up to set to data mode

// Reset the LCD display
EN_high;
_delay_ms(1);
EN_low;
}

// LCD initialization condition
void lcd_init()
{
lcd_cmd(0x38); // 8 bit 2 line, 5x7 dots
_delay_ms(500);
lcd_cmd(0x02); // Return home
_delay_ms(500);
lcd_cmd(0x01); // CLear display
_delay_ms(5);
lcd_cmd(0x0c); //Display ON cursor OFF
_delay_ms(5);
lcd_cmd(0x06); // Entry mode
_delay_ms(5);
lcd_cmd(0x80); //Force the cursor to start from 1st line
_delay_ms(5);
}

void cursorpos(char x,char y)
{
if((x<1 || x>2) || (y>16 || y<1 )) //condition will checks the 2 rows and 16 columns
{
x=1; //x= row
y=1; //y= column
}
if(x==1)
{
lcd_cmd(0x80 + y); //Force curcor on 1st line that column}
}
else //(x==2)
{

lcd_cmd(0xC0 + y); //Force curcor on 2nd line
}
}


void lcd_out(char x, char y, char *str) //here we are checking which row we want to print, and we print the string
{
cursorpos(x,y); //Check the row
while(*str) // get the string address
{
lcd_data(*str); // send the data to LCD
str++;
}
}

int main(void)
{
DDRC = 0XFF; //Declaring all 8-pins as output.
DDRA |= (1<<0)| (1<<1); //Declaring A0 & A1 pins as output.
DDRD &=~ (1<<2); //Declaring D2 pin as input.
DDRD &=~ (1<<3); //Declaring D3 pin as input.
DDRD &=~ (1<<4); //Declaring D3 pin as input.
DDRB |= (1<<PB3); //Declaring D3 pin as output
PORTD = (1<<2) | (1<<3) | (1<<4); //enabling internal pull up resistor for D2, D3, D4
TCCR0= (1<<WGM00) | (1<<COM01) | (1<<CS00); //Non-Inverting mode, PWM phase correct mode,NO prescalar.
GICR=(1<<INT0); //using INT0 for interrupt
sei(); //Enable global interrupt
lcd_init(); //Initialize lcd
while (1)
{
while(pw==0) //check the power status
{
if(sw_dir == 0) //check the direction for clockwise/anticlockwise
{
PORTA = 0b00000001; // clockwise direction.
lcd_out(1,1,"Motor_clockwise "); //Running clock wise direction

}
else
{
PORTA = 0b00000010; // Anti-Clockwise direction.
lcd_out(1,1,"Motor_anticlock"); ////Running clock wise direction

}
OCR0 = 127; //50% duty cycle

}

PORTA = 0x00; // Stop rotation.
lcd_out(1,1,"Motor_turnedoff"); // Motor OFF
}
}

//interrupt service routine
ISR(INT0_vect)
{

for(duty=0; duty<255; duty++)
{
OCR0=duty; /*increase the motor intensity*/
_delay_ms(8);
}
for(duty=255; duty>1; duty--)
{
OCR0=duty; /*decrease the motor intensity*/
_delay_ms(8);
}

}

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.