Challenge

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

CODE:

#include <avr/io.h>//Header file for AVR Functions
#define F_CPU 16000000//Micro controller clock speed
#include <util/delay.h>//Header file for delay loops
#include "avr/interrupt.h"
#define sw_dir (PIND & (1<<1))//Direction control pin P1
#define sw_spd (PIND & (1<<2))//P2 pin as speed control
#define pw (PIND & (1<<3))//P3 pin as switch on and off
#define lcd_port PORTC//Port c as lcd port
#define rs_high PORTD |= (1<<4)//Register select high as P4 pin
#define rs_low PORTD &= ~(1<<4)//Register select low as P4 pin
#define en_high PORTD |= (1<<5)//enable high pin as P5 pin
#define en_low PORTD &= ~(1<<5)//enable low pin as P5 pin
unsigned char i;
void lcd_cmd(unsigned char cmd)
{
PORTC = cmd;//giving cmd to the LCD
rs_low;//making register low
en_high;//making enable high
_delay_ms(1);
en_low;//making enable low
}
void lcd_data(unsigned char data)
{
PORTC = data;//giving data to the LCD
rs_high;//making register high
en_high;//making enable high
_delay_ms(1);
en_low;//making enable low
}
void lcd_init()
{
//Making lcd ready for displaying the output//
lcd_cmd(0x38);//2lines for 5x7 matrix
_delay_ms(5);
lcd_cmd(0x02);//Return home
_delay_ms(5);
lcd_cmd(0x01);//Clear 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 the cursor to begin from 1st line
_delay_ms(5);
}
void cursorpos(char x,char y)
{
if((x<1 || x>2)||(y<1 || y>16))//condtion check the 2 rows and 16 columns
{
x=1;
y=1;
}
if (x==1)
{
lcd_cmd(0x7F+y);//{80-1=7f} for starting the 1st line hex decimal value(80) y we r using means we need to dtart from that column
}
else
{
lcd_cmd(0xBF+y);//0c-1=0b for starting 2nd line,2nd row
}
}
void lcd_out(char a, char b,char *str)
{
cursorpos(a,b);//checking the cursor position or setting the position
while(*str)
{
lcd_data(*str);//sending the data to lcd function with string as argument
str++;
}
}
int main(void)
{
DDRC=0xff;//Decalring all pins PC as output
DDRB=0xff;//Declaring all the PB pins as output
DDRA=0xff;//Declaring all the PA pins as output
DDRD &=~(1<<1);//Decalring D1 as input
DDRD &=~(1<<2);//Decalring D2 as input
DDRD &=~(1<<3);//Decalring D3 as input
DDRD|=(1<<4);//Declaring the D4 pin as output
DDRD|=(1<<5);//Declaring the D5 pin as output
PORTD|=(1<<2);//Enabling the internal pull up resistor for D2
PORTD|=(1<<3);//Enabling the internal pull up resistor for D3
i=64;//25% of duty cycle
TCCR0=0x61;//Non-Inverting mode, PWM phase correct mode,NO prescalar.
GICR|=(1<<INT0);//using INT0 for interrupt
MCUCR=0x02;//make interrupt negative edge triggered
sei();//Enable the interrupt
lcd_init();//Initialize the LCD
while(1)//Infinte while loop
{
while(pw==0)//Continue till switch is closed
{
if(sw_dir==0)//if the direction control push button is released.
{
PORTA=0b00000001;// 1 0 to respective 1A 2A pins to rotate in clockwise direction.
lcd_out(1,1,"Motor On - CL "); //Running clock wise direction
_delay_ms(500);
}
else
{
PORTA=0b00000010;// 1 0 to respective 1A 2A pins to rotate in Anti-Clockwise direction.
lcd_out(1,1,"Motor On - ACL ");//Running anti clock wise
_delay_ms(500);
}
OCR0=i;//i increases in ISR, the corresponding value is to be loaded into OCR0.
}
PORTA=0x00;// 0 0 to respective 1A 2A pins to Stop rotation.
lcd_out(1,1,"Motor is OFF ");
_delay_ms(500);
}
}
ISR(INT0_vect)//interrupt service routine
{
i=i+20; // as i increases the duty cycle also increases. i increases by 20 in each push//
}
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.