Project 1 - Controlling a DC motor using PWM and monitoring its Running status
LIVE BATCH 5
CODE
/*
* 1_project.c
*
* Created: 29-10-2022 15:44:33
* Author : thira
*/
#include <avr/io.h> //Preprocessor for avr
#define F_CPU 16000000L // preprocessor for crystal frequency of 16MHz
#include "avr/interrupt.h" //Preprocessor for interrupt
#include <util/delay.h> // delay function preprocessor
#define lcd_port PORTB //PORTB pins are for LCD data
#define rs_high PORTD |= (1<<0) // d0 pin is used for Register Select(RS)
#define rs_low PORTD &=~ (1<<0) // d0 pin is used for RS
#define en_high PORTD |= (1<<1) // d1 pin is used for Enable
#define en_low PORTD &=~ (1<<1) // d1 pin is used for En.
#define lcdport PORTB
int a; //variable initialization
void lcd_cmd(unsigned char cmd) //definiing lcd_cmd function
{
lcdport = cmd; //storing the cmd to lcdport
rs_low; //setting register low
en_high; //setting enable high
_delay_ms(1); //giving delay of 1ms
en_low; //setting enable low
}
void lcd_data (unsigned char data) //defiinig lcd_data function
{
lcdport = data; //data to lcdport
rs_high; //setting register high for entering data
en_high; //enable is set high
_delay_ms(1); //delay of 1ms
en_low; //enable low
}
void lcd_int() //lcd_int definition
{
lcd_cmd(0x38); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
_delay_ms(5); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
lcd_cmd(0x02); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
_delay_ms(5); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
lcd_cmd(0x0c); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
_delay_ms(5); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
lcd_cmd(0x06); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
_delay_ms(5); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
lcd_cmd(0x80); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
_delay_ms(5); //setting up the led_cmd as per the specifications and requirements of the system and referring to the predefined register bits
}
void cursorpes(char x, char y) //cursorpes function definition
{
if((x<1 || x>2) | (y<1 || y>16)) //if condition
{
x=2; //variable declaration
y=1;
}
if(x==1) //if condition
{
lcd_cmd(0x80+y); //same as (1<<8)
}
else{
lcd_cmd(0xc0+y); //statement
}
}
void lcd_out(char a , char b, char *str) //lcd_out declaration
{
cursorpes(a,b);//function calling
while(*str) //while loop
{
lcd_data(*str); //function calling
str++; //str incrementation
}
}
int main(void)
{
DDRB = 0XFF; //Declaring all 8-pins as output.
DDRB |=(1<<5); //setting potr b pin 5 as output
DDRC = (1<<0) | (1<<1); //setting pin 0 an 1 of portc
sei(); //Enable global interrupt
GICR = (1<<7) | (1<<6); //global interrupt
PORTD = (1<<2) | (1<<3);
lcd_int(); //Initialize lcd
TCCR1A = 0x81;
TCCR1B = 0x01;
TCNT1L = 0; //lower bit
TCNT1H = 0; //higher bit
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) //interrupt service routine
{
PORTC |=(1<<0);
PORTC &=~(1<<1);
a=1;
lcd_cmd(0x01);
_delay_ms(5);
}
ISR(INT1_vect) //interrupt service routine
{
PORTC |=(1<<1);
PORTC &=~ (1<<0);
a=2;
lcd_cmd(0x01);
_delay_ms(5);
}


OUTPUT
Motor moving anticlockwise

Motor moving clockwise

