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

📄 step motor pic.c

📁 自己编写的步进电机的控制源码
💻 C
字号:
#include <htc.h>

unsigned int cw_n, ccw_n, Flag_T0, Flag_LED;
#define TMRB0 0x05;
unsigned char step[4] =
{
  0x0A, 0x06, 0x45, 0x09
};
#define n 2000
int i, j;

void interrupt clkint(void) //定时器中断函数
{
  TMR0 = TMRB0;
  T0IF = 0;
  Flag_T0 = 1;
 if ( i++ > 499 )
  {
    i = 0;
    Flag_LED = 1;
  }

}

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


void intmain()

{

  TRISB = 0x01;
  TRISA = 0x00;
  PORTB=0;
  INTCON = 0xE0;
  OPTION = 0x02;
  TMR0 = TMRB0;
	cw_n=n;
}

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


void cw()
{

  cw_n--; /* decrement cw step number */
  
    j++;
  /* if not zero increment index j */
}

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



void energize()
{
  PORTA = step[(j &0x03)]; /* only step 0-7 needed */
}

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



void main()
{

  intmain();
  
while (1)
  {
    if (Flag_LED)
    {
      Flag_LED = 0;
      RB1 =!RB1;
    }




    if (!RB0)
    {
      while (cw_n)
      {

        //tick_wait(); /* wait for 10ms elapsed */
        if (Flag_T0)
        {
          energize(); /* round-robin execution the following tasks every 10ms */
          cw();
			

          Flag_T0 = 0;

        }
      }
    }
cw_n=n;
PORTA=0x00;
  }
}

⌨️ 快捷键说明

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