<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://www.dynomotion.com/wiki/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://www.dynomotion.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Driving_Hobby_Servos</id>
		<title>Driving Hobby Servos - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://www.dynomotion.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Driving_Hobby_Servos"/>
		<link rel="alternate" type="text/html" href="https://www.dynomotion.com/wiki/index.php?title=Driving_Hobby_Servos&amp;action=history"/>
		<updated>2026-05-11T13:46:11Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.10</generator>

	<entry>
		<id>https://www.dynomotion.com/wiki/index.php?title=Driving_Hobby_Servos&amp;diff=291&amp;oldid=prev</id>
		<title>TK: Added Hobby RC Servo modules</title>
		<link rel="alternate" type="text/html" href="https://www.dynomotion.com/wiki/index.php?title=Driving_Hobby_Servos&amp;diff=291&amp;oldid=prev"/>
				<updated>2016-07-23T00:46:04Z</updated>
		
		<summary type="html">&lt;p&gt;Added Hobby RC Servo modules&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Hobby RC Servo modules like this can be controlled from KFLOP.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HobbyServo.png|none|link=]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most Servo Modules require a 1-2ms pulse at 50Hz to command their position.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HobbyServoPulse.png|none|link=]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is somewhat difficult to generate with normal PWM generators as high Time resolution is required at a slow rate.  KFLOP Hardware PWM generators have a minimum frequency of 254Hz.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A KFLOP User C Program can be used to enable and disable a PWM generator in a way that one pulse is output only every N PWM cycles.  PWM gernerator #1 is sacrificed to generate a fixed long PWM pulse that the C Program can monitor to count cycles as well as determine when the next PWM cycle for all the PWM generators will begin.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;brush:c&amp;quot;&amp;gt;#include &amp;quot;KMotionDef.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void ServiceHobbyPWM(void);&lt;br /&gt;
&lt;br /&gt;
int ncycles, v1, v2;&lt;br /&gt;
&lt;br /&gt;
main()&lt;br /&gt;
{&lt;br /&gt;
	int LowClocks, scaler=150;&lt;br /&gt;
	float PWMPeriod, BaseFreq=16.6667e6, PulseGap=20e-3, SafetyTime=200e-6;&lt;br /&gt;
	// Set base PWM to be high for entire time less 200us&lt;br /&gt;
	&lt;br /&gt;
	LowClocks = SafetyTime * BaseFreq / scaler;&lt;br /&gt;
	PWMPeriod = 256.0*scaler/BaseFreq;&lt;br /&gt;
	ncycles = PulseGap/PWMPeriod;&lt;br /&gt;
	printf(&amp;quot;PWM Period = %fus\n&amp;quot;, PWMPeriod*1e6);&lt;br /&gt;
	printf(&amp;quot;Low Clocks = %d\n&amp;quot;, LowClocks);&lt;br /&gt;
	printf(&amp;quot;Skip Cycles = %d\n&amp;quot;, ncycles);&lt;br /&gt;
	&lt;br /&gt;
	SetBitDirection(26,1);  // define bit as an output&lt;br /&gt;
	ClearBit(26);&lt;br /&gt;
	SetBitDirection(27,1);  // define bit as an output&lt;br /&gt;
	ClearBit(27);&lt;br /&gt;
	SetBitDirection(28,1);  // define bit as an output&lt;br /&gt;
	ClearBit(28);&lt;br /&gt;
	&lt;br /&gt;
	FPGA(IO_PWMS_PRESCALE) = scaler;  	// divide clock by 256 (250Hz)&lt;br /&gt;
	FPGA(IO_PWMS) = 255-LowClocks;  	// Mostly high pulse&lt;br /&gt;
	FPGA(IO_PWMS+1) = 1;  			// Enable&lt;br /&gt;
	ClearBit(26);&lt;br /&gt;
	&lt;br /&gt;
	v1 =  800e-6 * BaseFreq / scaler;  // set Pulse time&lt;br /&gt;
	v2 = 1700e-6 * BaseFreq / scaler;  // set Pulse time&lt;br /&gt;
&lt;br /&gt;
	printf(&amp;quot;PWM settings v1 = %d v2 = %d\n&amp;quot;, v1,v2);&lt;br /&gt;
&lt;br /&gt;
	for (;;)&lt;br /&gt;
	{&lt;br /&gt;
		ServiceHobbyPWM();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void ServiceHobbyPWM(void)&lt;br /&gt;
{&lt;br /&gt;
	static int i=0;&lt;br /&gt;
	static WaitLevel=0;&lt;br /&gt;
	&lt;br /&gt;
	if (!WaitLevel) // waiting for low?&lt;br /&gt;
	{&lt;br /&gt;
		if (!ReadBit(26)) // yes is it low?&lt;br /&gt;
		{&lt;br /&gt;
			WaitLevel=1;  // yes, change state&lt;br /&gt;
			if (++i == ncycles)  // count mostly high pulses, special cycle?&lt;br /&gt;
			{&lt;br /&gt;
				FPGA(IO_PWMS+2) = v1; 	// Pulse size&lt;br /&gt;
				FPGA(IO_PWMS+3) = 1;	// Enable&lt;br /&gt;
				FPGA(IO_PWMS+4) = v2; 	// Pulse size&lt;br /&gt;
				FPGA(IO_PWMS+5) = 1;	// Enable&lt;br /&gt;
				i=0;&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
			{&lt;br /&gt;
				FPGA(IO_PWMS+3) = 0;   // Disable&lt;br /&gt;
				FPGA(IO_PWMS+5) = 0;   // Disable&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		if (ReadBit(26)) // no, waiting for high, is it high?&lt;br /&gt;
		{&lt;br /&gt;
			WaitLevel=0; // yes, change state&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code above prints this:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PWM Period = 2303.995425us&amp;lt;br /&amp;gt;Low Clocks = 22&amp;lt;br /&amp;gt;Skip Cycles = 8&amp;lt;br /&amp;gt;PWM settings v1 = 88 v2 = 188&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It generates the timing shown below.  Note the yellow trace is the reference PWM used by the C Program to count the cycles.  Two PWM pulses are emitted every 8 cycles.  The cyan trace shows a pulse width of 780us (specified in the program to be 800us) with a period of 18.5ms.  The magenta trace shows a pulse width of 1.7ms (specified in the program to be 1700us) with a period of 18.5ms.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:HobbyServoTwoPulses.png|none|link=]]&lt;/div&gt;</summary>
		<author><name>TK</name></author>	</entry>

	</feed>