AT89S52 controls hybrid stepper motor C program

Publisher:rnm888Latest update time:2016-02-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include   //Include the serial communication header file (which contains reg52.h)

#include   //Include printf header file
#define uint unsigned int
#define uchar unsigned char

sbit cp  = P2^0;   //Pulse input terminal
sbit dir = P2^1;   //Direction control terminal
sbit free = P2^2;   //Offline control terminal, usually connected to high

sbit jia_su = P1^0;   //Declaration of acceleration key 1
sbit jian_su = P1^1;  //Deceleration key 2 declaration
sbit fang_xiang = P1^2;  //Declaration of direction switch key 3
sbit ting = P1^3;   //Declaration of stop key 4
uint num;
void uart_Init();   //Serial port initialization sub-function declaration

void delay(uint z)  //ms子函数

 uint x,y;
 for(x = 0;x   for(y = 0;y<120;y++);
}

void delay1(uint z)   //Motor delay sub-function us level

 uint x;
 for(x = 0;x   
}

void init()    //Initialize motor and serial port sub-function

 num = 2;
   dir = 1;
 free = 1;
 uart_Init();
}

void key_scan()   //Keyboard control subfunction
{
 if(jia_su == 0)   //Judge whether the accelerator key is pressed
 {
  delay(5);   //Delay debounceif
  (jia_su == 0)  //Judge whether the accelerator key is pressed again
  {
   num++;   //num (representing the motor speed) increasesif
   (num>4)   
   {num=4;}
   while(!jia_su);   //Release detection
     delay(5);
     while(!jia_su);
    printf("num = %dn",num);//View the num value through the serial port
  }
 }
 if(jian_su == 0)   //Judge whether the deceleration key is pressed
  {
   delay(5);   //Delay debounceif
   (jian_su == 0)  //Judge whether the deceleration key is pressed again
   {
    num--;   //num (representing the motor speed) decreasesif
    (num<1)
    {num=0;} 
    while(!jian_su);  //Release detection
     delay(5);
     while(!jian_su);
    printf("num = %dn",num);  //View the num value through the serial port
   }
  }
 if(fang_xiang ==0)   //Judge whether the direction switch key is pressed
 
  delay(5);    //Delay debounceif
  (fang_xiang == 0)
  {
   dir =~dir;   //Direction changewhile
   (!fang_xiang);   //Release detectiondelay
    (5);
    while(!fang_xiang);
   printf("dir = %dn",dir);
  }
 }
 if(ting ==0)   //Judge whether the stop key is pressed
 
  delay(5);    //Delay debounceif
  (ting == 0)
  {
   num = 0;   //Stopwhile
   (!ting);   //Release detectiondelay
    (5);
    while(!ting);
  }
 }
 
 switch(num)
 
  case 0: cp = 1; break;   //Motor stopscase
  1:      //Rotate at low speed
  
   
   
    cp = 1;
    delay1(20);
    cp = 0;
    delay1(20); 
  
  }
  break;

  case 2:    //rotate at medium speed
  
   
   
    cp = 1;
    delay1(15);
    cp = 0;
    delay1(15); 
   
  }
  break;
  
  case 3:    //rotate at high speed
  
   
   
    cp = 1;
    delay1(10);
    cp = 0;
    delay1(10); 
   
  }
  break;

  case 4:    //Rotate at a higher speed
  
   
   
    cp = 1;
    delay1(5);
    cp = 0;
    delay1(5);
    
  }
  break;
 }
 
}
void main()

 init();   //Call initialization subfunction
 while(1)
 
  key_scan();   //Continue keyboard scanning
 
 
}

Reference address:AT89S52 controls hybrid stepper motor C program

Previous article:Multi-task and multi-process based on 51 single chip microcomputer
Next article:Program for single-chip music and colored lights written with C51

Recommended ReadingLatest update time:2024-11-16 19:54

Cumulative shipments of HAPTIC™ Reactor Hybrid Tough Type exceeded 200 million units
Alps Alpine Co., Ltd. (TOKYO: 6770; President: Toshihiro Kuriyama; Headquarters: Tokyo) (hereinafter referred to as "Alps Alpine") has achieved cumulative shipments of 200 million units of its HAPTIC™ Reactor Hybrid Tough Type haptic feedback component, which realistically reproduces a rich sense of touch through vibr
[Microcontroller]
Cumulative shipments of HAPTIC™ Reactor Hybrid Tough Type exceeded 200 million units
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号