The remote control system of stepper motor developed based on 51 single-chip microcomputer

Summary

 

Motors are an essential part of everyday life and are also a commonly used electromechanical component. Stepper motor is a special motor. Compared with other types of motors, stepper motor has more prominent advantages and more obvious application advantages, and is widely used in various fields.

This design is based on the remote control system of stepping motor developed by single chip microcomputer. First of all, this design briefly expounds the domestic and foreign technical status of stepping motors, and briefly introduces the relevant content of stepping motors. Secondly, according to the system design requirements, the hardware part and software part of the stepper motor control system are designed. After program debugging simulation and physical debugging, the forward and reverse rotation of the stepper motor can be remotely controlled by infrared remote control. , Rotate a specific angle and other movement conditions. Finally, the design content is summarized, and the future development of single-chip microcomputer and stepping motor is briefly forecasted.

This design avoids the disadvantages of complex motor control systems on the market and high development and use costs. It is a simple motor control system with high control efficiency and low development and application costs.

 

Key words

single chip microcomputer; stepping motor; motor control; control system; system design

 

 

directory   

 

Chapter 1 Introduction

1.1 Research background and significance

1.2 Analysis of current situation at home and abroad

1.3 Research content

Chapter 2 Overview of Stepper Motor Remote Control System

2.1 Introduction to stepping motor

2.2 Performance characteristics and advantages and disadvantages of stepper motors

2.3 Classification of stepper motors

2.4 Common stepper motors

2.5 Composition of stepper motor remote control system

2.6 Single-chip microcomputer control stepper motor remote control system

Chapter 3 Hardware System Design of Motor Control System

3.1 System schematic diagram

3.2 Minimum single chip system

3.3 System drive circuit design

3.4 System working principle

Chapter 4 Software System Design of Motor Control System

4.1 The relationship between motor speed and time

4.2 System software design flow chart

4.3 Analysis of software design source program

Chapter 5 System Debugging

5.1 Program debugging simulation

5.2 Hardware debugging

Chapter 6 Summary

thank you

references

appendix

 

 

Chapter 1 Introduction

1.1 Research background and significance

Motors are commonly used components in daily life, and the normal operation of society requires the assistance of motors all the time. Stepper motor is a kind of motor, this type of motor is a kind of actuator with control function, which is mainly used to realize precise control of different instruments and equipment. In the actual operation process, the stepper motor can convert the pulse signal into a driving force, and drive the stepper motor to perform corresponding operations, such as rotation and stop. So far, single-chip microcomputer is the most commonly used chip structure for controlling and designing stepper motors. With the continuous improvement of computer technology, more and more types of single-chip microcomputers appear in people's field of vision. After the application of high-performance single-chip microcomputers, the overall performance of the stepper motor control system can be fundamentally improved.

Today, with the rapid development of Internet technology, the traditional motor control system using discrete components has the disadvantages of low stability, high component consumption, and high development costs. In order to solve these problems, it is necessary to apply advanced chips such as single-chip microcomputers. Precise control of the motor.

Under the impetus of various science and technology, the research and development level of single-chip microcomputer and stepper motor has been significantly improved. Taking the single-chip microcomputer with simple structure and good performance as the core of motor control can fundamentally improve the overall performance of the control system. Increasing the research and design of single-chip motor control systems can not only promote the further improvement of the theoretical system of related technologies, but also fundamentally improve their own practical ability, and establish a good foundation for the smooth progress of future research on motor control.

To sum up, this article is mainly based on the STC89C52 single-chip microcomputer, and designs and implements the stepper motor remote control system based on the single-chip microcomputer.

1.2 Analysis of current situation at home and abroad

1.2.1 Current status of domestic technology

my country's research on single-chip microcomputers and stepper motors started relatively late. The reason is that before the reform and opening up, my country's economic development level has been relatively backward, and the country does not have much funding for research on related technologies. Therefore, our country does not have rich practical experience in the research of single-chip microcomputer and motor control.

After the reform and opening up, my country's economic strength has been significantly improved in a short period of time. In this context, my country has strengthened the technical research on single-chip microcomputer and motor control, which has significantly improved the technical level of single-chip microcomputer and motor control in a short period of time. Today, my country mainly applies single-chip microcomputers and stepping motors to the chemical industry and other fields, and has achieved good results.

In the early stage of reform and opening up, my country mainly studied the subdivision drive technology of stepping motors. With the continuous investment in scientific research and development, my country has now realized the design of common driving methods such as single-chip chopper constant current drive and single-chip DC voltage drive. In addition, with the efforts of many scientific researchers, our country has been able to use assembly language for software development, and in this way, the accuracy of motor control has been further improved.

1.2.2 Current status of foreign technologies

Regarding the research on single-chip microcomputer and motor control, foreign countries have relatively rich practical experience. The fundamental reason is that foreign research in these fields started relatively early, and with sufficient funds, foreign researchers can carry out scientific research with more peace of mind. From the current point of view, the research direction of developed countries such as the United States and Japan is mainly the direction of stepper motor control and drive. Foreign researchers said that reducing the size of the driver can effectively improve the control accuracy of the stepper motor control system. For developed countries such as the United States, there are two types of typical chips, one is mainly used to realize the motor acceleration and deceleration functions and forward and reverse functions, and the other is mainly used to reduce the occurrence of motor out-of-step and overshoot. For example, Japan has developed a chip called ppmclOlb in recent years, which has been proved to have the function of reducing the out-of-step rate and overshoot rate of the motor.

1.3 Research content

The main contents of this design are as follows:

(1) Summarize the status quo of stepper motors

Introduce stepper motors, analyze their advantages and disadvantages, and classify stepper motors to ensure that the stepper motor remote control system is designed and implemented on the basis of a full understanding of stepper motors. Introduce the composition of the stepper motor remote control system, the composition of the single-chip microcomputer control stepper motor remote control system and the composition of the drive system, and make a deep understanding of the single-chip microcomputer and its system development tools to establish the foundation for the design and implementation of the system.

(2) Hardware system design of motor control system

Design the hardware part of the stepper motor control remote control system, mainly including the design of the minimum system of the single chip microcomputer and the system drive circuit, and complete the hardware system design of the motor control system.

(3) Software system design of motor control system

To design the software part of the stepper motor remote control system, first analyze the system software design requirements, and complete the design of the control system software design process. Secondly, according to the system software design requirements, write the system software source program, and further analyze the software source program. Finally, the software program is debugged and simulated to complete the final design and implementation of the stepper motor remote control system.

 

 

Chapter 2 Overview of Stepper Motor Remote Control System

2.1 Introduction to stepping motor

Academia defines a stepper motor as a digital converter and an angle converter, as well as a digital-to-analog converter with serial characteristics. The role of the stepper motor is of great significance, and it is often used as the main control element in the process control and electrical instrumentation to control the system. The stepper motor has a staggered tooth structure, which is the main reason why the stepper motor can rotate. The application of stepper motors can realize precise control and positioning of the system, so stepper motors are mainly widely used in precise positioning systems.

2.2 Performance characteristics and advantages and disadvantages of stepper motors

2.2.1 Performance characteristics

The main performance characteristics of the stepper motor are: (1) The stepper motor has high requirements on the working environment temperature, and if the temperature does not meet the requirements, it will not be possible to ensure that the magnetic material can reach the demagnetization point. In order to ensure the stable operation of the stepper motor, it is generally necessary to place the stepper motor in a relatively high temperature environment. (2) In the case of high speed, the stepping motor is difficult to start, and the starting probability is small (3) The control accuracy of the stepping motor is high, which is between 3% and 5% of the step angle and there is no cumulative error. (4) The speed continues to rise, and the torque of the stepping motor drops rapidly.

2.2.2 Advantages and disadvantages

The advantages of stepper motors are: the control of stepper motors is relatively simple and convenient, with obvious digital characteristics, and the open-loop control of the motor can be realized through digital signals. The stepper motor itself has relatively few parts, high reliability, and the service life is in the same line as the bearing life. The stepper motor can choose the step angle in a large range, and it can still run with high torque in the case of a small step. The structure of the stepper motor is very simple, the cost of use and development is low, and it is an economical motor.

The disadvantage of the stepper motor is: if there is improper control, it is easy to generate resonance phenomenon, which will have a certain impact on the system. The size and weight advantages of stepping motors are not obvious, and the energy utilization efficiency is relatively low. In the case of exceeding the load, the synchronous control of the stepping motor will be affected, and the ordinary AC power supply cannot be used to drive the stepping motor.

2.3 Classification of stepper motors

