Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Code:
#include
#define F_CPU 16000000 //define the crystal frequency
#include "avr/interrupt.h" // we are using external interrupt pin
#include
#define sw_dir !(PIND&(1<<1)) //d1 pin is declared as push button for direction control
#define sw_spd (PIND&(1<<2)) //d2 pin is declared as push button for speed control
#define power (!(PIND&(1<<0))) //d3 pin is declared as push button for power on/off.
#define lcd_port PORTC //portC pins are for LCD data
#define RS_high PORTD|=(1<<6) //d6 pin is used for resistor select(rs)
#define RS_low PORTD&=~(1<<6)
#define EN_high PORTD|=(1<<5) //d5 pin is used for enable
#define EN_low PORTD&=~(1<<5)
unsigned char i;
void lcd_cmd(char cmd) // command for LCD display
{
lcd_port=cmd;
RS_low;
EN_high;
_delay_ms(1); //after 1ms disable LCD
EN_low; // enable the LCD
}
void lcd_data(char data) //send the data to LCD
{
lcd_port=data;
RS_high; //access data register
EN_high;
_delay_ms(1);
EN_low;
}
void lcd_init()
{
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)) //condition will check the rows and 16 columns
{
x=1; //x=rows
y=1; //y=columns
}
if (x==1)
{
lcd_cmd(0x80+y);
}
else //(x=2)
{
lcd_cmd(0xC0+y);
}
}
void lcd_out(char x, char y, char *str) //here we are checking which position we want to print
{
cursorpos(x,y);
while (*str)
{
lcd_data(*str);
str++;
}
}
int main(void)
{
DDRC=0xFF; // declaring all 8 pins as output
DDRA|=((1<<0)|(1<<1)); // declaring A0 and A1 as output
DDRD&=~(1<<1); //declaring 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
DDRD&=~(1<<0);
PORTD|=(1<<1);
PORTD|=(1<<2); //enabling internal pull up resistor
PORTD|=(1<<0);
i=64; // initializing i=64 because we are running the motor with 25%duty cycle
TCCR0=0X61; // non inverting mode, PWM phase correct mode, no pre scalar
GICR=(1<
sei(); //enable global interrupt
lcd_init(); // initialized LCD
while (1)
{
while(power==0) //continue till push button is released
{
if (sw_dir==0) // if the direction control push button is released
{
PORTA=0b00000001; //10 respective 1A 2A pins to rotate in clockwise direction
lcd_out(1,1,"Motor on CLK"); // running clockwise direction
}
else
{
PORTA=0b00000010; //10 respective 1A 2A pins to rotate in anti-clockwise direction
lcd_out(1,1,"Motor on ACL"); // running anti-clockwise direction
}
OCR0=i; // i increase in ISR, the corresponding value is to be loaded into OCR0
}
PORTA=0X00; //0 0 to respective 1A 2A pins to stop condition
lcd_out(1,1,"Motor is_OFF");
}
}
ISR(INT0_vect) // interrupt service routine
{
i=i+20; //as i increases the duty cycle also increases .i increases by 20 in each push and the corresponding value is to be loaded into OCR0 in while loop
}
Output:
