Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Instructor: Abinaya Inbamani
Microchip Code:
#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "avr/interrupt.h"
#define Lcd_Port PORTB
#define Rs_high PORTD|=(1<<0)
#define Rs_low PORTD&=~(1<<0)
#define En_high PORTD|=(1<<1)
#define En_low PORTD&=~(1<<1)
int a;
void Lcd_Command(unsigned char cmd);
void Lcd_data(unsigned char data);
void Lcd_init();
void cursorpos(char x, char y);
void Lcd_Print(char row,char coloumn,unsigned int value,int digits);
void Lcd_out(char x,char y,char *str);
void adc_read(char channel);
int main(void)
{
DDRB=0xff; // portB as output
DDRD|=(1<<5); // PortD as output for PWM
DDRC|=(1<<0); //PortC as output and as input for LD293
DDRC|=(1<<1); //PortC as output and as input for LD293
sei(); //enabling interrupt
GICR=(1<<7)|(1<<6); // enable INT0 & INT1
PORTD=(1<<2)|(1<<3); //Enable pullup
Lcd_init();
TCCR1A=0x81; // PWM phase correct
TCCR1B=0x01; // no prescaling
TCNT1H=0;
TCNT1L=0;
while (1)
{
if(a==1)
{
Lcd_out(1,1,"Motor Clockwise");
OCR1AL=0x3f; //delay
}
else
{
OCR1AL=0x7f; //delay
Lcd_out(1,1,"Motor Anticlockwise");
}
}
}
ISR(INT0_vect)
{
PORTC |=(1<<0);
PORTC &=~(1<<1);
a=1;
Lcd_Command(0x01);_delay_ms(5);
}
ISR(INT1_vect)
{
PORTC |=(1<<1);
PORTC &=~(1<<0);
a=2;
Lcd_Command(0x01);_delay_ms(5);
}
void Lcd_Command(unsigned char cmd)
{
Lcd_Port=cmd;
Rs_low;
En_low;
_delay_ms(1);
En_high;
}
void Lcd_data(unsigned char data)
{
Lcd_Port=data;
Rs_high;
En_low;
_delay_ms(1);
En_high;
}
void Lcd_init()
{
Lcd_Command(0x38);_delay_ms(5);
Lcd_Command(0x02);_delay_ms(5);
Lcd_Command(0x01);_delay_ms(5);
Lcd_Command(0x0c);_delay_ms(5);
Lcd_Command(0x06);_delay_ms(5);
Lcd_Command(0x80);_delay_ms(5);
Lcd_Command(0xC0);_delay_ms(5);
}
void cursorpos(char x, char y)
{
if ((x<1||x>2)||(y<1||y>16))
{
x=1;
y=1;
}
if (x==1)
Lcd_Command(0x7f+y);
else
Lcd_Command(0xbf+y);
}
void Lcd_out(char x,char y,char *str)
{
cursorpos(x,y);
while(*str)
{
Lcd_data(*str);
str++;
}
}
OUTPUT:
A) DC Motor running clockwise direction with L293 motor driver

B) DC Motor running Anticlockwise direction with L293 motor driver