There are various types of stepper motors, and stepper motors can be classified as many types from different perspectives. In a broad sense, stepper motors can be divided into electromagnetic stepper motors, mechanical stepper motors, and combined stepper motors. According to the structural characteristics of the motor, stepper motors can be divided into permanent magnet stepper motors, hybrid stepper motors, and reactive stepper motors.

2.4 Common stepper motors

2.4.1 Reactive stepper motor

Figure 2.1 below is a schematic diagram of the internal structure of the reactive motor:

 

5bddf47d693543f787192b3d46f94ade.png

 

Figure 2.1 Schematic diagram of the internal structure of the reactive motor

Reactive stepper motor is a commonly used type of stepper motor. The working principle of this type of motor is that the reluctance torque generated by the magnetic flux to minimize the reluctance of the path forces the motor to rotate, thereby realizing remote control of the motor.

2.4.2 Four-phase stepping motor

The four-phase stepper motor is a commonly used type of stepper motor. The structure of this type of stepper motor is similar to that of ordinary motors, including stators, rotors, and stator windings. The difference is that the stator winding of a four-phase stepper motor is divided into four sections, which is where the name of this type of motor comes from. When the motor is energized in the direction of ABCD, the four-phase stepping motor will rotate forward, otherwise the four-phase stepping motor will rotate in reverse. The schematic diagram of the internal structure of the four-phase stepping motor is shown in Figure 2.2 below:

 

373ec7bc5fdf4db6b7540bd22f4e5ee8.png

 

Figure 2.2 Schematic diagram of the internal structure of a four-phase stepping motor

2.5 Composition of stepper motor remote control system

In general, the stepper motor remote control system is mainly composed of motion control components, drive components and motion execution components. The composition of the stepper motor remote control system is shown in Figure 2.3 below:

c70dcf4fa9474f8796ae5e2c6ab8cbb3.png

Figure 2.3 Composition of stepper motor remote control system

 

 

 

 

 

 

 

 

The stepper motor is a commonly used control element, and the stepper motor control system is a complete organic whole. Among them, the operating system and motion control system are the main components of the stepper motor control system. The function of the operating system is to convert the operations performed by the operator into electrical signals. After the electrical signals are applied to the motion control system, the motion system responds accordingly. The focus of motion control is to control the position and speed of objects. The control part, drive part and execution part are the main components of the motion control system.

2.6 Single-chip microcomputer control stepper motor remote control system

Single-chip microcomputer is often used in the design of stepping motor remote control system. The stepping motor remote control system based on single-chip microcomputer mainly includes single-chip microcomputer system, driving circuit, stepping motor and peripheral circuits. The stepper motor remote control system controlled by single chip microcomputer is shown in Figure 3.2 below:

 

 

 

8aacce0d845c47739d82e42d1ae47466.png

 

Figure 3.2 Stepping motor remote control system controlled by single chip microcomputer

The hardware structure of the motor control system is very complicated, and it needs to be designed at a high cost. After the single-chip microcomputer is applied, the software control of the driving motor can be realized, and the speed, rotation angle and number of rotations of the stepping motor can also be precisely controlled by programming. At the same time, after the single-chip microcomputer is applied, it can also realize accurate control of the running state of the stepping motor, so it can meet the different needs of different users. Moreover, the application of the single-chip microcomputer makes the circuit of the stepper motor remote control system simpler and reduces the cost of system design.

 

 

Chapter 3 Hardware System Design of Motor Control System

According to the design requirements of the stepper motor remote control system, the block diagram of the stepper motor remote control system is designed, as shown in Figure 3.1 below:

d689eba4d8db44cfbf6943a061d0df33.png

 

 

Figure 3.1 Block diagram of stepper motor remote control system

After the design of the block diagram of the stepper motor remote control system is completed, this chapter will explain the structure and function of the system, the minimum system of the single-chip microcomputer, the design of the system drive circuit, and the working principle of the system in turn.

3.1 System schematic diagram

Stepper motors are mainly used in automatic industrial control systems. Under the action of single-chip microcomputers, the operation of stepper motors will be effectively controlled. Applying the minimum system of single-chip microcomputer and a small number of peripheral devices, a stepper motor controller with high working stability and reliable overall performance can be formed. The overall design circuit diagram of the system is shown in Figure 3.2 below. After the controller is applied, the precise control of the forward rotation, reverse rotation and stop of the stepper motor can be realized.

