Single chip microcomputer stepper motor speed control program (speed LED display)

Publisher:SparklingRiverLatest update time:2020-02-08 Source: elecfansKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The microcontroller source program is as follows:

//Digital tube high position------low position

//Four buttons to control the stepper motor: forward, reverse, plus 1, minus 1

//When the power is on, the motor starts, and the digital tube displays the minimum speed gear 1. The plus and minus gears can be displayed through the digital tube. The motor adopts single and double eight-beat mode.

//There are 10 motor speeds in total, and the speed can be adjusted by pressing the button 

//When the motor rotates forward, the highest digital tube displays 0, and when it rotates reverse, it displays 1  

#include

#define uchar unsigned char

#define uint unsigned int

#define led P0 //digital tube segment selection

#define haha ​​P2

sbit s1 = P1^0;

sbit s2 = P1^1;

sbit s3 = P3^0;

sbit s4 = P3^1;

sbit s5 = P3^2; //button definition, s1 forward, s2 reverse, s3 plus 1, s4 minus 1

sbit wei3 = P2^3;sbit wei2 = P2^2;sbit wei1 = P2^1;sbit wei0 = P2^0;//digital tube bit selection definition

sbit a = P2^7;sbit b = P2^6;sbit c = P2^5;sbit d = P2^4; // Definition of pulse signal input terminal


