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

📄 1.txt

📁 pic得电机控制程序
💻 TXT
📖 第 1 页 / 共 3 页
字号:
end function
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- tests in full step mode if forced windings 1/2 are ok for motor=2
-- TRUE if no forced windings or forced windings are wrong
-- -----------------------------------------------------------------------------
function _stepper2_test_forced_windings return bit is
var bit index_lsb at _stepper2_index : 0
  return
    ((! stepper2_full_force_1_winding) & (! stepper2_full_force_2_winding)) |
    (stepper2_full_force_1_winding & index_lsb)|
    (stepper2_full_force_2_winding & (! index_lsb))
end function
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- steps motor=1 a half- or full-step forward
-- if motor is not in continuous mode and target position is reached,
-- no motor movement will be made.
-- in fullstep mode, also the settings of force full windings is checked
-- -----------------------------------------------------------------------------
procedure _stepper1_step_forward is
  ;increment indices
  _stepper1_index = _stepper1_index + 1
  _stepper1_inc_pos

  ;on full steps, take another half step
  if stepper1_full_steps &
     ( (! _stepper1_cmp_pos) |
       (stepper1_status == _stepper_status_continuous)
     ) then
     ;only step if no forced windings or forced windings are wrong
     if _stepper1_test_forced_windings then
      _stepper1_index = _stepper1_index + 1
      _stepper1_inc_pos
     end if
  end if

  ;correct table overflow
  if _stepper1_index >= 8 then
    _stepper1_index = _stepper1_index - 8
  end if
end procedure
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- steps motor=2 a half- or full-step forward
-- if motor is not in continuous mode and target position is reached,
-- no motor movement will be made.
-- in fullstep mode, also the settings of force full windings is checked
-- -----------------------------------------------------------------------------
procedure _stepper2_step_forward is
  ;increment indices
  _stepper2_index = _stepper2_index + 1
  _stepper2_inc_pos

  ;on full steps, take another half step
  if stepper2_full_steps &
     ( (! _stepper2_cmp_pos) |
       (stepper2_status == _stepper_status_continuous)
     ) then
     ;only step if no forced windings or forced windings are wrong
     ;only step if no forced windings or forced windings are wrong
     if _stepper2_test_forced_windings then
      _stepper2_index = _stepper2_index + 1
      _stepper2_inc_pos
     end if
  end if

  ;correct table overflow
  if _stepper2_index >= 8 then
    _stepper2_index = _stepper2_index - 8
  end if
end procedure
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- steps motor=1 a half- or full-step backward
-- if motor is not in continuous mode and target position is reached,
-- no motor movement will be made.
-- in fullstep mode, also the settings of force full windings is checked
-- -----------------------------------------------------------------------------
procedure _stepper1_step_backward is
var bit _stepper1_index_msb at _stepper1_index : 7

  ;decrement indices
  _stepper1_index = _stepper1_index - 1
  _stepper1_dec_pos

  ;on full steps, take another half step
  if stepper1_full_steps &
     ( (! _stepper1_cmp_pos) |
       (stepper1_status == _stepper_status_continuous)
     ) then
     ;only step if no forced windings or forced windings are wrong
     if _stepper1_test_forced_windings then
      _stepper1_index = _stepper1_index - 1
      _stepper1_dec_pos
     end if
  end if

  ;correct table overflow
  if _stepper1_index_msb then
    _stepper1_index = _stepper1_index + 8
  end if
end procedure
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- steps motor=2 a half- or full-step backward
-- if motor is not in continuous mode and target position is reached,
-- no motor movement will be made.
-- in fullstep mode, also the settings of force full windings is checked
-- -----------------------------------------------------------------------------
procedure _stepper2_step_backward is
var bit _stepper2_index_msb at _stepper2_index : 7

  ;decrement indices
  _stepper2_index = _stepper2_index - 1
  _stepper2_dec_pos

  ;on full steps, take another half step
  if stepper2_full_steps &
     ( (! _stepper2_cmp_pos) |
       (stepper2_status == _stepper_status_continuous)
     ) then
     ;only step if no forced windings or forced windings are wrong
     if _stepper2_test_forced_windings then
      _stepper2_index = _stepper2_index - 1
      _stepper2_dec_pos
     end if
  end if

  ;correct table overflow
  if _stepper2_index_msb then
    _stepper2_index = _stepper2_index + 8
  end if
end procedure
-- -----------------------------------------------------------------------------