After energizing the stepper motor controller circuit, the motor circuit can be reset, or the circuit can be reset through the switch button. At the same time, by operating the touch button and the independent button, the forward rotation, reverse rotation and stop of the stepping motor can be realized. In addition, by designing the software program of the single-chip microcomputer, the change of the step angle of the stepping motor can be realized. After setting the delay time constant for the motor, the speed of the motor can be controlled according to the difference of the delay constant. Generally speaking, the smaller the delay time constant, the faster the motor speed, and vice versa.

 

13a2dbd0919b42948302bb11175b2141.png

 

Figure 3.2 The overall circuit diagram of the stepper motor controller

3.2 Minimum single chip system

 

ffa9c74240c5475b85c6913b37585ef7.png

 

Figure 3.3 Minimum system circuit of single chip microcomputer

Combining the minimum system of STC89C52 single-chip microcomputer with a small number of peripheral devices can complete the design and implementation of the stepping motor remote control system based on single-chip microcomputer. In the system, the single-chip microcomputer mainly plays a control role, so the single-chip microcomputer is the core part of the control system. Connect the relevant minimum hardware that can make the single-chip microcomputer run, and then form and realize the design of the minimum system circuit diagram of the single-chip microcomputer. The minimum single-chip microcomputer system designed in this paper is mainly composed of clock crystal oscillator circuit, reset circuit and single-chip microcomputer. The minimum single-chip microcomputer system circuit is shown in Figure 3.3 below:

In the smallest single-chip microcomputer system, the circuit that generates the clock signal is a clock crystal oscillator circuit, and the crystal oscillator frequency is 12MHz. This type of single-chip microcomputer mainly has two external crystal oscillator pins, named XTAL1 and XTAL2. The clock crystal oscillator circuit of the microcontroller is shown in Figure 3.4 below:

 

299c328a67e8413386cd69be2ef749be.png

 

Figure 3.4 Microcontroller Clock Crystal Oscillator Circuit

 

baf2c211016e493487013253d58ce0cd.png

 

Figure 3.5 MCU reset circuit

In addition, the reset circuit is an important part of the single-chip microcomputer circuit, which mainly realizes the initialization operation function of the single-chip microcomputer. By keeping the high level on the reset circuit pin continuously for more than two cycles, the reset of the single chip microcomputer can be realized. The microcontroller reset circuit is shown in Figure 3.5 below:

Connect the single-chip microcomputer chip, reset circuit and crystal oscillator circuit to complete the production and realization of the most system circuit board of the single-chip microcomputer.

3.3 System drive circuit design

The stepper motor drive control system generally consists of five parts, namely the pulse signal part, the signal distribution part, the power amplification part, the stepper motor part and the load part. The composition of the drive control system is shown in Figure 3.6 below:

 

 

b4a389227d7a4b8cbd9ad88287265e9f.png

 

 

Figure 3.6 Schematic diagram of connection between ULN2003 chip and stepping motor

In the motor control system, the single-chip microcomputer is mainly responsible for generating pulse signals. After the pulse signal is generated, the distribution and control of the pulse signal can be realized through software programming and the use of a special pulse distributor. After adding the power amplifier circuit, the single chip microcomputer can be directly connected with the stepper motor.

In general, the pulse signal directly output by the single chip microcomputer is relatively small, and the small pulse signal cannot drive the motor to work normally. Therefore, in the actual control system design process, the signal directly output by the microcontroller cannot be directly connected to the stepping motor, and it is necessary to complete the control of the stepping motor with the assistance of the drive circuit. This paper mainly uses the ULN2003 high withstand voltage and high current Darlington series chip to design the driver of the single chip microcomputer. The ULN2003 chip has a relatively high operating voltage and a maximum sink current of 0.5A, and can withstand a maximum voltage of 50V. Applying the ULN2003 chip can realize a good connection between the single-chip microcomputer and the stepping motor, and form a stepping motor driver board. The driving circuit is shown in Figure 3.7.

 

1d0276bce8cc4a30ab41834e435416be.png

 

Figure 3.7 Drive circuit

3.4 System working principle

Generally speaking, the stepper motors in the control circuit are all four-phase stepper motors. In order to improve the load capacity of the stepper motors and ensure the stable and efficient operation of the remote control system of the stepper motors, this paper applies the eight-beat drive method to Come in, the motor control system is designed.

The control word of the four-phase eight-step power-on mode of the stepping motor can be represented by the following table 3.1:

Table 3.1 Control word of four-phase eight-beat power-on mode of stepping motor

power on state

P1.3

P1.2

P1.1

P1.0

control word

A

1

1

1

0

E

AB

1

1

0

0

C

B

BC

1

1

C

CD

1

1

D

AND

1

1

The power sequence of the four-phase eight-shot stepper motor is:

First, the A-phase coil is energized, and then the energization conversion is performed, so that the A and B phase coils are energized at the same time.

Next, the B-phase coil is energized.

Finally, the B and C two-phase coils are energized at the same time, and the rest of the steps are omitted. The power-on method and process can be described as A-AB-B-BC-C-CD-D-DA. After the stator windings in the stepping motor are energized in a certain order, the motor rotor will rotate in a certain direction according to the set program.

 

 

 

Chapter 4 Software System Design of Motor Control System

4.1 Relationship between motor speed and time

 

According to the design requirements of the stepper motor remote control system, the following figure 4.1 shows the relationship between the stepper motor speed and time:

 

 

942b5aa8dc1848c8a6cabe8f1465ec4b.png

 

 

 

 

 

Figure 4.1 The relationship between stepping motor speed and time

After driving the stepper motor, the speed of the rotor in the stepper motor gradually increases as time changes. After the rotor speed reaches the maximum value, it moves randomly at a uniform speed. After the power is turned off, the stepper motor stops, and the rotor does not stop immediately, but it takes a period of time to come to a complete stop.

4.2 System software design flow chart

 

 

 

 

 

345c693225e140bba26b0b87fb01e956.png

Figure 4.2 Flow chart of software design for single-chip microcomputer control stepper motor

The software control process of the single-chip microcomputer controlling the stepping motor is shown in Figure 4.2, and thus the process of the single-chip microcomputer controlling the stepping motor for variable speed movement is shown in Figure 4.3.

 

 

 

 

 

 

 

 

 

 

 

 

6f214257b8ee4acbba16265ef4fe975e.png

 

 

 

 

Figure 4.3 The specific flowchart of single-chip microcomputer control stepping motor

The flow chart shows that after the power is turned on, the control of the forward rotation, reverse rotation, stop and speed conversion of the control motor can be realized through the set function keys.

4.3 Analysis of software design source program

The source code of the software design of the single-chip stepper motor remote control system is shown in the appendix. The source program sets a stack for the P2 port of the microcontroller, and the stepper motor is in a stalled state at the beginning. At this time, the source program will scan the keys, and the purpose of scanning is mainly to judge whether there is a key pressed. If there is a button press, the button often needs to switch between the open state and the closed state, and it takes a certain period of time before it can be in a stable closed state. This process is called button shaking. At the same time, after the user presses the button for 10ms, the button vibration will disappear. After the user presses different buttons, the corresponding buttons will jump to different modules. At this time, the system will call the rotation program, and finally complete the precise control of the motion state of the stepper motor. For the software program of the stepper motor remote control system controlled by the single-chip microcomputer, it is mainly written in C language.

 

 

Chapter 5 System Debugging

5.1 Program debugging simulation

In order to ensure the effect of system software programming and achieve the goal of precise control of motor movement, it is usually necessary to debug and simulate the designed software program. Software program debugging and simulation are mainly accomplished through a series of simulation software that cooperate with each other. After applying various simulation software, the simulation of the stepper motor and its operating state can be realized, and online debugging of the operation of the single-chip microcomputer can also be carried out. This paper uses the proteus simulation software to complete the simulation design of the stepper motor remote control system controlled by the microcontroller. The simulation results are shown in Figure 4.4 below:

 

cc034d7be7de424c9f955c5383a6d68d.png

 

Figure 5.1 Simulation effect diagram of the control system

 

 

 

 

 

5.2 Hardware debugging

(1) The actual hardware is shown in Figure 5.2:

 

74e34b7da82f4f6b8e414835aa20c1b4.png

 

Figure 5.2 Hardware object

(2) The initial state of using USB to plug in the power supply is shown in Figure 5.3. At this time, the input power supply voltage measured by a multimeter is 5.1V, which is the normal working voltage of the single-chip microcomputer, and the next step can be performed.

 

5bbec1ba0a61465fb12ef969d2e391b5.png

 

Figure 5.3 Initial state

 

(3) On the infrared remote control, press the command to rotate forward one circle, and the stepper motor will rotate forward one circle according to the set speed, and the rotation situation will be displayed on the screen as shown in Figure 5.4:

 

