# PWM control of DC motor with single chip microcomputer for speed regulation

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_6035432c0100hc8y.html

The previous article has explained the DC motor Drive, how to make it forward, reverse and stop, and a detailed talk about the H-bridge principle. Today write here how to control the speed of the DC motor with a single chip microcomputer.

DC Governor is to adjust the speed of the DC motor equipment, the upper and the AC power connection, the lower and DC motor connection, DC governor will be alternating current into two output DC power supply, one input to the DC motor Rough Magnetic (stator), one input to the DC motor armature (rotor), The DC Governor regulates the speed of the DC motor by controlling the armature DC voltage. At the same time the DC motor to the governor of a feedback current, the governor according to the feedback current to determine the speed of the DC motors, when necessary to correct the armature voltage output, in order to adjust the speed of the motor again.

DC Motor speed control scheme generally has the following 3 ways: 1, change the armature voltage, 2, change the excitation winding voltage, 3, change the armature circuit resistance.

The use of single-chip microcomputer to control the speed of the DC motor, the general use of the method of regulating the armature voltage, through a single-chip microcomputer control pwm1,pwm2, to produce a variable pulse, so that the voltage on the motor is also a variable width pulse voltage. According to the formula

U=avcc

Wherein: U is the armature voltage, A is the pulse duty ratio (0<a<1), VCC DC voltage source, here is 5 v.

The armature voltage of the motor is controlled by the output pulse of single-chip microcomputer, which realizes the variable speed of DC motor using pulse width modulation (PWM) technology.

Because in the H-bridge circuit, only the PWM1 and PWM2 level opposite each other when the motor can be driven, that is, PWM1 and PWM2 are high or low, neither can work, so the actual pulse width in the above figure is B,

We set the PWM wave cycle to 1ms, the duty-free score of 100-level adjustable (each level of 10%), so that the timer T0 every 0.01ms to produce a timed interrupt, every 100 times after entering the next PWM wave cycle. Above, the duty ratio is 60%, that is, the output pulse is 0.6ms, the disconnection pulse is 0.4ms, so that the armature voltage is 5*60%=3v.

We are talking about can be reversed, if only in one direction, we just set the PWM1 to high or low, only change the pulse of the other PWM2 level changes can be, as shown below (Q4 conduction, Q3 closed, the motor can only adjust the rotational speed clockwise)

C language code:

#include <AT89X52.h>
#define UCHAR unsigned char
#define UINT unsigned int

Sbit k5=p1^4;
Sbit k6=p1^5;
Sbit pwm1=p1^0;
Sbit pwm2=p1^1;
Sbit fmq=p3^6;

Uchar Zkb1,zkb2;

void Delaynms (UINT AA)
{
UCHAR BB;
while (aa--)
{
for (bb=0;bb<115;bb++)//1ms Reference delay Program
{
;
}
}

}

void Delay500us (void)
{
Int J;
for (j=0;j<57;j++)
{
;
}
}

void beep (void)
{
Uchar T;
for (t=0;t<100;t++)
{
DELAY500US ();
fmq=!   FMQ; Generate pulses
}
Fmq=1; Turn off the buzzer
DELAYNMS (300);
}

void Main (void)
{
tr0=0; Turn off timer 0
tmod=0x01; Timer 0, mode of operation 1
th0= (65526-100)/256;
tl0= (65526-100)%256; 100us or 0.01ms interrupts once
Ea=1; Open Total Interrupt
Et0=1; Open Timer 0 Interrupt
Tr0=1; Start Timer T0
zkb1=50; Duty-ratio initial setting
zkb2=50; Duty-ratio initial setting
while (1)
{
if (! K5)
{
DELAYNMS (15); Anti-Shake
if (! K5)//OK button Press
{
Beep ();
zkb1++; Increase ZKB1
ZKB2=100-ZKB1; The corresponding ZKB2 is reduced
}
}
if (! K6)
{
DELAYNMS (15); Anti-Shake
if (! K6)//OK button Press
{
Beep ();
zkb1--; Reduce ZKB1
ZKB2=100-ZKB1; The corresponding ZKB2 increase
}
}
if (zkb1>99)
Zkb1=1;
if (zkb1<1)
zkb1=99;

}
}

void Time0 (void) Interrupt 1
{
Static Uchar n=0;
th0= (65526-100)/256;
tl0= (65526-100)%256;
n++;
if (n>100)
n=0;
if (N<=ZKB1)
pwm1=0;
Else
Pwm1=1;
if (N<=ZKB2)
pwm2=0;
Else
Pwm2=1;

}

Appearance: The motor speed to the highest, that is, N is 1 or 99 o'clock, and then click, Change to 99 or 1,
The motor rotates in the opposite direction at the highest speed

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.