Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Overview of the Project:
Implement a system on the simulator for controlling a DC motor using L293 motor driver, monitor its status and print the running status of motor on the LCD Display.
#include <avr/io.h>
#define F_CPU 16000000L
#include<util/delay.h>
#include "avr/interrupt.h"
#define lcdport 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 column, unsigned int value, int digits);
void lcd_out(char x, char y, char *str);
int adc_read(char channel);
int main(void)
{
DDRB =0xff; //Port B as output
DDRD |=(1<<5); //Port D as output for PWM - enable input for L293D
DDRC |=(1<<0); //PORTC as output & As input pins for L293D
DDRC |(1<<1); //PORTC as output & As input pins for L293D
sei(); //Enable global interrupt
GICR=(1<<7)|(1<<6);//Enable INT0 & INT1 - for switch connections
PORTD=(1<<2)|(1<<3);//Enable Pull up
lcd_init();
TCCR1A=0x81;
TCCR1B=0x01;
TCNT1L=0;
TCNT1H=0;
while(1)
{
if(a==1)
{
lcd_out(1,1,"Motor Clockwise");
OCR1AL=0X3F;
}
else
{
OCR1AL=0X7F;
lcd_out(1,1,"Motor AntiClockwise");
}
}
}
ISR(INT0_vect)
{
PORTC|=(1<<0);
PORTC&=~(1<<1);
a=1;
lcd_cmd(0x01);_delay_ms(5);
}
ISR(INT1_vect)
{
PORTC|=(1<<1);
PORTC&=~(1<<0);
a=2;
lcd_cmd(0x01);_delay_ms(5);
}
void lcd_cmd(unsigned char cmd)
{
lcdport=cmd;
rs_low;
en_low;
_delay_ms(1);
en_high;
}
void lcd_data(unsigned char data)
{
lcdport=data;
rs_high;
en_low;
_delay_ms(1);
en_high;
}
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);
lcd_cmd(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_cmd(0x7F+y);
else
lcd_cmd(0xbf+y);
}
void lcd_out(char x,char y, char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}