pigpio/DOC/HTML/ex_rotary_encoder.html

241 lines
9.2 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="description" content="Raspberry Pi Reg. C GPIO library and Python GPIO module and shell command utilities to control the GPIO, including SPI, I2C, and serial links." />
<meta name="keywords" content="raspberry, pi, C, Python, GPIO, library, shell, command, utilities, module, SPI, I2C, serial" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>pigpio library</title>
<link rel="stylesheet" type="text/css" href="scripts/index.css">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<table style="padding:0px; border:0px; margin:0px; width:780px; background-color:#e0e0e0;">
<td style="background:#EAF2E6 url('images/sidebar.gif') repeat-y; width:35px; height:100%"></td>
<td>
<table>
<div style="background:url('images/topbar.gif') repeat-x; height: 70px; font-size:1.5em; vertical-align: top;"><a href="index.html"><img src="images/pigpio-logo.gif" border="0" /></a>pigpio library</div>
</table>
<table><div>
<td><img src="images/msp430.jpg" width="250"></td>
<td><img src="images/leds.jpg" width="250"></td>
<td><img src="images/ldr.jpg" width="250"></td>
</div></table>
<table>
<td style="vertical-align: top; background-color: #98bf21;"><a class="l1" href="index.html">pigpio</a>
<a class="l1" href="cif.html">pigpio C I/F</a>
<a class="l1" href="pigpiod.html">pigpiod</a>
<a class="l1" href="pdif2.html">pigpiod C I/F</a>
<a class="l1" href="python.html">Python</a>
<a class="l1" href="pigs.html">pigs</a>
<a class="l1" href="piscope.html">piscope</a>
<a class="l1" href="misc.html">Misc</a>
<a class="l1" href="examples.html">Examples</a>
<a class="l1" href="download.html">Download</a>
<a class="l1" href="faq.html">FAQ</a>
<a class="l1" href="sitemap.html">Site Map</a>
</td>
<td><center><h2>Rotary Encoder Example</h2></center>
<p>The following code shows one way to read an incremental
mechanical rotary enoder (the sort used for volume control in audio
systems).&nbsp; These rotary encoders have two switches A and B
which return a quadrature output, i.e. they are 90 degrees out of
phase.</p>
<h3>SETUP</h3>
<img alt="fritzing diagram" style="width: 200px; height: 300px;"
src="images/re-fritz.png" align="left" hspace="10">The common
(centre) terminal should be connected to a Pi ground.
<p>The A and B terminals may be connected to any spare gpios.</p>
<p>Here A to gpio18 (P1-12), common to ground (P1-20), B to gpio7
(P1-26).<br clear="all"></p>
<p><img src="images/re-photo.jpg" style=
"width: 400px; height: 300px;" alt="photo of set-up"></p>
<h3>CODE</h3>
<code>#include &lt;stdio.h&gt;<br>
<br>
#include &lt;pigpio.h&gt;<br>
<br>
/*<br>
&nbsp;&nbsp; Rotary encoder connections:<br>
<br>
&nbsp;&nbsp; Encoder A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - gpio
18&nbsp;&nbsp; (pin P1-12)<br>
&nbsp;&nbsp; Encoder B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - gpio
7&nbsp;&nbsp;&nbsp; (pin P1-26)<br>
&nbsp;&nbsp; Encoder Common - Pi ground (pin P1-20)<br>
*/<br>
<br>
#define ENCODER_A 18<br>
#define ENCODER_B&nbsp; 7<br>
<br>
static volatile int encoderPos;<br>
<br>
/* forward declaration */<br>
<br>
void encoderPulse(int gpio, int lev, uint32_t tick);<br>
<br>
int main(int argc, char * argv[])<br>
{<br>
&nbsp;&nbsp; int pos=0;<br>
<br>
&nbsp;&nbsp; if (gpioInitialise()&lt;0) return 1;<br>
<br>
&nbsp;&nbsp; gpioSetMode(ENCODER_A, PI_INPUT);<br>
&nbsp;&nbsp; gpioSetMode(ENCODER_B, PI_INPUT);<br>
<br>
&nbsp;&nbsp; /* pull up is needed as encoder common is grounded
*/<br>
<br>
&nbsp;&nbsp; gpioSetPullUpDown(ENCODER_A, PI_PUD_UP);<br>
&nbsp;&nbsp; gpioSetPullUpDown(ENCODER_B, PI_PUD_UP);<br>
<br>
&nbsp;&nbsp; encoderPos = pos;<br>
<br>
&nbsp;&nbsp; /* monitor encoder level changes */<br>
<br>
&nbsp;&nbsp; gpioSetAlertFunc(ENCODER_A, encoderPulse);<br>
&nbsp;&nbsp; gpioSetAlertFunc(ENCODER_B, encoderPulse);<br>
<br>
&nbsp;&nbsp; while (1)<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pos != encoderPos)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos =
encoderPos;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("pos=%d\ ",
pos);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gpioDelay(20000); /* check pos 50
times per second */<br>
&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp; gpioTerminate();<br>
}<br>
<br>
void encoderPulse(int gpio, int level, uint32_t tick)<br>
{<br>
&nbsp;&nbsp; /*<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+---------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+---------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp; A&nbsp;&nbsp;&nbsp;&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
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>
&nbsp;&nbsp; ----+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+---------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+---------+&nbsp; 1<br>
<br>
&nbsp;&nbsp; */<br>
<br>
&nbsp;&nbsp; static int levA=0, levB=0, lastGpio = -1;<br>
<br>
&nbsp;&nbsp; if (gpio == ENCODER_A) levA = level; else levB =
level;<br>
<br>
&nbsp;&nbsp; if (gpio != lastGpio) /* debounce */<br>
&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lastGpio = gpio;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((gpio == ENCODER_A) &amp;&amp;
(level == 0))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!levB)
++encoderPos;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if ((gpio == ENCODER_B)
&amp;&amp; (level == 1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (levA)
--encoderPos;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp; }<br>
}<br></code>
<h3>BUILD</h3>
<code>cc -o rotary_encoder rotary_encoder.c -lpigpio -lrt
-lpthread<br></code>
<h3>RUN</h3>
<code>sudo ./rotary_encoder</code><br>
<p>While the program is running you can capture the waveform using
the notification feature built in to pigpio.&nbsp; Issue the
following commands on the Pi.</p>
<code>pigs no<br>
pig2vcd&nbsp; &lt;/dev/pigpio0 &gt;re.vcd &amp;<br>
pigs nb 0 0x40080 # set bits for gpios 7 (0x80) and 18
(0x40000)<br></code>
<p>Twiddle the rotary encoder forwards and backwards for a few
seconds.&nbsp; Then enter<br></p>
<code>pigs nc 0</code><br>
<p>The file re.vcd will contain the captured waveform, which can be
viewed using GTKWave.</p>
<br>
Overview<br>
<br>
<img src="images/re-wave-1.png" style=
"width: 600px; height: 100px;" alt=
"rotary encoder waveform overview"><br>
<br>
Detail of switch bounce.&nbsp; Contact A bounces for circa 700 us
before completing the level transition<br>
<br>
<img src="images/re-wave-2.png" style=
"width: 600px; height: 100px;" alt=
"rotary encoder waveform detail"><br>
</td>
</table>
<div style="vertical-align: center; text-align: center; background-color:#98bf21; font-size:0.8em; height:30px"><a class="l2" href="index.html">[pigpio]</a>
<a class="l2" href="cif.html">[pigpio C I/F]</a>
<a class="l2" href="pigpiod.html">[pigpiod]</a>
<a class="l2" href="pdif2.html">[pigpiod C I/F]</a>
<a class="l2" href="python.html">[Python]</a>
<a class="l2" href="pigs.html">[pigs]</a>
<a class="l2" href="piscope.html">[piscope]</a>
<a class="l2" href="misc.html">[Misc]</a>
<a class="l2" href="examples.html">[Examples]</a>
<a class="l2" href="download.html">[Download]</a>
<a class="l2" href="faq.html">[FAQ]</a>
<a class="l2" href="sitemap.html">[Site Map]</a>
</div>
<table><tr>
<td style="width: 200px"><div style="text-align: left;"><small>&copy; 2012-2020</small></div></td>
<td style="width: 350px"><div style="text-align: center;">e-mail: pigpio @ abyz.me.uk</div></td>
<td style="width: 200px"><div style="text-align: right;"><small>Updated: 30/04/2020</small></div></td>
</tr></table>
</td>
</table>
</body>
</html>