Challenge

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

#include <avr/io.h>// I/O registers of AVR
#define F_CPU 16000000L // define the cpu frequency
#include <avr/interrupt.h> // header file for intrupt
#include <util/delay.h>// delay function purpose
#define Pb_direction (PIND & (1<<1)) //
#define Pb_speed (PIND & (1<<2)) // d2 pin is declared as push button for speed control.
#define Powerbutton (PINC & (1<<3)) // c3 is declared as push button for power ON

#define lcd_port PORTB //PORTB pins are for LCD data
#define RS_high PORTD |= (1<<5) // defined RS as high
#define RS_low PORTD &=~ (1<<5)
#define EN_high PORTD |= (1<<6) // define en as high
#define EN_low PORTD &=~ (1<<6) //define en as low
unsigned char i;

void lcd_cmd(char cmd) //command for LCD display
{
lcd_port = cmd & 0xf0; //upper nibble
RS_low;// LCD take it as cmd
EN_low;
_delay_ms(1); //delay 1ms
EN_high;
lcd_port =cmd<<4; //lower nibble
RS_low;
EN_low;
_delay_ms(1);
EN_high;
}

void lcd_data (char data) //send the data to lcd
{
lcd_port = (data & 0xf0);
RS_high; //lcd will take as data
EN_low;
_delay_ms(1);
EN_high;
lcd_port = (data<<4);
RS_high; //LCD will take as data
EN_low;
_delay_ms(1);
EN_high;
}

void lcd_init()
{

lcd_cmd(0x28);// activate for 4 bit LCD mode
_delay_ms(5);
lcd_cmd(0x02); // return home
_delay_ms(5);
lcd_cmd(0x01); // clear the screen
_delay_ms(5);
lcd_cmd(0x0C); // display on cursor off
_delay_ms(5);
lcd_cmd(0x06); // increment the display cursor
_delay_ms(5);
lcd_cmd(0x80); // force cursor to begin from 1st line
_delay_ms(5);
}

void cursorpos(char x,char y)
{
if((x<1 || x>2) || (y>16 || y<1 )) //condition will check max 2row 16 column
{
x=1; //x= row
y=1; //y= column
}
if(x==1)
{
lcd_cmd(0x7F + y); //0x80 for starting the 1st line
}
else //(x==2)
{

lcd_cmd(0xBF + y); //for starting the 2nd line,
}
}

void lcd_out(char x, char y, char *str) //as per position
{
cursorpos(x,y);
while(*str) //this will run till null
{
lcd_data(*str);
str++;
}
}

int main(void)
{
DDRB = 0XFF; //Declaring PORTB as output.
DDRC |= ((1<<0)| (1<<1)); //C0 & C1 pins as output.
DDRD &=~ (1<<1); //D1 pin as input.
DDRD &=~ (1<<2); //Declaring D2 pin as input.
DDRD |= (1<<5); //Declaring D5 pin as output.
DDRD |= (1<<6); //Declaring D6 pin as output.
DDRC &=~ (1<<3); //Declaring C3 pin as input.
PORTD |= (1<<2); //enabling internal pull up resistor for D2.
PORTC |= (1<<3); //enabling internal pull up resistor for C3.
i=64; //initializing the motor with 25% duty cycle.
TCCR0=0X61;//Non-Inverting mode, PWM phase correct mode,NO pre scalar.
GICR=(1<<6); //enable INT0 in general
MCUCR=0X02; //interrupt as falling edge
sei(); //Enable global interrupt
lcd_init(); //Initialize LCD cmd function
while (1)
{
while(Powerbutton==0)
{
if(Pb_direction == 0) //if button is open as pull up
{
PORTC = 0b00000001; // polarity set in clock wise
lcd_out(1,1,"Motor ON- CLW "); //Running clock wise direction

}
else
{
PORTC = 0b00000010;// polarity set in anti clock wise
lcd_out(1,1,"Motor ON- Anti-CLW");
}
OCR0 = i; // as OCR0 varies the duty will cycle vary too
}

}
PORTC=0x00;


}
ISR(INT0_vect) //interrupt function defined here
{
i=i+10; //increment
}

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.