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

📄 stepper.c

📁 Motor & key & LED
💻 C
📖 第 1 页 / 共 2 页
字号:
                     case STP_ERROR:
                     default:
                             break;
                 }

                 SendCurPhaseTable();
            }

            ENABLE_INTS;
}

// Send moving phase
void SendCurPhaseTable(void)
{
	   Uint8 tPhase;

	   //UpdatePhaseIndex();

	   tPhase = LookUpPhaseTable[sMotor.phaseIndex];

	   //IO_WRITE8(regPIO, tPhase);
	   //IO_WRITE8(MSB_regTimer, (sMotor.curSpeed & 0xFF00) >> 8);
	   //IO_WRITE8(LSB_regTimer, (sMotor.curSpeed & 0x00FF));

	   regPIO = tPhase;
	   MSB_regTimer = (Uint8)((sMotor.curSpeed & 0xFF00) >> 8);
	   LSB_regTimer = (Uint8)(sMotor.curSpeed & 0x00FF);

	   UpdatePhaseIndex();

	   RESTART_TIMER;
}

// Get Current Phase
void UpdatePhaseIndex(void)
{
	   if(sMotor.curStatus.dir == STP_MOV_FWD)
	   {
	    	sMotor.phaseIndex += sMotor.curStatus.stepMode;
	   }
	   else
	   {
	    	sMotor.phaseIndex -= sMotor.curStatus.stepMode;
	   }

	   sMotor.phaseIndex &= 15;
}

// Switch to detect current
void Switch2DetectPhase(void)
{
     Uint8 tPhase;

	   tPhase = DetentLookUpPhaseTable[sMotor.phaseIndex];

	   //IO_WRITE8(regPIO, tPhase);
	   regPIO = tPhase;
}

// Next accel speed in the table
Uint16 AccelGetNextSpeed(void)
{
	   sMotor.accelIndex++;
	   return StpSpeedTable[sMotor.accelIndex];
}

// Next decel speed in the table
Uint16 DecelGetNextSpeed(void)
{
     sMotor.accelIndex--;
	   return StpSpeedTable[sMotor.accelIndex];
}

//////////////////////////////////////
// IsMotorRunning
// TRUE | FALSE : RUNNING | STOP
Boolean IsMotorRunning(void)
{
	   return (sMotor.curStatus.steps == 0) ?  FALSE : TRUE;
}

//////////////////////////////////////
// MotorDecelNow
// Start Decel to stop status
void MotorDecelNow(void)
{
	   sMotor.curStatus.steps = sMotor.accelSteps + 1;
	   sMotor.state = STP_DECEL;
}

//////////////////////////////////////
// Motor Move Start
void MotorMoveStart(void)
{
	   // Sample move action
	   StepMoveStart(TestMotorMove);
}

// Never stop
void SetNeverStopFlag(void)
{
	   //sMotor.curStatus.steps = 0xFFFF;
	   sMotor.flag |= MOTOR_NEVER_STOP; 
}

void ClrNeverStopFlag(void)
{
	   sMotor.flag &= ~MOTOR_NEVER_STOP; 
}

//////////////////////////////////////
//   Accel speed during running
//
void AddSelectNextIndexSpeed(void)
{
	   if(sMotor.accelIndex < MAX_TABLE_VALUE && (sMotor.state == STP_RUNNING))
	   {
	       sMotor.accelIndex++;
	       sMotor.accelSteps++;
	       sMotor.curStatus.vFinal = StpSpeedTable[sMotor.accelIndex];
	       sMotor.curSpeed = StpSpeedTable[sMotor.accelIndex];
	   }
}

//////////////////////////////////////
//  Dec speed during running
//
void DecSelectNextIndexSpeed(void)
{
	   if((sMotor.accelIndex > 0) && (sMotor.state == STP_RUNNING))
	   {
	       sMotor.accelIndex--;
	       sMotor.accelSteps--;
	       sMotor.curStatus.vFinal = StpSpeedTable[sMotor.accelIndex];
	       sMotor.curSpeed = sMotor.curStatus.vFinal;
	   }
}

//////////////////////////////////////
//  Get current steps and display it
//
void DisplayRemainSteps(void)
{
	   eUser.curMenu = REMAIN_STEPS_MENU;
}

//////////////////////////////////////
//  Get current speed and display it
//
void DisplayCurrentSpeed(void)
{
	   eUser.curMenu = CURRENT_SPEED_MENU;
}

//  refresh LED

void LED_Flush(void)
{
	  if(eUser.curMenu == CURRENT_SPEED_MENU)
	  {
       //eUser.ledValue = sMotor.curSpeed;    //当前速度
       eUser.ledValue = 100 + sMotor.accelIndex * 20;
    }
    else if(eUser.curMenu == REMAIN_STEPS_MENU)
    {
    	 eUser.ledValue = sMotor.curStatus.steps;	 //当前步数
    }
    else
    {
    	 //eUser.ledValue = sMotor.curSpeed;
    	 //
    	 //eUser.ledValue = sMotor.curStatus.steps;	 //当前步数
    	 eUser.ledValue = 100 + sMotor.accelIndex * 20;
    }
    
    LedDecParameter();

  	//LED_Display(eUser.ledValue);
}

/////////////////ISR
void LED_INT() interrupt 3 using 1
{
   endINT;
   endTim;

   LED_Display();

   TH1=0xf4;
   TL1=0x48;

   startINT;
   startTim;
}

//
void UpdateLedParameter(void)
{
	Uint8 t1,t2;

	t1 = (Uint8) (eUser.ledValue & 0x00FF);
	t2 = (Uint8) ((eUser.ledValue & 0xFF00) >> 8);

  a_val = (Uint8)(t1 & 0x0F);
	b_val = (Uint8)(t1 >> 4);
	c_val = (Uint8)(t2 & 0x0F);
	d_val = (Uint8)(t2 >> 4);
}

