Project 1 - Controlling a DC motor using PWM and monitoring its Running status
Here we implement the PWM mode DC motor control project .
Where we use SimulIde software for simulation.
and MicrowChip studio for Code genaretion.
Here we use a DC 5 volt motor for load and we use L293 driver for control the firing.
Also amplify the pulse we use a BJT.
For displaying the result or driving mode we use 26x2 LCD displat .and we use two toggle switch and two push botton for genarate the forward ,reverse and incriment and decrement mode.For control the motor.
Sircuit diogram:

Ambedded Code for PWM mode Motor Control:
/*
* GccApplication2.c
*
* Created: 19-06-2023 20:38:12
* Author : "SHAIBAL MANDAL"
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdint.h>
#define EN (1<<1)
//#define RW (1<<1)
#define RS (1<<0)
#define Forward_Increment (0xF5)
#define Reverse_Increment (0xF6)
#define Forward_Decrement (0xF9)
#define Reverse_Decremrnt (0xFA)
unsigned int n=0;
void PWM(int n);
void PWM_INIT();
void MOTOR_INIT();
void LCD_STRING(char *str);
void LCD_DATA(unsigned char data);
void LCD_COMAND(unsigned char cmd);
void LCD_DISPLAY_INIT(void);
void LCD_Clear();
void LCD_POS_xy (char row, char pos, char *str);
void Motor2();
void ForwardIncrement();
void ForwardDecrement();
void ReverseIncrement();
void ReverseDecrement();
int main()
{
PWM_INIT();
MOTOR_INIT();
LCD_DISPLAY_INIT();
_delay_ms(2);
LCD_STRING("PWM CONTROL MODE");
while (1)
{
Motor2();
}
}
void PWM(int n)
{ //Define Port as output
OCR0=n; //Define calculated OCR0 value
TCCR0=0x69; //Define TCCR0 resister value
}
void PWM_INIT() //INIT function for Motor pulse giving
{
DDRB |=(1<<3);
}
void MOTOR_INIT()
{
DDRA =0XFF;
DDRC =0x00;
PORTC=0xFF;
PORTA =0x00;
}
void LCD_DISPLAY_INIT(void)
{
DDRD =0xFF;
DDRB =0xFF;
_delay_ms(5);
LCD_COMAND(0x38);
LCD_COMAND(0x0C);
LCD_COMAND(0x06);
LCD_COMAND(0x01);
LCD_COMAND(0x80);
}
void LCD_COMAND(unsigned char cmd)
{
PORTD =cmd;
PORTB &=~RS;
PORTB |=EN;
_delay_us(20);
PORTB &=~EN;
_delay_us(20);
}
void LCD_DATA(unsigned char data)
{
PORTD =data;
PORTB |=RS;
PORTB |=EN;
_delay_us(20);
PORTB &=~EN;
_delay_us(20);
}
void LCD_STRING(char *str)
{
int i;
for (i=0;str[i]!=0;i++)
{
LCD_DATA(str[i]);
}
}
void LCD_Clear()
{
_delay_ms(2);
LCD_COMAND(0x01);
LCD_COMAND(0x80);
}
void Motor2()
{
if (n>255)
{
n--;
}
else if(n==0)
{
n++;
}
else
{
switch (PINC)
{
case Forward_Increment:
ForwardIncrement();
LCD_Clear();
LCD_POS_xy (0,0,"PWM MODE");
LCD_POS_xy (1,0,"MODE:Forward");
break;
case Forward_Decrement:
ForwardDecrement();
LCD_Clear();
LCD_POS_xy (0,0,"PWM MODE");
LCD_POS_xy (1,0,"MODE:Forward");
break;
case Reverse_Increment:
ReverseIncrement();
LCD_Clear();
LCD_POS_xy (0,1,"PWM MODE");
LCD_POS_xy (1,0,"MODE:Reverse");
break;
case Reverse_Decremrnt:
ReverseDecrement();
LCD_Clear();
LCD_POS_xy (0,0,"PWM MODE");
LCD_POS_xy (1,0,"MODE:Reverse");
break;
}
}
}
void LCD_POS_xy (char row, char pos, char *str)
{
if (row == 0 && pos<16)
{
LCD_COMAND(0x80);
LCD_STRING(str);
}
else if(row == 1 && pos<16)
{
LCD_COMAND(0xC0);
LCD_STRING(str);
}
}
void ForwardIncrement()
{
PORTA |=(1<<0);
PORTA &=~(1<<1);
PWM(n);//increment
n++;
}
void ReverseIncrement()
{
PORTA |=(1<<1);
PORTA &=~(1<<0);
PWM(n);//increment
n++;
}
void ReverseDecrement()
{
PORTA |=(1<<1);
PORTA &=~(1<<0);
PWM(n);//Decrement
n--;
}
void ForwardDecrement()
{
PORTA |=(1<<0);
PORTA &=~(1<<1);
PWM(n);//increment
n--;
}
Forward Mode:

Reverse Mode:

