Project 1 - Controlling a DC motor using PWM and monitoring its Running status
As per conversation with technical team regarding submission issue, Iam removing all text content and images(Please find in the document below).
/*
* AVR_PROJECT_1_DC_MOTOR.c
*
* Created: 07-11-2023 22:09:19
* Author : SEKHAR KUNCHA
*/
#include <avr/io.h> // avr Lib
#define F_CPU 8000000UL // defining the clock frequency
#include <util/delay.h> // delay lib
#define LCD PORTD // defining the PORTD as LCD
#define EN 6 // defining the 6 as EN
#define RS 0 // defining the 0 as RS
int Direction_ctrl = 0; // Defining the variable for motor direction control
void LCDcmd(unsigned char cmd) {
//To execute the commands this function is used
PORTB &= ~(1 << RS);
LCD = cmd & 0xF0;
PORTB |= (1 << EN);
_delay_ms(1);
PORTB &= ~(1 << EN);
LCD = cmd << 4;
PORTB |= (1 << EN);
_delay_ms(1);
PORTB &= ~(1 << EN);
}
void lCDdata(unsigned char data) {
//As we are used 4 - bit mode we have to send thw H_4bits 1st and remainig second
PORTB |= (1 << RS);
LCD = data & 0xF0;
PORTB |= (1 << EN);
_delay_ms(1);
PORTB &= ~(1 << EN);
LCD = data << 4;
PORTB |= (1 << EN);
_delay_ms(1);
PORTB &= ~(1 << EN);
}
void lcd_init() {
// this function initilize the LCD
PORTB &= ~(1 << EN);
_delay_ms(20); // Wait for LCD to power up
// Standard LCD initialization sequence
LCDcmd(0x33);
LCDcmd(0x32);
LCDcmd(0x28);
LCDcmd(0x0C);
LCDcmd(0x01);
_delay_ms(2);
}
void lcd_printer(const char *str) {
// To write the each character which we wanted to print on displat this function will help
unsigned char i = 0;
while (str[i] != '\0') {
lCDdata(str[i]);
i++;
}
}
int main(void) {
// to control the motor Speed using the timer2 f=to generate PWM Signal
DDRD = 0xF0; // Set PORTB as output for motor control
DDRB = 0x79;
lcd_init();
//LCDcmd(0xC0);
//configuring the timer0 Fast PWM and non inverted Mode
TCCR2A = 0b10000011;
// configure the clock without prescaler
TCCR2B = 0b00000001;
// Load the OCR0A with the calculated value for Duty Cycle
//initial text showing on the display
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Motor OFF");
while (1) {
//if the button is pressed variable is increamented and this will helpful to change the direction or speed of the motor
if(PINB & 0X04)
{
Direction_ctrl++;
}
switch(Direction_ctrl)// based on the variable value case selection is done
{
case 1:
// setting the motor in the clock wise direction with 50% speed and printing the same in the LCD
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Dire:Clock");
LCDcmd(0xC0);
lcd_printer("Speed: 50%");
PORTB &= 0x00;
PORTB |= 0x20;
OCR2A = 125;
break;
case 2:
// setting the motor in the clock wise direction with 100% speed and printing the same in the LCD
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Dire:Clock");
LCDcmd(0xC0);
lcd_printer("Speed: 100%");
PORTB &= 0x00;
PORTB |= 0x20;
OCR2A = 255;
break;
case 3:
// setting the motor in the Anti clock wise direction with 50% speed and printing the same in the LCD
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Dire:Anti-Clock");
LCDcmd(0xC0);
lcd_printer("Speed: 50%");
PORTB &= 0x00;
PORTB |= 0x10;
OCR2A = 125;
break;
case 4:
// setting the motor in the Anti clock wise direction with 100% speed and printing the same in the LCD
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Dire:Anti-Clock");
LCDcmd(0xC0);
lcd_printer("Speed: 100%");
PORTB &= 0x00;
PORTB |= 0x10;
OCR2A = 255;
break;
case 5:
// in this case motor will stop and printing the same
Direction_ctrl = 0;
OCR2A = 0;
LCDcmd(0x01);
LCDcmd(0x80);
lcd_printer("Motor OFF");
break;
}
_delay_ms(700); // delay for 700mSecs
}
return 0;
}