current position:Home>Stc89c52rc controls the stepping motor, and the program card is in the first line of main,

Stc89c52rc controls the stepping motor, and the program card is in the first line of main,

2022-02-04 16:50:18 CSDN Q & A

img

img


The procedure is as follows :

#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit maicong=P2^5;// Motor pulse output , Initial low level sbit fangxiang=P2^6;// Motor direction output , Low level after initialization sbit EN=P2^7;// Motor enable , Initial high level sbit XWKG=P2^4;// Initial position confirmation port , Low level active , call ZIJIAN Subroutines sbit k0=P1^0;// start-up , call START Subroutines sbit k1=P1^1;// stop it , call TINGZHI Subroutines sbit k2=P1^2;// Speed up , call SUDUZ Subroutines sbit k3=P1^3;// Speed down , call SUDUJ Subroutines sbit k4=P1^4;// Step size plus , call BUCHANGZ Subroutines sbit k5=P1^5;// Step minus , call BUCHANGJ Subroutines uint tt=200;// If you need ??uint step=1100;// ditto uint num=18;void keyscan();// Keyboard scanner , Program completion void delayms(uint ms);// Millisecond variable delay program void delayus(uint us);// Microsecond variable delay program , Used for low-level output delay , The frequency is adjusted by changing the low level .void delay(uint us);// Microsecond delay program , Used for pulse high-level output delay void baidong();//void ZIJIAN();// Program completion void START();// Corresponding k0void TINGZHI();// Corresponding k1void SUDUZ();// Corresponding k2void SUDUJ();// Corresponding k3void BUCHANGZ();// Corresponding k4void BUCHANGJ();// Corresponding k5void JIUQIANYISHEN();// Corresponding k6void main(void){    _    maicong = 0;_        fangxiang = 1;        IT1 = 1;                        //set INT1 int type (1:Falling only 0:Low level)        EX1 = 1;                        //enable INT1 interrupt        EA = 1;                         //open global interrupt switch        ZIJIAN();               while(1);// Wait for the key command , here PWM=0;DIR=0.}/* Lower level external interrupt 2, Responsible for key operation , Start now 、 stop it 、 Acceleration and deceleration 、 Step addition and subtraction 、 Start of automatic running program */void exint1() interrupt 2           //(location at 0013H){                  keyscan();                                  }void keyscan(){ while(1) {         if(k0==0)    START();    else if(k1==0)    TINGZHI();// stop it , Return to the initial position     else if(k2==0)    SUDUZ();// Speed increase     else if(k3==0)    SUDUJ();// Speed down     else if(k4==0)    BUCHANGZ();// Step size increases     else if(k5==0)    BUCHANGJ();// Step minus     else if(k6==0)    JIUQIANYISHEN();// Nine is shallow and one is deep     else if(XWKG==0) ZIJIAN();// The initial position switch is on , Motor commutation , turn }}void ZIJIAN()  {                EN=0;// Motor enable , Can rotate freely                 fangxiang=1;        while(XWKG==0)     {    maicong=1;    delay(1);    maicong=0;        delayus(tt);        }               while(XWKG==1)                        {         fangxiang=0;        maicong=1;    delay(1);    maicong=0;        delayus(tt);// It is necessary to determine the rotation angle with the limit switch                                                             }          EN=1;// Motor enable , Cannot rotate freely         delayms(50);}        void START()  {        EN=0;                step=1100;//50*55 Maximum moving steps , It is possible to trigger the limit switch                 delayms(50);        while(1)                {                            baidong();            }}void TINGZHI(){        while(1);    }void SUDUZ(){   tt=tt-5;   if(tt<1)    tt=1;    while(k2==0);}void SUDUJ(){   tt=tt+5;   if(tt>255)    tt=255;    while(k3==0);}void BUCHANGZ(){   step=step+5;   if(step>2750)    step=2750;    while(k4==0);}void BUCHANGJ(){   step=step-5;   if(step<50)    step=50;    while(k5==0);}/* External interrupt 0( highest ), Responsible for interrupting external interrupts  2, That is, the trigger of the limit switch */void exint0() interrupt 0           //(location at 0003H){    while(1);}    void baidong()    {     EN=0;// Motor enable , Can rotate freely                while(step--)//step The value range is less than the subdivision value *55     {    maicong=1;// initial pwm The output is low level , When you start, it will become high ( Common Yin connection )    delay(1);// Time delay 4 Microsecond , The pulse width is 5 Microsecond     maicong=0;// Low level     delayus(tt);// Time delay 9 Microsecond , Low level width 10 Microsecond , Adjust this delay value to change the speed      }   fangxiang=~fangxiang;// Commutation     }void delay(uint xus)    {        uint m;        for(m=0;m<xus;++m);            }void delayus(uint tt)    {        uint n;        for(n=0;n<tt;++n);// Variable delay program ???    }void delayms(uint xms)    {        uint x,y;        for(x=0;x<xms;++x)        for(y=0;y<110;++y);    }



Take the answer :

Hello , I am a little assistant who answers every question , So sorry , This time, you have to answer all your questions , The panel of technical experts timed out and did not answer for you


The number of times to answer all questions deducted from this question , Will answer with questions VIP Experience card (1 A chance to answer every question 、 Shopping malls buy physical books and enjoy 95 A discount ) It will be reissued to your account in the form of .


Because all questions must be answered VIP The experience card is only valid for 1 God , When you need it 【 Direct messages 】 Contact me , I'll reissue it for you .



Other answers 2:
Hello! , I am a little assistant who answers every question , Your question has been answered by a small partner , Thank you for your support and attention !
PS: Question and answer VIP Annual pass 【 Limited time bonus :IT Technical books are free 】, Learn more >>> https://vip.csdn.net/askvip?utm_source=1146287632

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041650160275.html

Random recommended