accurate square wave generator

gtslabs

Member
40X1 but plan on using 28x2 once it arrives

I want to send a square wave to a Steper motor driver and I need an accurate result.
I am using the following code:

Code:
runmotor:
pauseus  total  ' total is 16 bit value
toggle 7          'output pin for square wave
'Check input states for limit switch trigger to stop motor.
goto runmotor
I am sure there is a better way to skin this cat and I am looking for ideas.
I used a usb oscilloscope and measured the frequency generated vs what I want and the error is bad at the higher frequencies.
The attachment show the errors and the range I need acurate results (<1% error if possible)
 

Attachments

srnet

Senior Member
Not sure what the table actually represents, cant see how you can issue a 'pauseus 16.7' ?

At low values of pause, its not that the error is 'bad' as its inevitable due to the time taken for the toggle and goto commands.

Look up the HPWM command in the manual for another way of generating square waves.
 

hippy

Technical Support
Staff member
As srnet notes the loop time is the pause time plus execution time of the other instructions so you won't get a linear response to a change of 'total' value: f = 1 / ( total + K ).

What you need to do is adjust 'total' to get the frequency you want. Operating at higher than default speed using SETFREQ will reduce the 'K' overhead.
 
Top