9812d216ebf54a139c8d2562a964f77e.png

 

Figure 5.4 Forward rotation

(4) Press the reverse rotation command on the infrared remote control, and the stepping motor rotates reversely according to the set speed, and the rotation situation is displayed on the screen as shown in Figure 5.5:

 

a546d95be3e54450ae45b20537d5f05c.png

 

Figure 5.5 Inversion case

(5) The two rotation situations in the above examples have realized the basic motion situation of the stepper motor remotely controlled by infrared remote control, and the physical debugging is basically completed.

 

Chapter 6 Summary

As the most commonly used control element in remote control, stepper motors are widely used. The application of stepper motors can realize precise control of the motion status of different instruments and equipment. After applying the STC89C52 single-chip microcomputer in this design, the design and realization of the remote control system of the stepper motor are completed. After applying the microcontroller, this design realizes the precise control of the motion state of the stepper motor. Through simulation and debugging, the functions of stepper motor reverse rotation, forward rotation, speed conversion and stop are realized. The motor control system designed in this paper has certain practical value and reference value, and I hope it can be helpful to the research work of relevant personnel.

The role of stepper motors in the chemical industry and the manufacturing industry is of great significance, so they are very popular in these industries. With the continuous improvement of the level of science and technology, with the assistance of many technologies, the application range of stepper motors will become wider and wider. The development prospects of stepping motors are good, and the application value of stepping motors and single-chip microcomputers will become higher and higher in the future.

 

 

all codes

 

#include<reg52.h>

#include<stdio.h>

#include<intrins.h>



#define uchar unsigned char

#define uint unsigned int



sbit RS = P2^4;//1602数据/命令选择端(H:数据寄存器L:指令寄存器)

sbit RW = P2^5;//1602读/写选择端

sbit E  = P2^6;//1602使能信号端



sbit key1=P3^0;//用户按键

sbit key2=P3^1;

sbit key3=P3^2;

sbit key4=P3^3;

sbit key5=P3^4;



uchar code B_Rotation[8]={0x7f,0x3f,0xbf,0x9f,0xdf,0xcf,0xef,0x6f}; //反转表格

uchar code F_Rotation[8]={0xef,0xcf,0xdf,0x9f,0xbf,0x3f,0x7f,0x6f}; //正转表格





uchar table1[]="dir: +  spe:005";

uchar table2[]="cir:000 ang:000";



uchar num;

uint key,flag,speed=5,zflag,znum,fflag,fnum,select,pp,qq;

//1键值2键值标志3速度4正转标志5正转圈数6反转标志7反转圈数8正转角度9反转角度10选择

float zang,fang;

/******************************************************************/

/*                    延时函数 Xms                                   */

/******************************************************************/

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

/******************************************************************/

/*                   LCD1602写命令操作                            */

/******************************************************************/

void WriteCommand(uchar com)

{

delay(5);//操作前短暂延时,保证信号稳定

E=0;

RS=0;

RW=0;

P0=com;

E=1;

delay(5);

E=0;

}

/******************************************************************/

/*                   LCD1602写数据操作                            */

/******************************************************************/

void WriteData(uchar dat)

{

delay(5);  //操作前短暂延时,保证信号稳定

E=0;

RS=1;

RW=0;

P0=dat;

E=1;

delay(5);

E=0;

}

/******************************************************************/

/*                           1602初始化程序                       */

/******************************************************************/

void InitLcd()//1602初始化程序

{

delay(15);

WriteCommand(0x38); //display mode

WriteCommand(0x38); //display mode

WriteCommand(0x38); //display mode

WriteCommand(0x06); //当读或写一个字符后地址指针加一,且光标加一

WriteCommand(0x0c); //显示开及光标不显示

WriteCommand(0x01); //清屏



WriteCommand(0x80);

for(num=0;num<15;num++)//第一行的显示

{

WriteData(table1[num]);

delay(15);

}

WriteCommand(0x80+0x40);//第二行的显示

for(num=0;num<15;num++)

{

WriteData(table2[num]);

delay(15);

}



}

/******************************************************************/

/*              独立键盘扫描函数                                  */

/******************************************************************/

void keyscan()  

