Simulation and Program of Stepper Motor Controlled by STC89C52RC MCU

Publisher:吾道明亮Latest update time:2019-12-05 Source: 51heiKeywords:STC89C52RC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This single chip microcomputer is STC89C52RC, this program is applied to step motor--28BYJ, a small stepper motor, with a power supply voltage of DC5V. It can be directly connected to the single chip microcomputer and driven by the UL2003 driver chip. The digital tube is a common cathode interface, which controls the start/stop, forward and reverse rotation, acceleration and deceleration of the stepper motor, and has a running and conversion status display.

It is a good routine for beginners.


Physical map:
 
The simulation schematic is as follows

 

The microcontroller source program is as follows:

/*This microcontroller is STC89C52RC, this program is applied to step motor--28BYJ, a small stepper motor,

   The power supply voltage is DC5V, which can be directly connected to the microcontroller and driven by the UL2003 driver chip. The digital tube is a common cathode interface. */



#include

#define uchar unsigned char //data macro definition

#define uint unsigned int

#define A P10 //

#define _A P11 //Stepper motor pulse interface

#define B P12 //

#define _B P13 //Stepper motor pulse interface

uchar su_du=0; //speed

bit fang_xiang=0; //direction

bit qi_dong=0; //Start flag

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


#include //Contains the shift standard library function header file



sbit DU = P2^6; //digital tube segment selection

sbit WE = P2^7; //digital tube segment selection



//Common cathode digital tube segment selection table 0-9

uchar code shu_ma[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};

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

/*====================================

Function: delay(uint z)

Parameter: z delay setting in milliseconds, value range 0-65535

Return value: None

Description: 12T/Fosc11.0592M millisecond delay

=======================================*/

void delay(uint z)

{

        uint x,y;

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

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


/*====================================

Function: display(uchar i)

Parameter: i displays the value, the value range is 0-255

Return value: None

Description: Three-digit common cathode digital tube dynamic display

=======================================*/

void display(uchar i) //data processing

{

        uchar bai, shi, ge;

// bai = i / 100; //236 / 100 = 2

// shi = i % 100 / 10; // 236 % 100 / 10 = 3

        ge = i; // % 10; // 236 % 10 = 6

        

/* First digit of digital tube                  

        P0 = 0XFF; // Clear broken code

        WE = 1; //Open the bit select latch

        P0 = 0XFE; //1111 1110

        WE = 0; // latch bit select data

        

        DU = 1; //Open the segment select latch

        P0 = shu_ma[bai];//

        DU = 0; // latch segment selection data

        delay(5);


        //The second digital tube

        P0 = 0XFF; // Clear broken code

        WE = 1; //Open the bit select latch

        P0 = 0XFD; //1111 1101

        WE = 0; // latch bit select data

        

        DU = 1; //Open the segment select latch

        P0 = shu_ma[shi]; //

        DU = 0; // latch segment selection data

        delay(5);*/


/********* The third digital tube*/

        P0 = 0XFF; // Clear broken code

        WE = 1; //Open the bit select latch

        P0 = 0XFB; //1111 1011

        WE = 0; // latch bit select data

        

        DU = 1; //Open the segment select latch

        P0 = shu_ma[ge]; //

        DU = 0; // latch segment selection data

        delay(5);

}

void yanshi2(uint a) //delay program

{

        while(a--);

}

uchar an_jian() //Key detection, P3.0, P3.1, P3.2, P3.3 are connected to 4 keys respectively

{

        if((P3&0x0f)!=0x0f)//As long as one key of P3 is pressed, the key value will be returned after a delay.

        {

                yanshi2(20000);

                if((P3&0x0f)!=0x0f)

                {

                        return P3&0x0f;

                }

        }

        return P3&0x0f;

}


void yanshi() //Stepper motor delay time

{

        uint sd1 = 0;

        sd1=8000+5753*(10-su_du); // The larger the su_du value, the shorter the period and the higher the frequency

        while(sd1--)

        {

                if((P3&0x0f)!=0x0f)return; //Judge whether a key is pressed in the delay, and return to the main function directly if it is pressed

        }

}

void bu_jin() //Each time the stepper motor is called, it moves one step in the specified direction

{

        static char j=1; //Set the starting speed to 1.

        if(fang_xiang) //Judge the direction bit, if it is 1, cycle in positive phase sequence,

        {

                j++;

                if(j==5)j=1;

        }

        else

        {

                j--;

                if(j==0)j=4; //Otherwise, loop in reverse order

        }

        switch(j) //Select different voltage combinations according to the number of beats

        {

                case 1:

                        A=1;B=1;_A=0;_B=0;yanshi(); //ab 1100

                break;

                case 2:

                        A=0;B=1;_A=1;_B=0;yanshi(); //a~b 0110

                break;

                case 3:

                        A=0;B=0;_A=1;_B=1;yanshi(); //a~b~0011

                break;

                case 4:

                        A=1;B=0;_A=0;_B=1;yanshi(); //ab~ 1001

                break;

        }

}




        


void main()

{

        P14=0; //Direction sign

        P15=0; //Start flag

        while(1)

        {

                switch(an_jian()) //key processing

                {

                        case 0x0e: //The first button is pressed to invert the button status and start/stop

                                qi_dong=!qi_dong;

                                P15=qi_dong; //status indication

                        break;

                        case 0x0d: //The second button is pressed, the speed increases

                                if(su_du<9)

                                        su_du++; //speed increase

                        break;

                        case 0x0b: //The third button is pressed, the speed decreases

                                if(su_du>0)

                                        su_du--; //speed reduction

                        break;

                        case 0x07: //The fourth button is pressed to invert the button state and change direction

                                fang_xiang=!fang_xiang;          

                                P34=fang_xiang; //Direction indicator light

                        break;

                }

        

                if(qi_dong) //Start button is 1

                {

                        bu_jin(); //Stepper motor

                }

                else P1=0X0F; //Stop state, all pulse output ports stop outputting


                   display(su_du); //digital tube display function

……………………



Keywords:STC89C52RC Reference address:Simulation and Program of Stepper Motor Controlled by STC89C52RC MCU

Previous article:Final version of the single-chip four-story elevator control system design (including simulation + report + program source code)
Next article:Single chip fully automatic washing machine program + simulation + schematic design debugging and analysis

Latest Microcontroller Articles
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号