28BYJ-48 Stepper Motor Control Program Basics

Publisher:PeacefulWarriorLatest update time:2016-12-24 Source: eefocusKeywords:28BYJ-48 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

After solving the accuracy problem, let's go back to our motor control program. The two routines given above are not practical programs. Why? Because there are long delays in the program, and nothing else can be done during the delay. Think about the second program, nothing else can be done for 200 seconds, which is absolutely not allowed in the actual control system. So how to modify it? Of course, it is still done with timer interrupts. Since each beat lasts for 2ms, we can directly use the timer to time 2ms to refresh the beat. The modified program is as follows:

#include

unsigned long beats = 0; //Total number of motor rotation beats

void StartMotor(unsigned long angle);

void main(){

    EA = 1; // Enable general interrupt

    TMOD = 0x01; //Set T0 to mode 1

    TH0 = 0xF8; //Assign the initial value 0xF8CD to T0, and set the timing to 2ms

    TL0 = 0xCD;

    ET0 = 1; // Enable T0 interrupt

    TR0 = 1; //Start T0

    StartMotor(360*2+180); //Control the motor to rotate 2 and a half circles

    while (1);

}

/* Stepper motor start function, angle-the angle to be rotated*/

void StartMotor(unsigned long angle){

    // Turn off interrupts before calculation and turn them on after completion to avoid interrupts interrupting the calculation process and causing errors

    EA = 0;

    beats = (angle * 4076) / 360; //The actual measurement is 4076 beats per rotation

    EA = 1;

}

/* T0 interrupt service function, used to drive the stepper motor to rotate*/

void InterruptTimer0() interrupt 1{

    unsigned char tmp; //temporary variable

    static unsigned char index = 0; //beat output index

    unsigned char code BeatCode[8] = { //IO control code corresponding to the stepper motor beat

        0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6

    };

    TH0 = 0xF8; //Reload initial value

    TL0 = 0xCD;

    //If the beat number is not 0, a driving beat is generated

    if (beats != 0){

        tmp = P1; //Use tmp to temporarily store the current value of port P1

        tmp = tmp & 0xF0; // Use & to clear the lower 4 bits

        //Use the | operation to write the beat code to the lower 4 bits

        tmp = tmp | BeatCode[index];

        //Send the lower 4-bit beat code and the upper 4-bit original value back to P1

        P1 = tmp;

        index++; //beat output index increment

        index = index & 0x07; //Use & operation to return to 8

        beats--; //Total beats - 1

    }else{ //If the number of beats is 0, turn off all phases of the motor

        P1 = P1 | 0x0F;

    }

}

The program is relatively simple. The motor startup function StartMotor is only responsible for calculating the total number of beats required, and then checking this variable in the interrupt function. If it is not 0, the beat operation is performed and it is reduced by 1 until it is reduced to 0.


Here, we need to explain the two operations on EA in the StartMotor function. We can see that the assignment calculation statement for beats is sandwiched between the two statements EA=0;EA=1;, which means that this line of assignment calculation statement turns off the interrupt before execution, and turns it back on after it is executed. During its execution, the microcontroller will not respond to the interrupt, that is, the interrupt function InterruptTimer0 will not be executed. Even if the timer overflows and an interrupt occurs, it can only get a response after EA is reset to 1, and the interrupt function InterruptTimer0 will be executed.


So why do we do this? Let's think about it: At the beginning of this book, we mentioned that the STC89C52 microcontroller we use is an 8-bit microcontroller. The concept of 8 bits means that the microcontroller operates data in 8 bits, that is, 1 byte. Therefore, to operate multiple bytes (whether reading or writing), it must be done multiple times. The beats variable defined in our program is an unsigned long type, which occupies 4 bytes, so its assignment must be completed at least 4 times. Let's imagine that after completing the assignment of the first byte, an interrupt happens to occur, and the InterruptTimer0 function is executed. In this function, beats may be subtracted by 1. The subtraction may cause a borrow, and the borrow will change other bytes. However, because the other bytes have not been assigned new values ​​at this time, an error will occur, and the result of subtracting 1 is not the expected value! Therefore, to avoid this error, you must temporarily turn off the interrupt and turn it on again after the assignment is completed. If we use char or bit variables, because they are completed in one operation of the CPU, no error will occur even if the interrupt is not disabled. The problem has been analyzed clearly, and how to choose depends on the actual situation. When encountering such problems, please consider them more.


Keywords:28BYJ-48 Reference address:28BYJ-48 Stepper Motor Control Program Basics

Previous article:Simple Addition Calculator
Next article:Practical Motor Control Program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号