uchar code tab[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; // Common anode digital tube drive signal 0---9, no display

uchar code time_counter[10][2]={{0xda,0x1c},{0xde,0xe4},{0xe1,0xec},{0xe5,0xd4},{0xe9,0xbc}, //9.7 ----1ms

                                                            {0xed,0xa4},{0xf1,0x8c},{0xf5,0x74},{0xf9,0x5c},{0xfc,0x18}};

uchar code qudong[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90};         

uchar num1 = 0; //Control the excitation signal variable                                                  

uchar num2 = 8;


char k=0; //Addition and subtraction gear control, 1 is the minimum gear

char pause=1; //Save the previous speed when paused, the default setting is 1 when powered on

bit flag1 = 0; //initial forward, forward and reverse rotation flag

uchar buf[4]={0,10,0,0}; //digital tube display cache, forward, no display, no display, display 1 gear, high----low  

//=================================Timer 0/1 initialization function=================================

void T0_T1_init()

{

        TMOD = 0x11; //Timer 0/1 both work in mode 1, 16-bit timing mode

           TH1 = time_counter[k-1][0];

        TL1 = time_counter[k-1][1]; //Timer 1, 10ms timing for stepper motor speed control

        TR1 = 0;

        ET1= 1; // Enable timer interrupt

        EA = 1; // Enable general interrupt

}

//==================================ms level delay function======================================

void delay1m(uint x)

{

    uint i,j;

    for(i=0;i             for(j=0;j<120;j++); //Count 120 times, about 1 ms

void display()

{

        wei3=1;wei2=1;wei1=1;wei0=0;led = tab[buf[3]];delay1m(1);led=0xff;

    wei3=1;wei2=1;wei1=0;wei0=1;led = tab[buf[2]];delay1m(1);led=0xff;

    wei3=1;wei2=0;wei1=1;wei0=1;led = tab[buf[1]];delay1m(1);led=0xff;

        wei3=0;wei2=1;wei1=1;wei0=1;led = tab[buf[0]];delay1m(1);led=0xff;

}

//==================================Main function==============================================

void main()

{

        T0_T1_init();

        buf[1] = 10; // not displayed

        while(1)

        {

                display();

                if(s1 == 0)

                {

                        delay1m(3);

                        if(s1 == 0)

                        {

                                flag1 = 0; //Forward

                                buf[0] = 0; //The highest bit displays 0

                // haha ​​= 0x00; // stop

                                 

                        }

                        while(!s1) display();

                }

                if(s2 == 0 )

                {

                

                        delay1m(3);

                        if(s2 == 0)

                        {

                        

                                flag1 = 1; // reverse

                                buf[0] = 1; //The highest bit displays 1

                // haha ​​= 0x00; // stop

                        }

                        while(!s2) display();

                }

                if(s3 == 0) //speed up 1 level

                {

                        delay1m(3);

                        if(s3 == 0)

                        {

                                k++;

                                TR1=1;

                                if(k > 10 )

                                {

                                        k = 1;

                                }

                                buf[2]= k/10;

                                buf[3]= k%10;

                        } 

                        while(!s3) display();

                }

                if(s4 == 0) //Speed ​​reduction by 1 level

                {

                        delay1m(3);

                        if(s4 == 0)

                        {

                                k--;

                                TR1=1;

                                if(k <= 0)

                                {

                                        k = 10;

                                }

                                buf[2]= k/10;

                                buf[3]= k%10;

                        }

                        while(!s4) display();

                }

                if(s5==0)

                {

                        delay1m(3);

                        if(s5==0)

                        {

                                if(TR1==1)

                                {

                                        pause=k;

                                        k=0;

                                        TR1=0;

                                        a=b=c=d=0;

                                }

                                else if(TR1==0)

                                {

                                        k=pause;

                                        TR1=1;

                                }

                                buf[2]= k/10;

                                buf[3]= k%10;

                                while(!s5) display();

                        }

                }

        } 


}

//===================================Timer 1 interrupt function, used for pulse frequency control======================================

void time1_interrupt()interrupt 3

{        

        static num1 = 0;

        static num2 = 0;

        TH1 = time_counter[k-1][0];

        TL1 = time_counter[k-1][1]; //Timer 1, timing 1 is used for stepper motor speed control

        if(flag1 == 0) //Forward

        {

                switch(num1)

                {

                        case 0:a = 1;b = 0;c = 0;d = 0;break;

                        case 1:a = 1;b = 1;c = 0;d = 0;break;

                        case 2:a = 0;b = 1;c = 0;d = 0;break;

                        case 3:a = 0;b = 1;c = 1;d = 0;break;

                        case 4:a = 0;b = 0;c = 1;d = 0;break;

                        case 5:a = 0;b = 0;c = 1;d = 1;break;

                        case 6:a = 0;b = 0;c = 0;d = 1;break;

                        case 7:a = 1;b = 0;c = 0;d = 1;break;

                }

                num1++;

                if(num1 == 8)num1 = 0; 

        }

        else //reverse

        {

                switch(num2)

                {

                        case 0:a = 1;b = 0;c = 0;d = 1;break;

                        case 1:a = 0;b = 0;c = 0;d = 1;break;

                        case 2:a = 0;b = 0;c = 1;d = 1;break;

                        case 3:a = 0;b = 0;c = 1;d = 0;break;

                        case 4:a = 0;b = 1;c = 1;d = 0;break;

                        case 5:a = 0;b = 1;c = 0;d = 0;break;

                        case 6:a = 1;b = 1;c = 0;d = 0;break;

                        case 7:a = 1;b = 0;c = 0;d = 0;break;

                }

                num2++;

                if(num2 == 8)num2 = 0;         

        }

}

1. This design uses STC89C51/52 (compatible with AT89S51/52, AT89C51/52, optional) microcontroller as the main controller;

· 2. The motor uses a DC-5V stepper reduction motor (step angle 5.625°, reduction ratio 1/64);

3. Integrated chip ULN2003 as motor driver;

4. Button functions: Button 1 forward, Button 2 reverse, Button 3 speed up, Button 4 speed down, Pause/Start

· 5. The first digit of the LED digital tube displays the forward and reverse rotation of the motor, and the third and fourth digits display the gear number of the motor running speed;

6. Five red LEDs, one for power indication and four for motor speed indication.   

     Partial Program

//Digital tube high position------low position

//Four buttons to control the stepper motor: forward, reverse, plus 1, minus 1

//When the power is on, the motor starts, and the digital tube displays the minimum speed gear 1. The plus and minus gears can be displayed through the digital tube. The motor adopts single and double eight-beat mode.

//There are 10 motor speeds in total, and the speed can be adjusted by pressing the button

//When the motor rotates forward, the highest digital tube displays 0, and when it rotates reverse, it displays 1  

#include

#define uchar unsigned char

#define uint unsigned int

#define led P0 //digital tube segment selection

#define haha ​​P2

sbit s1 = P1^0;

sbit s2 = P1^1;

sbit s3 = P3^0;

sbit s4 = P3^1;

sbit s5 = P3^2; //button definition, s1 forward, s2 reverse, s3 plus 1, s4 minus 1

sbit wei3 = P2^3;sbit wei2 = P2^2;sbit wei1 = P2^1;sbit wei0 = P2^0;//digital tube bit selection definition

sbit a = P2^7;sbit b = P2^6;sbit c = P2^5;sbit d = P2^4; // Definition of pulse signal input terminal

uchar code tab[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; // Common anode digital tube drive signal 0---9, no display

uchar code time_counter[10][2]={{0xda,0x1c},{0xde,0xe4},{0xe1,0xec},{0xe5,0xd4},{0xe9,0xbc}, //9.7 ----1ms


  • QQ browser screenshot 20190509180725.png

  • QQ browser screenshot 20190509180520.png



Keywords:MCU Reference address:Single chip microcomputer stepper motor speed control program (speed LED display)

Previous article:STC MCU + DM134 to realize LED multi-segment control
Next article:Based on 52 single chip microcomputer control encoder display 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号