​ ​ ​ ​ Stepping motor pulsout question
Results 1 to 4 of 4

Thread: Stepping motor pulsout question

  1. #1
    New Member
    Join Date
    Mar 2015
    South Africa

    Default Stepping motor pulsout question


    Over the years there have been posts on the driving of stepping motors. Having read some of them, I would like to pose a question on how fast an 18M2 can drive an output pin to a stepping motor drive. I have written a very simple do loop code and measured on the scope the time elapsed between pulses. The code is:

    high PinC.1
    low PinC.1

    For a 18M2 with the setfreq m32, I get a pulse length of 350 us (microseconds) and a high time of 100 us. If I change the code to

    pulsout PinC.1,10
    pulseus 10

    For the same chip and frequency, I get a pulse length of 500 us with 40 us for the high time.

    I must be missing something quite important. I don't understand how the max output frequency is less than 3kHz. Now I do understand about the overheads in the code generated by the compiler. Yet given that the clock frequency is stated at m32, I thought the output frequency at the pin would be somewhat higher.

    It would be nice if someone would take a few minutes to confirm my numbers (assuming the scope, chip, etc is available). Or explain the reason why I cannot get a higher output frequency for the 18M2. The stepping motor drive spec sheet claims that it can accept pulses up to 200 kHz. I would like to prove that this drive manufacturer is correct. If I cannot get a higher speed, then I would need to look at external hardware or alternatives to validate the claim.



  2. #2
    Senior Member
    Join Date
    Feb 2012



    PICaxe doesn't (just) use a compiler but a fixed interpreter programmed within the chip. It's not even optimised for "speed" but for minimum code size (and a degree of encryption to protect the interpreter code).

    The PICaxe instruction cycle times are not specified (and not entirely constant between chip types) but I always estimate them in terms of "base PIC Instruction Cycles" (ICs) which execute in 1us with a 4 MHz clock (i.e. 125 ns @ 32 MHz). HIGH and LOW execute in just under 400 ICs (i.e. 50 us @ 32 MHz) and GOTO (the "compiled" version of an unconditional DO .. LOOP) is just under 1000 ICs. PULSOUT increments in 10 ICs steps (per specified integer variable) but the overhead (e.g. with a zero parameter) is around 500 ICs.

    So yes, the fastest simple loop you can create with an M2 is around 5 kHz, but an optimised loop of sequential stepper motor coil pulses might be a little faster (e.g. by directly XORing the output pin states).

    Cheers, Alan.

  3. #3
    Senior Member
    Join Date
    Jan 1970
    Perth, Western Australia


    You don't mention what type of stepper motor you are using. I don't think using pulsout is a very efficient way of driving a stepper motor with a PICAXE.

    I used the following basic code to test a unipolar motor. This was on a 20X2 but could easily be altered to run on an 18M2. Obviously, it must be wired to just one port.
    'ymbol Version = 8 'v0-8  11-Apr-2017  251 bytes Revise for second RS 5v unipolar motor 7.5deg, 50:1 reduction gearbox
    Symbol Version = 9 'v0-9  29-May-2018  190 bytes Cut down code for demonstration
    Symbol Major = 0   'Major revision
    #Picaxe 20x2
    #Terminal 19200   'Open PE Terminal immediately (9600 baud is default for 20x2)
    '#No_Data         'Don't overwrite EEPROM
    #No_Table         'Don't download Table (Flash) or Overwrite EEPROM
    '---------- I/O Pins --- PICAXE 20X2 -------------------------------------------------------------
    '                                 Leg 20 Supply ground  ( 0v)
    '  Port A: Shared with download circuit 
    '                       = A.0    'Leg 19 Port A  (Serial Out for programming) 
    '  Port B: I/O not used
    '  Port C: I/O 
       Symbol PortC_Dirs    = %00011110 '4-wire stepper motor drive pins C.1 to C.4 
       'ymbol hSerialTx     = C.0    'Leg 10  
       Symbol oMotor0       = C.1    'Leg 9  Yellow wire  
       Symbol oMotor1       = C.2    'Leg 8  Orange wire
       Symbol oMotor2       = C.3    'Leg 7  Brown wire
       Symbol oMotor3       = C.4    'Leg 6  Black wire
       'ymbol outLED        = C.5    'Leg 5  Out Green LED
       'ymbol iModeSwitch   = pinC.6 'Leg 4  Input only.  Pushbutton switch (pulled up to +5v)
       'ymbol unused        = C.7    'Leg 3
       '                              Leg 2  Serial In (Programming)
       '                              Leg 1  Positive suppy (+5v)
    '--------- Variable Definitions ------------------------------------------------------------
       Symbol bStep            = b4        'w2 Step counter 0-7
    '----------- Constants ---------------------------------------------------------------------
       Symbol mskZeroToSeven   = %00000111
    ' ===== P r o g r a m m e ==================================================================
    Init:    SetFreq M16
             Pause 2000
             SerTxd (" Stepper Motor Test Vers ", #Major, "-", #Version, CR, LF, "Direction = ")
             DirsC = PortC_Dirs
    ' ===== M a i n   L o o p ==================================================================
                '      PICAXE Leg:  6789   
                'Reverse direction C4321
                ' 4 pins are        |||| indicated
                ' Motor wire colour knOY k=Black; n=Brown; O=Orange; Y=Yellow       
                Select Case bStep  'vvvv
                Case 0: PinsC = %00000100    'Pattern 0 C.2 only
                Case 1: PinsC = %00001100    'Pattern 1 C.3 & C.2
                Case 2: PinsC = %00001000    'Pattern 2 C.3 only
                Case 3: PinsC = %00001010    'Pattern 3 C.3 & C.1
                Case 4: PinsC = %00000010    'Pattern 4 C.1 only
                Case 5: PinsC = %00010010    'Pattern 5 C.1 & C.4
                Case 6: PinsC = %00010000    'Pattern 6 C.4 only
                Case 7: PinsC = %00010100    'Pattern 7 C.2 & C.4
                End Select
                bStep = bStep + 1 And mskZeroToSeven  'Limit to 8 patterns (0 - 7)

  4. #4
    Senior Member
    Join Date
    Feb 2013


    If you just want a string of pulses to test the specification of the stepper motor without actually counting the pulses then you could you use PWMOUT. This should have no problem in producing square wave pulses at the frequency that you are looking for. You can also adjust the pulse width to the specification required by your stepper motor driver circuit.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts