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.




Senior Member

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.
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.
[color=Green]'ymbol Version = 8 'v0-8  11-Apr-2017  251 bytes Revise for second RS 5v unipolar motor 7.5deg, 50:1 reduction gearbox[/color]
[color=Blue]Symbol Version [/color][color=DarkCyan]= [/color][color=Navy]9 [/color][color=Green]'v0-9  29-May-2018  190 bytes Cut down code for demonstration
[color=Blue]Symbol Major [/color][color=DarkCyan]= [/color][color=Navy]0   [/color][color=Green]'Major revision
[color=Navy]#Picaxe [/color][color=Black]20x2[/color]
[color=Green]' [/color]
[color=Navy]#Terminal 19200   [/color][color=Green]'Open PE Terminal immediately (9600 baud is default for 20x2)
'#No_Data         'Don't overwrite EEPROM[/color]
[color=Navy]#No_Table         [/color][color=Green]'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 
   [/color][color=Blue]Symbol PortC_Dirs    [/color][color=DarkCyan]= [/color][color=Navy]%00011110 [/color][color=Green]'4-wire stepper motor drive pins C.1 to C.4 
   'ymbol hSerialTx     = C.0    'Leg 10  
   [/color][color=Blue]Symbol oMotor0       [/color][color=DarkCyan]= [/color][color=Blue]C.1    [/color][color=Green]'Leg 9  Yellow wire  
   [/color][color=Blue]Symbol oMotor1       [/color][color=DarkCyan]= [/color][color=Blue]C.2    [/color][color=Green]'Leg 8  Orange wire
   [/color][color=Blue]Symbol oMotor2       [/color][color=DarkCyan]= [/color][color=Blue]C.3    [/color][color=Green]'Leg 7  Brown wire
   [/color][color=Blue]Symbol oMotor3       [/color][color=DarkCyan]= [/color][color=Blue]C.4    [/color][color=Green]'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 ------------------------------------------------------------
   [/color][color=Blue]Symbol [/color][color=Purple]bStep            [/color][color=DarkCyan]= [/color][color=Purple]b4        [/color][color=Green]'w2 Step counter 0-7
'----------- Constants ---------------------------------------------------------------------
   [/color][color=Blue]Symbol mskZeroToSeven   [/color][color=DarkCyan]= [/color][color=Navy]%00000111
' ===== P r o g r a m m e ==================================================================
[color=Black]Init:    [/color][color=Blue]SetFreq M16
         Pause [/color][color=Navy]2000
         [/color][color=Blue]SerTxd ([/color][color=Red]" Stepper Motor Test Vers "[/color][color=Black], #[/color][color=Blue]Major[/color][color=Black], [/color][color=Red]"-"[/color][color=Black], #[/color][color=Blue]Version[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF[/color][color=Black], [/color][color=Red]"Direction = "[/color][color=Blue])
         [/color][color=Purple]DirsC [/color][color=DarkCyan]= [/color][color=Blue]PortC_Dirs[/color]
' ===== M a i n   L o o p ==================================================================
            [/color][color=Green]'      PICAXE Leg:  6789   
            'Reverse direction C4321
            ' 4 pins are        |||| indicated
            ' Motor wire colour knOY k=Black; n=Brown; O=Orange; Y=Yellow       
            [/color][color=Blue]Select Case [/color][color=Purple]bStep  [/color][color=Green]'vvvv
            [/color][color=Blue]Case [/color][color=Navy]0[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00000100    [/color][color=Green]'Pattern 0 C.2 only
            [/color][color=Blue]Case [/color][color=Navy]1[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00001100    [/color][color=Green]'Pattern 1 C.3 & C.2
            [/color][color=Blue]Case [/color][color=Navy]2[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00001000    [/color][color=Green]'Pattern 2 C.3 only
            [/color][color=Blue]Case [/color][color=Navy]3[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00001010    [/color][color=Green]'Pattern 3 C.3 & C.1
            [/color][color=Blue]Case [/color][color=Navy]4[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00000010    [/color][color=Green]'Pattern 4 C.1 only
            [/color][color=Blue]Case [/color][color=Navy]5[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00010010    [/color][color=Green]'Pattern 5 C.1 & C.4
            [/color][color=Blue]Case [/color][color=Navy]6[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00010000    [/color][color=Green]'Pattern 6 C.4 only
            [/color][color=Blue]Case [/color][color=Navy]7[/color][color=Black]: [/color][color=Purple]PinsC [/color][color=DarkCyan]= [/color][color=Navy]%00010100    [/color][color=Green]'Pattern 7 C.2 & C.4
            [/color][color=Blue]End Select
            [/color][color=Purple]bStep [/color][color=DarkCyan]= [/color][color=Purple]bStep [/color][color=DarkCyan]+ [/color][color=Navy]1 [/color][color=DarkCyan]And [/color][color=Blue]mskZeroToSeven  [/color][color=Green]'Limit to 8 patterns (0 - 7)


Senior Member
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.