libpruio  0.0
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 stepper.bas {#SubExaStepper}
177 -----------
178 
179 This example shows how to use header pins as output. It creates a new
180 PruIo instance and sets four header pins in output mode to control a
181 stepper motor. Connect a common GRND and the inputs of the stepper
182 driver board to pins H8_08, H8_10, H8_12 and H8_14. Ie. use a x113647
183 driver board and a 28BYJ-48 stepper motor. (Note: the header pin output
184 is 3.3 V by a maximum current of 5 mA). This program is designed to
185 run in console mode and prints out user information like
186 
187 ~~~{.txt}
188 Controls: (other keys quit, 1 and 3 only when Direction = 0)
189  8 = faster
190  4 = rotate CW 5 = stop, hold position 6 = rotate CCW
191  1 = single step CW 2 = slower 3 = single step CCW
192  0 = stop, power off
193 
194 Direction Speed
195  0 2
196 ~~~
197 
198 The user can control speed and direction of the motor by pressing the
199 listed keys. Ie. press
200 
201 - key 1 to move a single step,
202 - then key 4 to start continuous rotation,
203 - then key 8 to speed up rotation or
204 - key 2 to slow down,
205 
206 and so on. Any other (not listed) keystroke quits the program.
207 
208 A similar version in C syntax is included named stepper.c.
209 
210 
211 Grafic examples {#SecExaGrafic}
212 ===============
213 
214 The following examples are designed to run in grafic mode. They open a
215 grafic window. You need a display connected to the Beaglebone (ie. a
216 HDMI monitor). Sorry, no C versions (due to long and complex code for
217 grafics).
218 
219 
220 oszi.bas {#SubExaOszi}
221 --------
222 
223 This example shows how to receive fast ADC input in IO mode. It opens a
224 grafic window and creates a new PruIo instance for drawing continuously
225 the sampled analog data from all channels (AIN0 to AIN7) as line grafic
226 with colored lines. By default it creates a full screen window without
227 a frame. You can customize the window by setting the size as command
228 line option (ie like `./oszi 640x400` for width = 640 and hight = 400).
229 
230 The grafic is scaled 0 V at the bottom and 1.8 V at the top. You can
231 toggle the channels on or off by pressing keys 0 to 7 (0 = AIN0, ... 7
232 = AIN7), but you cannot de-activate the last channel (at least one
233 channel stays active). Key '+' restores the default setting (all
234 channels active). Any other key quits the program.
235 
236 The less channels are activated, the faster the ADC device samples the
237 data. Ie. check it by connecting a constant frequency sine wave (0 to
238 maximal 1.8 V) to a channel and switch off the others one by one.
239 
240 
241 triggers.bas {#SubExaTriggers}
242 ------------
243 
244 This example shows how to use triggers to start a measurement in MM
245 mode and how to customize an ADC step (for the analog trigger 2). It
246 opens a grafic window and creates a new PruIo instance for MM. By
247 default it creates a full screen window without a frame. You can
248 customize the window by setting the size as command line option (ie
249 like `./triggers 640x400` for width = 640 and hight = 400).
250 
251 The grafic is scaled 0 V at the bottom and 1.8 V at the top. The
252 sampling rate is 1 kHz. You can start a measurement of two channels
253 (AIN4 and AIN7) by a trigger. Choose one of the trigger types form the
254 menu
255 
256 ~~~{.txt}
257 Choose trigger type
258  0 = no trigger (start immediately)
259  1 = GPIO trigger (pin P8_07)
260  2 = analog trigger (AIN4 > 0.9 V)
261  3 = analog pre-trigger (any AIN < 0.9 V)
262 ~~~
263 
264 Further keystrokes (other than 0 to 3) quit the program.
265 
266 After choosing a trigger type (1 to 3), the program waits for the
267 trigger event. So make sure to prepare a cable to connect header pins
268 before testing.
269 
270 Here's an instruction how you can test the program. First, execute the
271 binary with a customized window size by
272 
273 ~~~{.txt}
274 ./triggers 640x400
275 ~~~
276 
277 and you'll see the above mentioned menu in an empty grafic window. The
278 next steps depend on the trigger to test:
279 
280 <b>0 = no trigger (start immediately)</b>
281 -# Press key 0. After a short while (less than 1 second) a red and a
282  white line show up in the window. White = AIN4 (open connector) shows
283  any signal and red = AIN7 (board voltage = 1.65 V) is always at the
284  top of the window. The menu showns up again for the next test.
285 
286 <b>1 = GPIO trigger (pin P8_07)</b>
287 -# Press key 1 to start the trigger. You'll see a new message `waiting
288  for GPIO trigger (pin P8_07 low) ...`. Nothing else happens.
289 -# Use a cable to connect P8_01 (GRND) and P8_07 (trigger pin). After a
290  short while (less than 1 second) a red and a white line show up in
291  the window. White = AIN4 (open connector) shows any signal and red =
292  AIN7 (board voltage = 1.65 V) is always at the top of the window. The
293  menu showns up again for the next test.
294 
295 <b>2 = analog trigger (AIN4 > 0.9 V)</b>
296 -# Connect a cable between P9_34 (AGND) and P9_33 (AIN4).
297 -# Press key 2 to start the trigger. You'll see a new message `waiting
298  for analog trigger (AIN4 > 0.9 V) ...`. Nothing else happens.
299 -# Release the cable. The channel AIN7 will pull up the open trigger
300  channel AIN4 to a voltage above the trigger voltage. (If this doesn't
301  happen, connect the cable between P9_32 (VADC) and P9_33 (AIN4).)
302  After a short while (less than 1 second) a red and a white line show
303  up in the the window. White = AIN4 (open connector) starts on the
304  left side at the middle of the window hight (the trigger event) and
305  shows any signal. Red = AIN7 (board voltage = 1.65 V) is always at
306  the top of the window. The menu showns up again for the next test.
307 
308 <b>3 = analog pre-trigger (any AIN < 0.9 V)</b>
309 -# Connect a cable between P9_32 (VADC) and P9_33 (AIN4).
310 -# Press key 3 to start the trigger. You'll see a new message `waiting
311  for analog pre-trigger (any AIN < 0.9 V) ...`. Nothing else happens.
312 -# Release the cable. The open trigger channel AIN4 starts to swing
313  below the trigger voltage. (If this doesn't happen, connect the cable
314  between P9_34 (AGND) and P9_33 (AIN4).) After a short while (less
315  than 1 second) a red and a white line show up in the the window.
316  White = AIN4 (open connector or AGND) goes across the middle of the
317  window (the trigger event) and shows any signal in the upper left
318  (pre-trigger samples) and some parts of the curve in the lower right
319  (regular samples after trigger event). Red = AIN7 (board voltage =
320  1.65 V) is always at the top of the window. The menu showns up again
321  for the next test.