Project 1 - Controlling a DC motor using PWM and monitoring its Running status
(This project was taught by Abinaya mam)
Aim: Concept of interrupt is used in this programme.
D2 & D3 pins are used as interrupts & we know when interrupts occured it should be given highest priority.
one push button is connected to D2 pins another push button connected to D3 according to that DC motor is running and showing clockwise, anticlockwise on display.
Big deal is here that we are using push button as interrupt according to that we used the logic in this programme.
#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 configured as output
DDRD|=(1<<5); //Port D as o/p for PWM enable for l293D
DDRC|=(1<<0); //Port c as o/p & as input pins for driver
DDRC|=(1<<1); //Port c as o/p & as input pins for driver
sei(); //enable global interrupt
GICR=(1<<7)|(1<<6); //enable INTO & INT1 for switch Conection
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++;
}
}