void LedDecParameter(void)
{
	char tmp[6];

	//itoa(eUser.ledValue, tmp, 10);
	myitoa(tmp, eUser.ledValue);

	a_val = tmp[0];
	b_val = tmp[1];
	c_val = tmp[2];
	d_val = tmp[3];
	e_val = tmp[4];
}

int myitoa(char *s,Uint16 n)
{
	 Uint8 i;

	 for(i = 0; i < 5; i++)
	 {
	 	   *(s + i) = n % 10;
	 	   n = n / 10;
	 }

	 return 0;
   //return *(short *)(s+(n=(n>9)?myitoa(s,n/10):0))=n%10+'0',n+1;
}

//
void LED_Display(void)
{
	//static Uint8 counter;

  //UpdateLedParameter();
  //LedDecParameter();
	counter++;

	switch(counter)
	 {
	   case 1:
	          P0 = 0x00 | a_val;
	      break;
	   case 2:
	          P0 = 0x10 | b_val;
	      break;
	   case 3:
	          P0 = 0x20 | c_val;
	      break;
	   case 4:
	          P0 = 0x30 | d_val;
	      break;
	   case 5:
	   	      P0 = 0x40 | e_val;
	   	  break;
	   default:
	      break;
	  }

	  if(counter > 5)
	  {
	     counter = 0x00;
	  }
}

void TempPause(void)
{
	Uint8 i;

	for(i = 0; i < 10; i++);
}
void DispatchKey(void)
{
	  Uint8 key, tRow, tCol;

	  tRow = 0xFF;
	  tCol = 0xFF;

    P1 = SCAN_MSB;
    //MS_DELAY(0x01);
    TempPause();
    key = P1;

    if(key != SCAN_MSB)
    {
    	    MS_DELAY(6);
			//P1 = SCAN_MSB;  	   //
    	    key = P1;

    	    // scan and get row value
    	    if(key != SCAN_MSB)
    	    {
		           key = (~key) & SCAN_MSB;

		           if(key != 0)
		           {
		               switch(key)
		               {
		               	   case 0x01:
		               	   	     tRow = 0x03;
		               	   	     break;
		               	   case 0x02:
		               	   	     tRow = 0x02;
		               	   	     break;
		               	   case 0x04:
		               	             tRow = 0x01;
		               	   	     break;
		               	   case 0x08:
		               	   	     tRow = 0x00;
		               	   	     break;
		               	   default:
		               	   	     break;
		               }
		           }
		      }

          P1 = SCAN_LSB;
          TempPause();
          //MS_DELAY(0x01);
          key = P1;

          // If get row value send col value and scan col value.
          if(key != SCAN_LSB)
          {
		                 key = (~key) & SCAN_LSB;
		                 key = key >> 4;

		                 if(key != 0)
		                 {
		                     switch(key)
		                     {
		                     	   case 0x01:
		                     	   	     tCol = 0x03;
		                     	   	     break;
		                     	   case 0x02:
		                     	   	     tCol = 0x02;
		                     	   	     break;
		                     	   case 0x04:
		                     	         tCol = 0x01;
		                     	   	     break;
		                     	   case 0x08:
		                     	   	     tCol = 0x00;
		                     	   	     break;
		                     	   default:
		                     	   	     break;
		                     }
		                 }
          }
    }


    if((tRow != 0xFF) || (tCol != 0xFF))
    {
         ProcessKey(tRow, tCol);
    }
    else
    {
    	   eUser.dbcButton = NO_BUTTON;
    	   eUser.newButton = NO_BUTTON;
    }
}
////////////////////////////////////////

Uint8 GetButtonTable(Uint8 fRow, Uint8 fCol)
{
	    return (ButtonTable[fRow][fCol]);
}

//////////////////////////////////////////////////
//
//   ProcessKey Function
//
void ProcessKey(Uint8 row, Uint8 col)
{
	   //Uint8 x;
	   
	   //x = IsMotorRunning();
     eUser.newButton = GetButtonTable(row, col);

     // If not release the button, we should not view it as key pressed.
     if(eUser.newButton != eUser.dbcButton)
     {
           eUser.dbcButton = eUser.newButton;
           //eUser.ledValue =  eUser.newButton;
           //eUser.ledValue = eUser.preButton;
     }
     else
     {
           eUser.dbcButton = NO_BUTTON;
     }

     eUser.newButton = eUser.dbcButton;

     switch(eUser.newButton)
	   {
		      case START_STOP:
		           if (IsMotorRunning() && (sMotor.state == STP_RUNNING))
		           {
		     	        MotorDecelNow();
		           }
		           else //if (IsMotorRunning() == FALSE)
		           {
		             	MotorMoveStart();
		           }
		           break;

		      case ACCEL_SPEED:
		           AddSelectNextIndexSpeed();
		           DisplayCurrentSpeed();
		           break;

		      case DECEL_SPEED:
		           DecSelectNextIndexSpeed();
		           DisplayCurrentSpeed();
		           break;

		      case STEP_MENU:
		           DisplayRemainSteps();
		           break;

	      	case SPEED_MENU:
		           DisplayCurrentSpeed();
		           break;
		      case NEVER_STOP:
		      	   if(sMotor.flag == 0)
		      	   {
		      	       SetNeverStopFlag();
		      	   }else
		      	   {
		      	   	   ClrNeverStopFlag();
		      	   }
		      	   break;
		      case Undefine:
		           break;

		      default:
		           break;
	   }
}

⌨️ 快捷键说明

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