51 single chip microcomputer controls the forward and reverse rotation of the stepper motor

Publisher:跳跃龙珠Latest update time:2017-09-02 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is a program that uses a 51 single-chip microcomputer to control the forward and reverse rotation of a stepper motor, and can also achieve speed regulation.

#include "reg51.h"     
#include "intrins.h"

#define uchar unsigned char

#define uint unsigned int

#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};

unsigned char code FFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6}; //??

unsigned char code REV[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe}; //??

//unsigned char code FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //??

//unsigned char code REV[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //??

sbit K1 = P3^2; //????

sbit K2 = P3^3; //????

sbit K3 = P3^4; //????

sbit K4 = P3^5; //????

sbit LCD_RS = P2^0;             

sbit LCD_RW = P2^1;

sbit LCD_EN = P2^2;

bit on_off=0; //???????

bit direction=1; //????

bit rate_dr=1; //????

bit snum_dr=1; //????

uchar code cdis1[ ] = {"NO.0738230126 "};

uchar code cdis2[ ] = {" 2012/5/24"};

uchar code cdis3[ ] = {" STOP "};

uchar code cdis4[ ] = {"NUM: RATE: "};

uchar code cdis5[ ] = {" RUNNING "};

uchar m,v=0,q=0;

uchar number=0,number1=0; 

uchar snum=20,snum1=20; //?????

uchar rate=3; //?????

uchar data_temp,data_temP0,data_temp2;   

void delay(uint t)

{                           

   uchar k;

   while(t--)

   {

     for(k=0; k<124; k++)

     { }

   }

}

void delayB(uchar x) //x*0.14MS

{

   uchar i;

   while(x--)

   {

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

     { }

   }

}

 

bit lcd_busy()

{                          

    bit result;

    LCD_RS = 0;

    LCD_RW = 1;

    LCD_EN = 1;

    delayNOP();

    result = (bit)(P0&0x80);

    LCD_EN = 0;

    return(result); 

}

void lcd_wcmd(uchar cmd)

{                          

   while(lcd_busy());

    LCD_RS = 0;

    LCD_RW = 0;

    LCD_EN = 0;

    _nop_();

    _nop_(); 

    P0 = cmd;

    delayNOP();

    LCD_EN = 1;

    delayNOP();

    LCD_EN = 0;  

}

void lcd_wdat(uchar dat)

{                          

   while(lcd_busy());

    LCD_RS = 1;

    LCD_RW = 0;

    LCD_EN = 0;

    P0 = dat;

    delayNOP();

    LCD_EN = 1;

    delayNOP();

    LCD_EN = 0; 

}

void lcd_init()

    delay(30);                   

    lcd_wcmd(0x38); //16*2??,5*7??,8???

    delay(5);

    lcd_wcmd(0x38);         

    delay(5);

    lcd_wcmd(0x38);         

    delay(5);

    lcd_wcmd(0x0c); //???,???

    delay(5);

    lcd_wcmd(0x06); //????

    delay(5);

    lcd_wcmd(0x01); //??LCD?????

    delay(5);

}

void lcd_pos(uchar pos)

{                          

  lcd_wcmd(pos | 0x80); //????=80+????

}

void LCD_init_DIS()

{            

     delay(10); //??

     lcd_init(); //???LCD             

        

     lcd_pos(0); //????????????1???

     m = 0;

     while(cdis1[m] != '\0')

      { //????

        lcd_wdat(cdis1[m]);

        m++;

      }

     lcd_pos(0x40); //???????????1???

     m = 0;

     while(cdis2[m] != '\0')

      {

        lcd_wdat(cdis2[m]); //????

        m++;

      }

      delay(3000); //??        

        

      lcd_pos(0); //????????????1???

      m = 0;

      while(cdis3[m] != '\0')

        { //????

          lcd_wdat(cdis3[m]);

          m++;

        }

      lcd_pos(0x40); //???????????1???

      m = 0;

     while(cdis4[m] != '\0')

        {

          lcd_wdat(cdis4[m]); //????

          m++;

        }      

        for(m=0;m<2;m++)

          { 

       lcd_pos(0x0c+m); //??????

             lcd_wdat(0x3e);

    }

}

