Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Code:
#define F_CPU 16000000L
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define spcn PD2//declaring the d2 pin has speed control pin
#define dm PD0//declaring the d0 pin has speed control pin
#define mof PD6//declaring d6 as motor off switich
#define rs_high PORTA |=(1<<6)//enabling lcd register high
#define rs_low PORTA &=~(1<<6)//enabling lcd register low
#define en_high PORTA |=(1<<7)//enabling lcd enable high
#define en_low PORTA &=~(1<<7)//enabling lcd enable low
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);
_delay_ms(5);
lcd_cmd(0x02);
_delay_ms(5);
lcd_cmd(0x01);
_delay_ms(5);
lcd_cmd(0x0c);
_delay_ms(5);
lcd_cmd(0x06);
_delay_ms(5);
lcd_cmd(0x80);
_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++;
}
}
void motordir()
{
if((PIND &(1<<dm))==0x00)//checking the the direction pin close or open
{
PORTA &=~(1<<1);//clearing the pin1 status before toggling
PORTA|=(0b00000001);//00000001
PORTA|=(0x10);//for led blinking
lcd_cmd(0x01); //clearing the lcd data
lcd_out(1,1,"Motor on ");//printing the data motor on
lcd_out(2,1,"Dirct-clck");//direction printing
_delay_ms(500);
}
else{
PORTA &=~(1<<0);//clearing the pin0 status during the toggling
PORTA|=(0b00000010);//00000010
PORTA|=(0x20);
lcd_cmd(0x01);
lcd_out(1,1,"Motor on ");
lcd_out(2,1,"Dirct-anti-clk ");
_delay_ms(500);
}
OCR0=i;
}
int main(void)
{
DDRA |=0xFF;//making all port has output
DDRC |=0xFF;//making all port has output
DDRD &=~(1<<spcn);//making pin d2 as input
DDRD &=~(1<<dm) ;//making pd0 as input
DDRD &=~(1<<mof);//making pd6 as input
PORTD |=((1<<dm)|(1<<spcn)|(1<<mof));//enable pullup
DDRA |=((1<<0)|(1<<1));//making pin a0 and a1has output
DDRB |=(1<<3);//making b3 has output
PORTB |=(1<<3);//11making pin3 has high
i=64;
OCR0=i;//for 25 percent duty cycle
TCCR0=0x61;//phase correct PWM
GICR=(1<<INT0);//enable external interrupt0
MCUCR=0x02;//neagtive edge trigger
sei();//enable interrupt
lcd_init();
while(1)
{
while((PIND &(1<<mof))==0x00)//condition for on/off switich if switich is closed
{
motordir();//while switich is closed then the we determine direction of the motor
}
PORTA=0x00;//turning off the motor condition
PORTA|=(0x20);
lcd_cmd(0x01);
lcd_out(1,1,"Motor off");//calling the lcd function
_delay_ms(500);
}
}
ISR(INT0_vect)//enabling the int0 intruppt
{
i=i+20;//adding the value to the timer
}