42 Stepper Motor Control MCU Program

Publisher:心灵舞者Latest update time:2020-02-20 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following is a simulation diagram:

/********************************************Copyright (c)******************************************************                            

**                                     

** -----------------------------------------------------------------------------------------------------

** File Name : 

** Version : V1.0.0

** Description:                                 

* [External crystal]: 11.0592mhz        

* 【Main control chip】: STC89C52

* 【Compilation environment】: Keil μVisio4        

* 【Program functions】:                                                                                                    

* 【Instructions for use】: 

** -----------------------------------------------------------------------------------------------------

** Author : Creavalia

** Date : October 19, 2016

** -----------------------------------------------------------------------------------------------------

** Modify Person:

** Modify Date :

** Description:

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

// Includes --------------------------------------------------------------------------------------------

#include 

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

** Marcos defines

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

#define MotorTabNum 5

unsigned char T0_NUM;

//IO settings

sbit K1 = P3^5; // Start

sbit K2 = P3^4; // reverse

sbit K3 = P3^3; // Acceleration

sbit K4 = P3^2; // deceleration


sbit FX = P2^4; // Direction

sbit MotorEn = P2^5; // Enable

sbit CLK = P2^6; // pulse


//----Digital Tube---------------

sbit SMG1 = P2^0; //definition of the first bit of digital tube

sbit SMG2 = P2^1; //Definition of the second bit of the digital tube

sbit SMG3 = P2^2; //Definition of the third bit of the digital tube

sbit SMG4 = P2^3; //Definition of the fourth bit of the digital tube


int table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //Common cathode digital tube segment value

//int table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,}; //Common anode digital tube segment value



unsigned char g_MotorSt = 0; // Start and stop

unsigned char g_MotorDir = 0; // forward and reverse


//unsigned char MotorTab[6] = {200, 160, 120, 80, 40, 20};

unsigned char MotorTab[6] = {12, 10, 8, 6, 4, 2};

//unsigned char MotorTab[6] = {6, 5, 4, 3, 2, 1};


signed char g_MotorNum = 0;


//--------------

void delayms(xms);

void mDelay(unsigned int DelayTime); //delay function

void T0_Init();


void KeyScan(void);

void Display(void);



// Main program main()

void main(void)

{

        unsigned char i;

        T0_Init(); // Timer 0 initialization

        MotorEn = 0; // L297 enable (only intelligent, can work normally, otherwise stop)

        FX = 0;

        while(1)

        {

                KeyScan(); // Button

                for(i=0; i<10; i++)

                Display();

        }


}


/********Timer initialization************************************************************/

void T0_Init()

{

        TMOD = 0x01;

        TH0 = (65535-100)/256; // 1ms

        TL0 = (65535-100)%256;

        EA = 1;

        ET0 = 1;

//TR0 = 1; 


}


/********Timer interrupt*****************/

void T0_time() interrupt 1

{

//TR0 = 0;

        TH0 = (65535-100)/256;   

        TL0 = (65535-100)%256;

        T0_NUM++;

        if(T0_NUM >= MotorTab[g_MotorNum]) // Increase key g_MotorNum++ Decrease key g_MotorNum--

        {

                T0_NUM = 0;

                CLK=CLK^0x01; // output pulse

        }

//TR0 = 1;

}         



//-----Button control---------------------

void KeyScan(void)

{

        if(K1 == 0)

        {

                delayms(5); // Software delay debounce

                if(K1 == 0)

                {

                        while(!K1){Display();}; 

                        g_MotorSt = g_MotorSt ^ 0x01;

                        MotorEn ^= 1;

                        TR0 = 1; 

                }

        }


        if(K2 == 0)

        {

                delayms(10); // Software delay debounce

                if(K2 == 0)

                {

                // while(!K2){Display();};        

                        g_MotorDir = g_MotorDir ^ 0x01;

                        FX^= 1;

                        while(!K2){Display();};        

                }

        }


        if(K3 == 0) // Accelerate

        {

                delayms(5); // Software delay debounce

                if(K3 == 0)

                {

                        while(!K3){Display();};        

                        g_MotorNum++;

                        if(g_MotorNum > MotorTabNum)

                                g_MotorNum = MotorTabNum;

                }

        }


        if(K4 == 0) // deceleration

        {

                delayms(5); // Software delay debounce

                if(K4 == 0)

                {

                        while(!K4){Display();};        

                        g_MotorNum--;

                        if(g_MotorNum < 0)

                                g_MotorNum = 0;

                }

        }



}



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

* Name: Set_Display()

* Function: Digital tube display function

* Input: None

* Output: None

* illustrate:

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

void Display(void)

        unsigned char b1, b2;

        b1=g_MotorNum/10;                

    b2=g_MotorNum%10;

        if(FX == 0)

                P0=0x71; //Display F

    else

                P0=0x79;

        SMG1=0;                                                         

    delayms(3);

    SMG1=1;


        P0=0x40;

    SMG2=0; //Display--

    delayms(3);

    SMG2=1;


        P0=table[b1];

        SMG3=0; //Display tens digit

        delayms(3);

        SMG3=1;


        P0=table[b2];

    SMG4=0;

    delayms(3); //display the units digit

    SMG4=1;

}




/************************************Delay function************************************************************/

void delayms(xms)

{

         unsigned int x,y;

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

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

}

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

** End Of File

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


Reference address:42 Stepper Motor Control MCU Program

Previous article:Single chip microcomputer temperature, humidity, light, carbon dioxide intelligent detection
Next article:MCU driving LED digital tube, key TM1638 chip learning experience

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号