<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://hades.mech.northwestern.edu//index.php?action=history&amp;feed=atom&amp;title=C_Example%3A_Bi-Directional_PWM_Motor_Control</id>
	<title>C Example: Bi-Directional PWM Motor Control - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://hades.mech.northwestern.edu//index.php?action=history&amp;feed=atom&amp;title=C_Example%3A_Bi-Directional_PWM_Motor_Control"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_Bi-Directional_PWM_Motor_Control&amp;action=history"/>
	<updated>2026-05-20T06:53:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=C_Example:_Bi-Directional_PWM_Motor_Control&amp;diff=4970&amp;oldid=prev</id>
		<title>LIMS at 20:56, 25 June 2007</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_Bi-Directional_PWM_Motor_Control&amp;diff=4970&amp;oldid=prev"/>
		<updated>2007-06-25T20:56:30Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 15:56, 25 June 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 63:&lt;/td&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 63:&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;            set_pwm1_duty(0);&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;            set_pwm1_duty(0);&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;            set_pwm2_duty(0);}&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;            set_pwm2_duty(0);}&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/td&gt;
  &lt;td colspan=&quot;2&quot; class=&quot;diff-empty diff-side-added&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   }&lt;/div&gt;&lt;/td&gt;
  &lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   }&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>LIMS</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=C_Example:_Bi-Directional_PWM_Motor_Control&amp;diff=4969&amp;oldid=prev</id>
		<title>LIMS at 20:56, 25 June 2007</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_Bi-Directional_PWM_Motor_Control&amp;diff=4969&amp;oldid=prev"/>
		<updated>2007-06-25T20:56:08Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
==Code==&lt;br /&gt;
Program to run a motor bidirectionally at varying speeds depending&lt;br /&gt;
on the position of a potentiometer.&lt;br /&gt;
&lt;br /&gt;
First include header file with definitions for specific PIC.&lt;br /&gt;
Set fuses.  HS is type of external clock, low voltage protection&lt;br /&gt;
(LVP) is off, and the watchdog timer (WDT) is off.&lt;br /&gt;
External clock frequency of 20 MHz is specified.&lt;br /&gt;
&lt;br /&gt;
   #include &amp;lt;18F4520.h&amp;gt;&lt;br /&gt;
   #fuses HS,NOWDT,NOPROTECT,NOLVP&lt;br /&gt;
   #use delay(clock=20000000)&lt;br /&gt;
&lt;br /&gt;
Introduce the variables to be used in the main program.&lt;br /&gt;
&lt;br /&gt;
   int read;&lt;br /&gt;
   int readlow;&lt;br /&gt;
   int readhigh;&lt;br /&gt;
&lt;br /&gt;
Begin main body of program.&lt;br /&gt;
&lt;br /&gt;
   void main() {&lt;br /&gt;
&lt;br /&gt;
Setup the CCP1 and CCP2 to be PWM outputs. Also set the timer&lt;br /&gt;
to be used for PWM.&lt;br /&gt;
&lt;br /&gt;
      setup_ccp1(CCP_PWM);&lt;br /&gt;
      setup_ccp2(CCP_PWM);&lt;br /&gt;
      setup_timer_2(T2_DIV_BY_1, 255, 1);&lt;br /&gt;
&lt;br /&gt;
Setup analog input, and which analog channel is active.&lt;br /&gt;
&lt;br /&gt;
      setup_adc_ports(AN0_ANALOG);&lt;br /&gt;
      setup_adc(ADC_CLOCK_INTERNAL);&lt;br /&gt;
      set_adc_channel(0);&lt;br /&gt;
&lt;br /&gt;
Setup an infinite loop, using a while statement.&lt;br /&gt;
&lt;br /&gt;
      while(TRUE) {&lt;br /&gt;
&lt;br /&gt;
Read the analog input and define &amp;quot;readhigh&amp;quot; and &amp;quot;readlow&amp;quot;&lt;br /&gt;
as functions of &amp;quot;read&amp;quot;. Note that the value of &amp;quot;readhigh&amp;quot;&lt;br /&gt;
varies from 0 to 255 when the potentiometer is between&lt;br /&gt;
140 and 255, and &amp;quot;readlow&amp;quot; behaves similarly.&lt;br /&gt;
&lt;br /&gt;
         read = read_adc();&lt;br /&gt;
         readlow = (120-read)*2.125;&lt;br /&gt;
         readhigh = (read-140)*2.217;&lt;br /&gt;
&lt;br /&gt;
Check the value of &amp;quot;read&amp;quot;, and set the duty cycle of each PWM&lt;br /&gt;
output signal accordingly.  The H-bridge circuit used requires&lt;br /&gt;
two inputs (the two PWM outputs of the 18F4520) to control the&lt;br /&gt;
direction and speed of the driven motor.&lt;br /&gt;
&lt;br /&gt;
         if (read&amp;lt;120){&lt;br /&gt;
            set_pwm1_duty(readlow);&lt;br /&gt;
            set_pwm2_duty(0);}&lt;br /&gt;
         else if (read&amp;gt;140){&lt;br /&gt;
            set_pwm1_duty(0);&lt;br /&gt;
            set_pwm2_duty(readhigh);}&lt;br /&gt;
         else {&lt;br /&gt;
            set_pwm1_duty(0);&lt;br /&gt;
            set_pwm2_duty(0);}&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
==Associated Circuitry==&lt;/div&gt;</summary>
		<author><name>LIMS</name></author>
	</entry>
</feed>