Project 1 - Controlling a DC motor using PWM and monitoring its Running status
//
/*
* AVRProject1.c
*
* Created: 14/10/2022 05:48:36
* Author : varaprasad
*/
#include <avr/io.h> //header file for avr
#define F_CPU 16000000L//cpu clock frequency
#include <util/delay.h>//header file for delay
#include "avr/interrupt.h"
#define lcdport PORTB //PORTB AS LCD, MACRO
#define rs_high() PORTD |= (1<<5) //D5 logical high
#define rs_low() PORTD &= ~(1<<5) //D5 logical low
#define en_high() PORTD |= (1<<6) //D6 logical high
#define en_low() PORTD &= ~(1<<6) //D6 logical low
#define direction (PIND & (1<<1))
#define speed (PIND & (1<<2))
#define power_but (PINC & (1<<3))
unsigned char a;
void lcd_cmd (unsigned char cmd)//function definitions for lcd_cmd,lcd_data,lcd_print,cursor_pos,lcd_init
{
lcdport=(cmd & 0xF0) ;
rs_low(); //command register selected
en_low();
_delay_ms(1);
en_high();
lcdport=((cmd<<4)&0xF0);//4 bit lcd
en_low();
rs_low();
_delay_ms(2);
en_high();
}
void lcd_data (unsigned char data)
{
lcdport=(data & 0xF0);
rs_high(); //data register selected
en_low();
_delay_ms(1);
en_high();
lcdport=((data<<4)&0xF0);
en_low();
rs_high();
_delay_ms(2);
en_high();
}
void lcd_init()
{
lcd_cmd(0x28); //4bit lcd display
_delay_ms(5);
lcd_cmd(0x02); //return home
_delay_ms(5);
lcd_cmd(0x01);//clear screen
_delay_ms(5);
lcd_cmd(0x0C);
_delay_ms(5);
lcd_cmd(0x06);
_delay_ms(5);
lcd_cmd(0x80);//first line
_delay_ms(5);
}
void cursor_pos(char x, char y)//x-row,y-col
{
if((x<1||x>2)||(y<1||y>16))//any typo mistakes
{
x=1;
y=1;
}
if (x==1) //
{
lcd_cmd(0x7F+y);//value will be increased ton based in Y
}
else
{
lcd_cmd(0xBF+y);//value will be increased ton based in Y
}
}
void lcd_out(char x, char y, char *str)
{
cursor_pos(x,y);//2,5
while(*str)
{
lcd_data(*str);
str++;
}
}
int main(void)
{
/* Replace with your application code */
DDRB =0xFF;//Make PortB o/p
DDRC &=~(1<<3);//pinc input
DDRD &= ~(1<<1);//pinD1 i/p
DDRD &= ~(1<<2);//pinD2 i/p
DDRD |=(1<<5);//D5 o/p
DDRD |=(1<<6);//D6 o/p
DDRA |=((1<<0)|(1<<1));//A0,A1 as O/p
PORTD |=(1<<2);//enable pullup
PORTC |=(1<<3);//enable pullup
a=64;//initialising a value as 64
TCCR0=0x61;//make TCCR0=0x61
GICR|=(1<<INT0);//enable INT0 -interrupt pin
MCUCR=0x02;
sei();//enable global interrupt
lcd_init();//lcd Initialise fun
while (1) //infinite loop
{
while(power_but == 0){//run till powerbutton is closed
if(direction == 0){//direction is open
PORTA =0b00000001;//asigning ox01 value so that motor will run in clclwise-
lcd_out(1,1,"Motor Run -CLCK"); //print in lcd
}
else{
PORTA=0b00000010;//anticloclwise making 1A=0,2A=1
lcd_out(1,1,"Motor Run -ACLK");
}
OCR0=a; //avalue loaded to the OCR0
}
PORTA|= 0x00;//make motor off
lcd_out(1,1,"Motor is in OFF");
}
}
ISR(INT0_vect){//isr function for increasing the OCR0value
a=a+10; //increasing by value 10
}
//Motor is In OFF state

//Motor run in clockwise(power on and Direction switch is release)

//Motor run in CLokcwise with Dutycycle

//Motor run Anticlockwisse with Dutycycle