-- -----------------------------------------------------------------------------
-- internal routine
-- this is the procedure that should be called by the interrupt routine
-- If you're using JALcc, it's most convenient done by a macro statement
-- -----------------------------------------------------------------------------
procedure _stepper_TMR0_interrupt is
;;;;;<;mac> interrupt_sub _stepper_TMR0_interrupt T0IF 5
;;;;;<;/mac>
var byte value
var bit _stepper1_acc_index_msb at _stepper1_acc_index : 7
var bit _stepper2_acc_index_msb at _stepper2_acc_index : 7

  -- must be (one of) the first statement for accurate timing
  TMR0 = _tmr0_preset  -- preset counter again

  ;if motor is running
  if stepper1_status != _stepper_status_stopped then
    ;only decrement period counter when it's not already zero,
    ;this could happen if a motor has reached his target position,
    ;while still in running mode
    ;if this limiting is not done, it could take quiet a while to restart
    if _stepper1_period != 0 then _stepper1_period = _stepper1_period - 1 end if

    ;if motor puls finished
    if _stepper1_period == 0 then

      ;as long as position not reached or running continuous
      if (! _stepper1_cmp_pos) |
         (stepper1_status == _stepper_status_continuous) then

        if stepper1_accelerate_on then
          _stepper1_acc_count = _stepper1_acc_count - 1
          if _stepper1_acc_count == 0 then
            if _stepper1_not_decelerating then
              _stepper1_acc_index = _stepper1_acc_index + 1
              if _stepper1_acc_index > _stepper_max_speed_index then
                _stepper1_acc_index = _stepper_max_speed_index
              end if
            else
              _stepper1_acc_index = _stepper1_acc_index - 1
              if _stepper1_acc_index_msb then
                _stepper1_acc_index = 0
                stepper1_status = _stepper_status_stopped
              end if
            end if
            stepper_acc1 (_stepper1_acc_index,_stepper1_period_preset)
            stepper_acc1N (_stepper1_acc_index,_stepper1_acc_count)
          end if
        end if

        ;reload the counter for this loop
        _stepper1_period = _stepper1_period_preset

        ;now get value for next step and send to port
        if stepper1_forward then _stepper1_step_forward
        else _stepper1_step_backward
        end if

        ;get table value, output to port
        _get_stepper_value ( _stepper1_index, value )
        _stepper1_port = value
      end if
    end if
  end if

  if stepper_Nmotors == 2 then  -- compiler directive !!
  ;if motor is running
  if stepper2_status != _stepper_status_stopped then
    if _stepper2_period != 0 then _stepper2_period = _stepper2_period - 1 end if

    ;if motor puls finished
    if _stepper2_period == 0 then

      ;as long as position not reached or running continuous
      if (! _stepper2_cmp_pos) |
         (stepper2_status == _stepper_status_continuous) then

         if stepper2_accelerate_on then
           _stepper2_acc_count = _stepper2_acc_count - 1
           if _stepper2_acc_count == 0 then
             if _stepper2_not_decelerating then
               _stepper2_acc_index = _stepper2_acc_index + 1
               if _stepper2_acc_index > _stepper_max_speed_index then
                 _stepper2_acc_index = _stepper_max_speed_index
               end if
             else
               _stepper2_acc_index = _stepper2_acc_index - 1
               if _stepper2_acc_index_msb then
                 _stepper2_acc_index = 0
                 stepper2_status = _stepper_status_stopped
               end if
             end if
             stepper_acc1 (_stepper2_acc_index,_stepper2_period_preset)
             stepper_acc1N (_stepper2_acc_index,_stepper2_acc_count)
           end if
         end if

        ;reload the counter for this loop
        _stepper2_period = _stepper2_period_preset

        ;now get value for next step and send to port
        if stepper2_forward then _stepper2_step_forward
        else _stepper2_step_backward
        end if

        ;get table value, output to port
        _get_stepper_value ( _stepper2_index, value )
        _stepper2_port = value
      end if
    end if
  end if
  end if

  -- must be the last statement, to prevent reentrance
  T0IF = false        -- clear TMR0 interrupt flag
end procedure
-- -----------------------------------------------------------------------------



-- ---------------------------------------------------------------------
-- sets motor to run continuously in the current speed and direction,
-- without any acceleration
-- ---------------------------------------------------------------------
procedure stepper_run_continuous (byte in motor_nr = 1) is
  if motor_nr == 1 then
    stepper1_accelerate_on = false
    stepper1_status = _stepper_status_continuous
  else
    stepper2_accelerate_on = false
    stepper2_status = _stepper_status_continuous
  end if
end procedure
-- ---------------------------------------------------------------------



-- ---------------------------------------------------------------------
-- sets motor to run continuously, with smooth start in the current direction
-- motor is supposed to be initial not running
-- ---------------------------------------------------------------------
procedure stepper_run_continuous_smooth (byte in motor_nr = 1) is
  if motor_nr == 1 then
    stepper1_accelerate_on = true
    _stepper1_not_decelerating = true
    _stepper1_acc_index = 0
    stepper_acc1 (_stepper1_acc_index,_stepper1_period_preset)
    stepper_acc1N (_stepper1_acc_index,_stepper1_acc_count)
    stepper1_status = _stepper_status_continuous
  else
    stepper2_accelerate_on = true
    _stepper2_not_decelerating = true
    _stepper2_acc_index = 0

⌨️ 快捷键说明

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