⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 web.c

📁 这是一个简单的步进电机的程序,用C51编写,主要可以定时控制电机进行180度的旋转
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <mc9s12a64.h>     /* derivative information */


#pragma LINK_INFO DERIVATIVE "mc9s12a64"

byte np,i,control,Rti8Flg,Rti8timer,step,stepi,stepin;

const byte motorb[]={0x05,0x06,0x0a,0x09};


//初始化
void CRGINI(void)
{     
    REFDV=1;       /*REF FREQUENCY IS 4/(REFDV+1) MHZ*/
    SYNR=0;       /*SYSCLK FREQUENCY IS (SYNR+1)*REF MHZ, 16MHz*/
    //PLLPC=0x67;    /*PLL LOCK INT ENABLE,PLL ON,AUTO MODE,limp mode enable,pseudo-stop enable*/
    PLLCTL=0xE4;  /*clock monitor, PLL is turned on, auto mode*/
    CRGINT=0x80;   /*ENABLE RTI INT*/ 
    CLKSEL=0x40;   /*system clock come from oscclock bus=osclock/2, all clock go on running in wait mode*/
    RTICTL=0x41;   /*PRESCALE(1000001=2*2**13 1.024MS FREQUENCY SENSITIVE)*/
    COPCTL=0;    /*disable COP*/
}

//单步
void a_step(byte dir)    //dir 0 is in 1 is out ;
 {
 
  
   if(dir&0x01)
   {
    
   if(np==0) {
    np=3;
   }
   else {
    np--;
   }  
   }
   
   else
   {
   
   if(np==3) {
    np=0;
   }
   else {
    np++;
   }
   }
   
   PORTB =motorb[np];
   
   }   

void  n_stepin(byte dir,byte stepin)
{
 
 if(control==1&&Rti8Flg==1) {    
 
  if(stepi<stepin)
  {
  Rti8Flg=0;
  stepi++;  
  a_step(dir);      //0 IN
  }
  else
  {
    Rti8Flg=0;
    stepi=0;
    control=0;
  }
  } 
}

void n_stepout(byte dir,byte step)
{
 if(control==0&&Rti8Flg==1) {    
 
  if(stepi<step)
  {
  Rti8Flg=0;
  stepi++;  
  a_step(dir);      //out
  }
  else
  {
    Rti8Flg=0;
    stepi=0;
    control=1;
  }
  } 
}


void main(void) {

CRGINI();

  
  DDRJ=0x03;
   PTJ=0x03;   //enable L298 
   DDRB=0x0F;    //pb0-pb4 output pb0 A pb1 A- pb2 B pb3 B-
   PORTB=0x06;
   np=2;
   control=1;
   i=0;
   stepi=0;
   
  EnableInterrupts;
  for(;;) {
   
   n_stepin(0,200);
 n_stepout(1,150);

   
  
  } /* wait forever */
}


interrupt void RTIRTI(void)
{
  CRGFLG=0x80;
  
  if(Rti8timer==8)
   {
     Rti8Flg=1;           //8ms for sci
     Rti8timer=0;        
     
   }else
   {
     Rti8timer++;
   } 
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -