libpruio  0.0.2
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
src/_Examples.md
Go to the documentation of this file.
1 Examples {#ChaExamples}
2 ========
3 \tableofcontents
4 
5 This page contains some example code. A very simple example is shown as
6 source code here. The other examples are described in text form. Find
7 the corresponding source code and pre-compiled binaries in the folder
8 *examples*.
9 
10 \note Make sure you followed the installation instructions in \ref
11  SubPreSystem before you execute a sample program.
12 
13 Feel free to send your testing code (max. 200 LOC) to the author for
14 extending this section:
15 
16 \Email
17 
18 
19 Console examples {#SecExaConsole}
20 ================
21 
22 The following examples are designed to run in text mode at the console.
23 
24 
25 1.bas {#SecExaSimple}
26 -----
27 
28 This example operates the driver in IO mode. Default settings are used
29 to read the analog input lines AIN 0 to 7 and output 13 lines of sampled
30 data at the console. It's designed as a minimal example to show the
31 basic code.
32 
33 The output of the program looks like (FreeBASIC version)
34 
35 ~~~{.txt}
36  D530 E0C0 DE20 CEE0 0080 0780 1850 EE20
37  E990 E150 DD80 D010 0000 07A0 1770 EE00
38  E980 E230 DE70 CEE0 0070 07B0 17A0 EE30
39  E9C0 E110 DD50 CF00 0090 08C0 18D0 EE40
40  E8D0 E0B0 DD10 CE60 0010 0880 1800 EE40
41  E9B0 E210 DDC0 CEC0 0090 0790 1850 EE00
42  E970 E220 DE40 D020 00D0 0840 17D0 EE70
43  E9D0 E1C0 DD90 CE90 00B0 06F0 1830 EE70
44  E920 E120 DC90 CD80 0040 0750 1850 EDF0
45  E8E0 E0D0 DD60 CEC0 00B0 0810 1780 EEB0
46  E8D0 E130 DD00 CE90 0000 0730 17D0 EE70
47  EA20 E210 DDD0 CED0 00E0 0820 1850 EEA0
48  E960 E0D0 DDE0 CF10 0010 0740 17B0 EE40
49 ~~~
50 
51 First the source code of the FreeBASIC version (unfortunately with
52 incorrect syntax highlighting - this Doxygen version cannot switch off
53 the highlighting):
54 
55 ~~~{.bas}
56 #INCLUDE ONCE "../pruio/pruio.bi" '* include header
57 VAR io = NEW PruIo() '*< create new driver UDT
58 io->config() ' upload (default) settings, start IO mode
59 
60 '' now current ADC samples are available for AIN0 to AIN7 in array Value[]
61 
62 FOR n AS INTEGER = 1 TO 13 '' print some lines
63  FOR i AS INTEGER = 1 TO 8 '' all steps
64  PRINT " " & HEX(io->Value[i], 4); '' output one channel as hexadecimal
65  NEXT
66  PRINT '' next line
67 NEXT
68 
69 '' we're done
70 
71 DELETE io ' destroy driver UDT
72 ~~~
73 
74 And here's a similar version in C syntax:
75 
76 \dontinclude examples/1.c
77 \skipline #include
78 \until } // end of main
79 
80 \note C syntax doesn't support default parameters. You've to pass all
81  parameters when using a C compiler.
82 
83 
84 analyse.bas {#SubExaAnalyse}
85 -----------
86 
87 This example shows how to get information about the original devices
88 configurations. It creates a new PruIo instance and analyses the
89 original (initial) configuration of the devices GPIOs, Control Module
90 and ADC. It's designed to run in console mode and stops after printing
91 out a listing like
92 
93 ~~~{.txt}
94 GPIO0 was on
95  OE: 11111111111111111111111111111111
96  INT0: 00000000000000000000000000000000
97  INT1: 00000000000000000000000000000000
98 GPIO1 was on
99  OE: 11110110000011111111111111111111
100  ...
101 Unlocked Pins:
102  P8_19, GPIO 0/22: input, pulldown
103  ...
104 Locked header Pins:
105  P8_03, mode 1 (locked)
106  ...
107 ADC was off
108  REVISION: 47300001
109  wakeup-count: 00000002
110 ~~~
111 
112 A similar version in C syntax is included named analyse.c.
113 
114 
115 io_input.bas {#SubExaIoInp}
116 ------------
117 
118 This example shows how to get input from digital and analog devices. It
119 creates a new PruIo instance and prints out the state of all the
120 digital and analog lines. It's designed to run in console mode and
121 creates continuous output like
122 
123 ~~~{.txt}
124  . | . | . | . |
125 00000000000000000000000000000000
126 00001000101000000000000000000000
127 00000000000000000000000000100100
128 00000000000000000000000000000000
129 EF10 E850 E660 D8C0 2C60 2C00 3710 EE80
130 ~~~
131 
132 The first line is a scale to support identifying the bit positions. The
133 next four lines show the state of the GPIOs 0 to 3. The last line is
134 the sampled ADC data form AIN 0 to 7 as hexadecimal values in 16 bit
135 format.
136 
137 You can watch the heartbeat (user LED 0) in the third line (GPIO-1, bit
138 21). The last analog value (AIN7) is the measured voltage on the board
139 (it should be the half of 3.3 V: &hEE80 * 1.8 V / (4095 SHL 4) = 1.677
140 V).
141 
142 To quit the program press any key.
143 
144 A version in C syntax is included named io_inout.c. This version prints
145 out all values in one line and uses octal numbers for the GPIO state,
146 instead of binary numbers.
147 
148 
149 sos.bas {#SubExaSos}
150 -------
151 
152 This example shows how to unlock and control an internal CPU ball. It
153 creates a new PruIo instance and unlocks the User3 LED ball as output.
154 Then it continuously blinks this LED in SOS code (short, short, short -
155 long, long, long - short, short, short). The output looks like:
156 
157 ~~~{.txt}
158 watch SOS code on user LED 3 (near ethernet connector)
159 
160 execute this command to get rid of mmc1 triggers
161  sudo su && echo none > /sys/class/leds/beaglebone:green:usr3/trigger && exit
162 
163 press any key to quit
164 ~~~
165 
166 The blinking code may get disturbed by the system (kernel), which is
167 also blinking the LED on mmc1 interrupts. Execute the mentioned command
168 to get rid of this interferences. (Replace 'none' by 'mmc1' to restore
169 the original configuration.)
170 
171 To quit the program press any key.
172 
173 A similar version in C syntax is included named sos.c.
174 
175 
176 button.bas {#SubExaButton}
177 -----------
178 
179 This example shows how to use a header pin as input. It creates a new
180 PruIo instance and sets pin 7 on header P8 in input mode with pullup
181 restistor, so that the GPIO state is high (= 1) when unconnected. The
182 program outputs a single line containing the GPIO state, updated
183 continously until any keypress.
184 
185 When the pin gets grounded, the state changes to low (= 0). This can be
186 done by connecting a cable between P8_07 (GPIO input) and P8_01 (GND).
187 Or you connect a button to these pins an see the state changing to zero
188 when pressing the button. Instead, you also can connect a photoelectric
189 sensor (ie. the detector part of a forked light barrier: + on P8_07, -
190 on P8_01, and supply the emitter part by an appropriate voltage /
191 current).
192 
193 ~~~{.txt}
194 1
195 ~~~
196 
197 A similar version in C syntax is included named button.c.
198 
199 
200 stepper.bas {#SubExaStepper}
201 -----------
202 
203 This example shows how to use header pins as output. It creates a new
204 PruIo instance and sets four header pins in output mode to control a
205 stepper motor. Connect a common GRND and the inputs of the stepper
206 driver board to pins H8_08, H8_10, H8_12 and H8_14. Ie. use a x113647
207 driver board and a 28BYJ-48 stepper motor. (Note: the header pin output
208 is 3.3 V by a maximum current of 5 mA). This program is designed to
209 run in console mode and prints out user information like
210 
211 ~~~{.txt}
212 Controls: (other keys quit, 1 and 3 only when Direction = 0)
213  8 = faster
214  4 = rotate CW 5 = stop, hold position 6 = rotate CCW
215  1 = single step CW 2 = slower 3 = single step CCW
216  0 = stop, power off
217 
218 Direction Speed
219  0 2
220 ~~~
221 
222 The user can control speed and direction of the motor by pressing the
223 listed keys. Ie. press
224 
225 - key 1 to move a single step,
226 - then key 4 to start continuous rotation,
227 - then key 8 to speed up rotation or
228 - key 2 to slow down,
229 
230 and so on. Any other (not listed) keystroke quits the program.
231 
232 A similar version in C syntax is included named stepper.c.
233 
234 
235 Grafic examples {#SecExaGrafic}
236 ===============
237 
238 The following examples are designed to run in grafic mode. They open a
239 grafic window. You need a display connected to the Beaglebone (ie. a
240 HDMI monitor). Sorry, no C versions (due to long and complex code for
241 grafics).
242 
243 
244 oszi.bas {#SubExaOszi}
245 --------
246 
247 This example shows how to receive fast ADC input in IO mode. It opens a
248 grafic window and creates a new PruIo instance for drawing continuously
249 the sampled analog data from all channels (AIN0 to AIN7) as line grafic
250 with colored lines. By default it creates a full screen window without
251 a frame. You can customize the window by setting the size as command
252 line option (ie like `./oszi 640x400` for width = 640 and hight = 400).
253 
254 The grafic is scaled 0 V at the bottom and 1.8 V at the top. You can
255 toggle the channels on or off by pressing keys 0 to 7 (0 = AIN0, ... 7
256 = AIN7), but you cannot de-activate the last channel (at least one
257 channel stays active). Key '+' restores the default setting (all
258 channels active). Any other key quits the program.
259 
260 The less channels are activated, the faster the ADC device samples the
261 data. Ie. check it by connecting a constant frequency sine wave (0 to
262 maximal 1.8 V) to a channel and switch off the others one by one.
263 
264 
265 triggers.bas {#SubExaTriggers}
266 ------------
267 
268 This example shows how to use triggers to start a measurement in MM
269 mode and how to customize an ADC step (for the analog trigger 2). It
270 opens a grafic window and creates a new PruIo instance for MM. By
271 default it creates a full screen window without a frame. You can
272 customize the window by setting the size as command line option (ie
273 like `./triggers 640x400` for width = 640 and hight = 400).
274 
275 The grafic is scaled 0 V at the bottom and 1.8 V at the top. The
276 sampling rate is 1 kHz. You can start a measurement of two channels
277 (AIN4 and AIN7) by a trigger. Choose one of the trigger types form the
278 menu
279 
280 ~~~{.txt}
281 Choose trigger type
282  0 = no trigger (start immediately)
283  1 = GPIO trigger (pin P8_07)
284  2 = analog trigger (AIN4 > 0.9 V)
285  3 = analog pre-trigger (any AIN < 0.9 V)
286 ~~~
287 
288 Further keystrokes (other than 0 to 3) quit the program.
289 
290 After choosing a trigger type (1 to 3), the program waits for the
291 trigger event. So make sure to prepare a cable to connect header pins
292 before testing.
293 
294 Here's an instruction how you can test the program. First, execute the
295 binary with a customized window size by
296 
297 ~~~{.txt}
298 ./triggers 640x400
299 ~~~
300 
301 and you'll see the above mentioned menu in an empty grafic window. The
302 next steps depend on the trigger to test:
303 
304 <b>0 = no trigger (start immediately)</b>
305 -# Press key 0. After a short while (less than 1 second) a red and a
306  white line show up in the window. White = AIN4 (open connector) shows
307  any signal and red = AIN7 (board voltage = 1.65 V) is always at the
308  top of the window. The menu showns up again for the next test.
309 
310 <b>1 = GPIO trigger (pin P8_07)</b>
311 -# Press key 1 to start the trigger. You'll see a new message `waiting
312  for GPIO trigger (pin P8_07 low) ...`. Nothing else happens.
313 -# Use a cable to connect P8_01 (GRND) and P8_07 (trigger pin). After a
314  short while (less than 1 second) a red and a white line show up in
315  the window. White = AIN4 (open connector) shows any signal and red =
316  AIN7 (board voltage = 1.65 V) is always at the top of the window. The
317  menu showns up again for the next test.
318 
319 <b>2 = analog trigger (AIN4 > 0.9 V)</b>
320 -# Connect a cable between P9_34 (AGND) and P9_33 (AIN4).
321 -# Press key 2 to start the trigger. You'll see a new message `waiting
322  for analog trigger (AIN4 > 0.9 V) ...`. Nothing else happens.
323 -# Release the cable. The channel AIN7 will pull up the open trigger
324  channel AIN4 to a voltage above the trigger voltage. (If this doesn't
325  happen, connect the cable between P9_32 (VADC) and P9_33 (AIN4).)
326  After a short while (less than 1 second) a red and a white line show
327  up in the the window. White = AIN4 (open connector) starts on the
328  left side at the middle of the window hight (the trigger event) and
329  shows any signal. Red = AIN7 (board voltage = 1.65 V) is always at
330  the top of the window. The menu showns up again for the next test.
331 
332 <b>3 = analog pre-trigger (any AIN < 0.9 V)</b>
333 -# Connect a cable between P9_32 (VADC) and P9_33 (AIN4).
334 -# Press key 3 to start the trigger. You'll see a new message `waiting
335  for analog pre-trigger (any AIN < 0.9 V) ...`. Nothing else happens.
336 -# Release the cable. The open trigger channel AIN4 starts to swing
337  below the trigger voltage. (If this doesn't happen, connect the cable
338  between P9_34 (AGND) and P9_33 (AIN4).) After a short while (less
339  than 1 second) a red and a white line show up in the the window.
340  White = AIN4 (open connector or AGND) goes across the middle of the
341  window (the trigger event) and shows any signal in the upper left
342  (pre-trigger samples) and some parts of the curve in the lower right
343  (regular samples after trigger event). Red = AIN7 (board voltage =
344  1.65 V) is always at the top of the window. The menu showns up again
345  for the next test.