void data_conv() 

{

     data_temP0=data_temp/10; //??

  if(data_temP0==0)

  {data_temP0=0x20;} //???0???

     else 

  {data_temP0=data_temP0+0x30;}

   data_temp2=data_temp; //??

     data_temp2=data_temp2+0x30;

}

void data_dis()

{

    data_temp = snum; //????

    data_conv();

       lcd_pos(0x44); 

       lcd_wdat(data_temP0);

       lcd_pos(0x45); 

       lcd_wdat(data_temp2);

    

    data_temp = rate; //????

    data_conv();

       lcd_pos(0x4d); 

       lcd_wdat(data_temP0);

       lcd_pos(0x4e); 

       lcd_wdat(data_temp2);

}

void motor_DR()

  {

       if(direction==1) //??????

        { for(m=0;m<2;m++)

          { 

      lcd_pos(0x0c+m); //??????

            lcd_wdat(0x3e);

     }

   }

        else

         { for(m=0;m<2;m++) //??????

           { 

          lcd_pos(0x0c+m); //?????? 

             lcd_wdat(0x3c);

     }

   }

  }

void motor_RUN()

{

      if(on_off==1)

    { TR0=1; 

      lcd_pos(0); //????????????1???

         m = 0;

         while(cdis5[m] != '\0')

          { lcd_wdat(cdis5[m]); //RUNNING

            m++; }

            motor_DR(); //

    }  

      else  

    { TR0 = 0; P0 = 0x0f; 

   lcd_pos(0); //????????????1???

         m = 0;

         while(cdis3[m] != '\0')

          { lcd_wdat(cdis3[m]); //STOP

            m++; }

            motor_DR(); //

   snum=snum1; //

   number1=0; //??????

         }

  }

main()

{                

         LCD_init_DIS();

  

   TMOD = 0x01; //T0????1

   TL0 = 0x33;

   TH0 = 0xf5;

   EA = 1;

   ET0 = 1; 

   

   P1=0;

   while(1)

    {  

       if(K1==0)

    {

         //beep();

      while(K1==0); //?????

      on_off=~on_off;       

         motor_RUN();  

        } //K1 end

      if(K2==0)    

       {

        if(snum_dr==1)

    { snum++;

        snum1=snum;

      if(snum==0x14)

            { snum_dr = ~snum_dr;}

    }

   else  

     {snum--;

   snum1=snum;

      if(snum==0x01)

   { snum_dr=~snum_dr; }

     }

   

    } //K2 end

        if(K3==0)    

      {

   direction=~direction; 

         motor_DR();

      }//K3 end

        if(K4==0)    

       {

   if(rate_dr==1)

    { rate++;

      if(rate==0x10)

            { rate_dr=~rate_dr;}

    }

    else  

     { 

      rate--;

      if(rate==0x01)

   { rate_dr=~rate_dr; }

     }

  } //K4 end

    

      if(number1==snum1) //?????????  

     { number1 = 0; 

         on_off=0;

         TR0=0;

        snum=snum1;

   P1=0x0f;

    motor_RUN();

    }         

         data_dis();

} // while(1) end

} //main end

void motor_onoff() interrupt 1  

{     

       TL0 = 0x33;

       TH0 = 0xf5; 

       q++;

    if(q < rate)

        { return; }

    else 

       { q=0;    

       number++; //????

        

      if(number==64) //64?????????

       { snum--;

      number=0;

            number1++; } //??????

    

         if(direction==1) //????

       { if(v<8)  

         {P1 = FFW[v];v++;} //???,??

        if(v==8) 

        { v=0; } 

          }

   

      else

       { if(v<8)  

         {P1 = REV[v];v++;} //???,??

         if(v==8) 

         { v=0; } 

          }

    }

}


Reference address:51 single chip microcomputer controls the forward and reverse rotation of the stepper motor

Previous article:51 single chip temperature alarm DS18B20 system program
Next article:51 MCU ESP8266WIFI module LED light wireless control

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号