mirror of https://github.com/joan2937/pigpio
241 lines
9.2 KiB
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). 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 <stdio.h><br>
|
|
<br>
|
|
#include <pigpio.h><br>
|
|
<br>
|
|
/*<br>
|
|
Rotary encoder connections:<br>
|
|
<br>
|
|
Encoder A - gpio
|
|
18 (pin P1-12)<br>
|
|
Encoder B - gpio
|
|
7 (pin P1-26)<br>
|
|
Encoder Common - Pi ground (pin P1-20)<br>
|
|
*/<br>
|
|
<br>
|
|
#define ENCODER_A 18<br>
|
|
#define ENCODER_B 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>
|
|
int pos=0;<br>
|
|
<br>
|
|
if (gpioInitialise()<0) return 1;<br>
|
|
<br>
|
|
gpioSetMode(ENCODER_A, PI_INPUT);<br>
|
|
gpioSetMode(ENCODER_B, PI_INPUT);<br>
|
|
<br>
|
|
/* pull up is needed as encoder common is grounded
|
|
*/<br>
|
|
<br>
|
|
gpioSetPullUpDown(ENCODER_A, PI_PUD_UP);<br>
|
|
gpioSetPullUpDown(ENCODER_B, PI_PUD_UP);<br>
|
|
<br>
|
|
encoderPos = pos;<br>
|
|
<br>
|
|
/* monitor encoder level changes */<br>
|
|
<br>
|
|
gpioSetAlertFunc(ENCODER_A, encoderPulse);<br>
|
|
gpioSetAlertFunc(ENCODER_B, encoderPulse);<br>
|
|
<br>
|
|
while (1)<br>
|
|
{<br>
|
|
if (pos != encoderPos)<br>
|
|
{<br>
|
|
pos =
|
|
encoderPos;<br>
|
|
printf("pos=%d\ ",
|
|
pos);<br>
|
|
}<br>
|
|
gpioDelay(20000); /* check pos 50
|
|
times per second */<br>
|
|
}<br>
|
|
<br>
|
|
gpioTerminate();<br>
|
|
}<br>
|
|
<br>
|
|
void encoderPulse(int gpio, int level, uint32_t tick)<br>
|
|
{<br>
|
|
/*<br>
|
|
<br>
|
|
|
|
+---------+
|
|
+---------+ 0<br>
|
|
|
|
|
|
|
|
|
|
| |<br>
|
|
A
|
|
|
|
|
|
|
|
| |<br>
|
|
|
|
|
|
|
|
|
|
| |<br>
|
|
|
|
+---------+
|
|
+---------+ +-----
|
|
1<br>
|
|
<br>
|
|
|
|
+---------+
|
|
+---------+
|
|
0<br>
|
|
|
|
|
|
|
|
|
|
| |<br>
|
|
B
|
|
|
|
|
|
|
|
| |<br>
|
|
|
|
|
|
|
|
|
|
| |<br>
|
|
----+
|
|
+---------+
|
|
+---------+ 1<br>
|
|
<br>
|
|
*/<br>
|
|
<br>
|
|
static int levA=0, levB=0, lastGpio = -1;<br>
|
|
<br>
|
|
if (gpio == ENCODER_A) levA = level; else levB =
|
|
level;<br>
|
|
<br>
|
|
if (gpio != lastGpio) /* debounce */<br>
|
|
{<br>
|
|
lastGpio = gpio;<br>
|
|
<br>
|
|
if ((gpio == ENCODER_A) &&
|
|
(level == 0))<br>
|
|
{<br>
|
|
if (!levB)
|
|
++encoderPos;<br>
|
|
}<br>
|
|
else if ((gpio == ENCODER_B)
|
|
&& (level == 1))<br>
|
|
{<br>
|
|
if (levA)
|
|
--encoderPos;<br>
|
|
}<br>
|
|
}<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. Issue the
|
|
following commands on the Pi.</p>
|
|
<code>pigs no<br>
|
|
pig2vcd </dev/pigpio0 >re.vcd &<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. 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. 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>© 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>
|