{



P3=0xff;//拉高P3口,以读取P3口的值

if(key1==0)//键1被按下

{

delay(5);//延时消抖

if(key1==0)

{

flag=1;

}

while(!key1);

delay(5);//延时消抖

while(!key1);//松手检测

}

if(key2==0)

{

delay(5);

if(key2==0)

{

flag=2;

}

while(!key2);

delay(5);

while(!key2);

}

if(key3==0)//键1被按下

{

delay(5);//延时消抖

if(key3==0)

{

flag=3;

}

while(!key3);

delay(5);//延时消抖

while(!key3);//松手检测

}

if(key4==0)

{

delay(5);

if(key4==0)

{

flag=4;

}

while(!key4);

delay(5);

while(!key4);

}

if(key5==0)

{

delay(5);

if(key5==0)

{

flag=5;

}

while(!key5);

delay(5);

while(!key5);

}

}

/******************************************************************/

/*                           1602显示3位数                       */

/******************************************************************/

void write_sfm(uchar add,uint date)//1602显示

{

uchar bai,shi,ge;

bai=date/100;

shi=date%100/10;

ge=date%10;

WriteCommand(0x80+add);//设置数据地址指针

WriteData(0x30+bai);

WriteData(0x30+shi);

WriteData(0x30+ge);

WriteData(0x20);

}

/******************************************************************/

/*                           键值处理                             */

/******************************************************************/

void handle_flag()

{



uchar select1;

if(TR0==0&&TR1==0)

{

if(flag==1) //正转

{

WriteCommand(0x80+0x05);

WriteData('+');

delay(20);

select=0;

}

if(flag==2) //反转

{

WriteCommand(0x80+0x05);

WriteData('-');

delay(20);

select=1;

}

if(flag==3)//转速加

{

     speed++;

if(speed>=12)

speed=12;

write_sfm(0x0c,speed);//显示速度

delay(20);

     }

if(flag==4) //转速减

{

speed--;

if(speed<=1)

speed=1;

write_sfm(0x0c,speed);//显示速度

delay(20);

}

}

if(flag==5)关闭中断,停止电机

{

select1++;

select1%=2;

if( select1==0)

{

TR0=0;

TR1=0;

if(select==0)//显示转动的角度

write_sfm(0x40+0x0c,zang);

else

write_sfm(0x40+0x0c,fang);

      }

else

{if(select==0)

{

TR0=1;//开定时器0,产生正转相序

TR1=0;//关定时器1

}

else

{

TR1=1;//开定时器1,产生反转相序

TR0=0;//关定时器0

}

}

}

flag=0;

}





void main()

{



pp=0;

qq=0;

    

InitLcd();

TMOD=0x11;

    EA=1;

ET0=1;

ET1=1;



TH0=(65536-(14648/speed))/256;

TL0=(65536-(14648/speed))%256;

TH1=(65536-(14648/speed))/256;

TL1=(65536-(14648/speed))%256;



TR0=0;

TR1=0;



while(1)

{

keyscan();

handle_flag();

}



}



void time0() interrupt 1

{

uint zz;

TH0=(65536-(14648/speed))/256; // 360/(5.625/64)=4096    

                                   //假设60秒转一圈,即60秒中断4096次,

   //那么一次中断需要60000000/4096=14648个时钟

   //60000000/(4096*speed)   zflag=4096时一圈

TL0=(65536-(14648/speed))%256;

zz%=8;

P1=F_Rotation[zz];  //输出对应的相

zz++;

zflag++;

if(zflag==4096) //正转了一圈

{

zflag=0;

znum++;

write_sfm(0x40+0x04,znum);

}

if(zflag%64==0)//计算正转动的角度

{

pp++;

zang=(uint)(pp*5.625);

if(zang==360)

pp=0;

write_sfm(0x40+0x0c,zang);

}

//zang=0.088*zflag;

//write_sfm(0x40+0x0c,zang);

}

void time1() interrupt 3

{



uint ff;

TH1=(65536-(14648/speed))/256;

TL1=(65536-(14648/speed))%256;

    ff%=8;

P1=B_Rotation[ff]; //输出对应的相

ff++;

fflag++;

if(fflag==4096) //反转了一圈

{

fflag=0;

fnum++;

write_sfm(0x40+0x04,fnum);

}

if(fflag%64==0)//计算反转动的角度

{

qq++;

fang=(uint)(qq*5.625);

if(fang==360)

qq=0;

write_sfm(0x40+0x0c,fang);

}

}

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_58404700/article/details/131365381