<?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_PWM_Motor_Control</id>
	<title>C Example: 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_PWM_Motor_Control"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_PWM_Motor_Control&amp;action=history"/>
	<updated>2026-04-06T12:05:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=C_Example:_PWM_Motor_Control&amp;diff=4968&amp;oldid=prev</id>
		<title>LIMS at 20:53, 25 June 2007</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=C_Example:_PWM_Motor_Control&amp;diff=4968&amp;oldid=prev"/>
		<updated>2007-06-25T20:53:09Z</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 at different speeds using pulse width&lt;br /&gt;
modulation (PWM), controlled by a push button.&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;
Define pin names to be used in the main program. See header file&lt;br /&gt;
for currently defined pin names.&lt;br /&gt;
&lt;br /&gt;
   #define BUTTON PIN_A4&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 pin (PIN_C2 on the 18F4520 chip) to be a PWM&lt;br /&gt;
output. Also set the timer to be used for PWM.&lt;br /&gt;
&lt;br /&gt;
      setup_ccp1(CCP_PWM);&lt;br /&gt;
      setup_timer_2(T2_DIV_BY_1, 255, 1);&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;
Check whether button is pressed or not, and vary duty cycle&lt;br /&gt;
of PWM accordingly. Note that duty cycle is defined from 0-255&lt;br /&gt;
with 0 being 0% and 255 being 100% (8-bit number).&lt;br /&gt;
&lt;br /&gt;
         if (input(BUTTON)){&lt;br /&gt;
            set_pwm1_duty(235);}&lt;br /&gt;
         else {&lt;br /&gt;
            set_pwm1_duty(135);}&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
==Associated Circuitry==&lt;/div&gt;</summary>
		<author><name>LIMS</name></author>
	</entry>
</feed>