Pantech.AI

“How to Interface DC motor with 8051”

Small DC Motor

Small DC motor

A DC motor converts electrical energy, provided as Direct Current (DC), into mechanical energy.

In the case of a DC motor, this mechanical energy is seen as the rotational movement of the motor shaft. By reversing the direction of the DC current through the motor, the shaft’s rotation direction can also be reversed. The speed of the motor is determined by the voltage applied: a fixed voltage yields a constant speed, while varying the voltage adjusts the speed proportionally.

Thus, the speed of a DC motor can be controlled by applying variable DC voltage, while the direction can be changed by reversing the current flow. The Pulse Width Modulation (PWM) technique is commonly used to vary the applied voltage, thereby controlling the speed. For reversing current flow, an H-Bridge circuit or motor driver ICs implementing H-Bridge configurations are typically used.

For more detailed information on DC motors, the PWM technique, and H-Bridge configurations, refer to the “DC Motors” section in sensors and modules documentation.

DC Motor Connection to 8051 using Motor Driver

8051 DC Motor Connection using L293D Motor Driver 

Interfacing a DC Motor with the AT89S52 Microcontroller

In this setup, we’ll interface a DC motor with the AT89S52 microcontroller and control its speed and rotation direction using external switches. The motor’s speed will be adjusted using Speed Increment and Speed Decrement switches, while the direction is controlled by a Direction switch.

For this project, we’ll use the L293D motor driver IC to manage bidirectional control of the DC motor, as it has an integrated H-bridge circuit for motor control.

Components and Pin Configuration

  1. Speed Control Switches: Two toggle switches connected to pins P1.0 and P1.1 of the AT89S52 microcontroller allow us to increase or decrease the motor speed in 10% increments.
  2. Direction Control Switch: A toggle switch connected to P1.2 determines the rotation direction of the motor.
  3. Direction Control Output Pins: Pins P1.6 and P1.7 are configured as output pins. These provide control signals to the motor input pins of the L293D driver, allowing the motor to rotate clockwise or counterclockwise by changing the polarity.
  4. PWM Output for Speed Control: The speed of the DC motor is adjusted using PWM generated by the microcontroller’s P2.0 pin.
  5. PWM Generation Using Timer: The AT89S52 microcontroller’s timer is used to create a PWM signal, which varies the motor speed according to the inputs from the speed control switches.

This setup enables smooth control of both the speed and direction of the DC motor using simple external switches and the L293D motor driver.

DC Motor Controlling Code using 8051

#include <reg52.h>
#include <intrins.h>

/* Define value to be loaded in timer for PWM period of 20 milli second */
#define PWM_Period 0xB7FE

sbit PWM_Out_Pin 	= P2^0;	/* PWM Out Pin for speed control */
sbit Speed_Inc 		= P1^0;	/* Speed Increment switch pin */
sbit Speed_Dec 		= P1^1;	/* Speed Decrement switch pin */
sbit Change_Dir		= P1^2;	/* Rotation direction change switch pin */

sbit M1_Pin1		= P1^6;	/* Motor Pin 1 */
sbit M1_Pin2		= P1^7;	/* Motor Pin 2 */

unsigned int ON_Period, OFF_Period, DutyCycle, Speed;

/* Function to provide delay of 1ms at 11.0592 MHz */
void delay(unsigned int count)
{
    int i,j;
    for(i=0; i<count; i++)
			for(j=0; j<112; j++);
}

void Timer_init()
{
	TMOD = 0x01;		/* Timer0 mode1 */
	TH0 = (PWM_Period >> 8);/* 20ms timer value */
	TL0 = PWM_Period;
	TR0 = 1;		/* Start timer0 */
}

/* Timer0 interrupt service routine (ISR) */
void Timer0_ISR() interrupt 1	
{
	PWM_Out_Pin = !PWM_Out_Pin;
	if(PWM_Out_Pin)
	{
		TH0 = (ON_Period >> 8);
		TL0 = ON_Period;
	}	
	else
	{
		TH0 = (OFF_Period >> 8);
		TL0 = OFF_Period;
	}	
			
}

/* Calculate ON & OFF period from PWM period & duty cycle */
void Set_DutyCycle_To(float duty_cycle)
{
	float period = 65535 - PWM_Period;
	ON_Period = ((period/100.0) * duty_cycle);
	OFF_Period = (period - ON_Period);	
	ON_Period = 65535 - ON_Period;	
	OFF_Period = 65535 - OFF_Period;
}

/* Initially Motor Speed & Duty cycle is zero and in either direction */
void Motor_Init()
{
	Speed = 0;
	M1_Pin1 = 1;
	M1_Pin2 = 0;
	Set_DutyCycle_To(Speed);
}

int main()
{
  EA  = 1;			/* Enable global interrupt */
  ET0 = 1;   			/* Enable timer0 interrupt */
  Timer_init();
  Motor_Init();
  while(1)
    {
	/* Increment Duty cycle i.e. speed by 10% for Speed_Inc Switch */
	if(Speed_Inc == 1)
		{
			if(Speed < 100)
			Speed += 10;
			Set_DutyCycle_To(Speed);
			while(Speed_Inc == 1);
			delay(200);
		}
	/* Decrement Duty cycle i.e. speed by 10% for Speed_Dec Switch */
	if(Speed_Dec == 1)
		{
			if(Speed > 0)
			Speed -= 10;
			Set_DutyCycle_To(Speed);
			while(Speed_Dec == 1);
			delay(200);
		}
	/* Change rotation direction for Change_Dir Switch */
	if(Change_Dir == 1)
		{
			M1_Pin1 = !M1_Pin1;
			M1_Pin2 = !M1_Pin2;
			while(Change_Dir == 1);
			delay(200);
		}
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *