libpruio  0.2
Input/Output driver for digital/analog lines on Beagleboard hardware
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pwm_cap.bas
Go to the documentation of this file.
1 /'* \file pwm_cap.bas
2 \brief Example: PWM output and CAP input.
3 
4 This file contains an example on how to measure the frequency and duty
5 cycle of a pulse train with a eCAP module input. The program sets
6 another pin as eHRPWM output to generate a pulse width modulated signal
7 as source for the measurement. The output can be changed by some keys,
8 the frequency and duty cycle of the input is shown continously in the
9 terminal output.
10 
11 Licence: GPLv3
12 
13 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
14 
15 
16 Compile by: `fbc -w all pwm_cap.bas`
17 
18 '/
19 
20 ' include libpruio
21 #INCLUDE ONCE "../pruio/pruio.bi"
22 ' include the convenience macros for header pins
23 #INCLUDE ONCE "../pruio/pruio_pins.bi"
24 
25 '* The pin for PWM output.
26 #DEFINE P_OUT P9_21
27 '* The pin for CAP input.
28 #DEFINE P_IN P9_42
29 
30 VAR io = NEW PruIo '*< Create a PruIo structure, wakeup subsystems.
31 
32 WITH *io
33  DO
34  IF .Errr THEN ?"NEW failed: " & *.Errr : EXIT DO
35 
36  IF .Cap->config(P_IN, 2.) THEN _ ' configure input pin
37  ?"failed setting input @P_IN (" & *.Errr & ")" : EXIT DO
38 
39  DIM AS Float_t _
40  f1 _ '*< Variable for calculated frequency.
41  , d1 _ '*< Variable for calculated duty cycle.
42  , f0 = 31250 _ '*< The required frequency.
43  , d0 = .5 '*< The required duty cycle.
44  IF .Pwm->setValue(P_OUT, f0, d0) THEN _
45  ?"failed setting output @P_OUT (" & *.Errr & ")" : EXIT DO
46 
47  IF .config(1, 2) THEN _ ' upload configuration to PRU
48  ?"config failed: " & *.Errr : EXIT DO
49 
50  WHILE 1
51  VAR k = ASC(INKEY()) '*< The key code.
52  IF k THEN
53  SELECT CASE AS CONST k ' react on user keystrokes
54  CASE ASC("0") : d0 = 0.0
55  CASE ASC("1") : d0 = 0.1
56  CASE ASC("2") : d0 = 0.2
57  CASE ASC("3") : d0 = 0.3
58  CASE ASC("4") : d0 = 0.4
59  CASE ASC("5") : d0 = 0.5
60  CASE ASC("6") : d0 = 0.6
61  CASE ASC("7") : d0 = 0.7
62  CASE ASC("8") : d0 = 0.8
63  CASE ASC("9") : d0 = 0.9
64  CASE ASC(",") : d0 = 1.0
65  CASE ASC("*") : f0 = IIF(f0 < 1000000, f0 * 2, 1000000.)
66  CASE ASC("/") : f0 = IIF(f0 > .5, f0 / 2, .5)
67  CASE ASC("m") : f0 = IIF(f0 > 5.5, f0 - 5., .5)
68  CASE ASC("p") : f0 = IIF(f0 < 999995., f0 + 5., 1000000.)
69  CASE ASC("+") : f0 = 1000000
70  CASE ASC("-") : f0 = .5
71  CASE ELSE : EXIT WHILE ' finish
72  END SELECT
73 
74  IF .Pwm->setValue(P_OUT, f0, d0) THEN _ ' set new output
75  ?"failed setting PWM output (" & *.Errr & ")" : EXIT WHILE
76 
77  ?!"\n--> " & LEFT("Frequency: " & f0 & SPACE(10), 20) _
78  & LEFT(", Duty: " & d0 & SPACE(10), 20) ' user info
79  END IF
80 
81  IF .Cap->Value(P_IN, @f1, @d1) THEN _ ' get current input
82  ?"failed reading input @P_IN (" & *.Errr & ")" : EXIT WHILE
83 
84  ?!"\r " & LEFT("Frequency: " & f1 & SPACE(10), 20) _
85  & LEFT(", Duty: " & d1 & SPACE(10), 20); ' user info
86  SLEEP 1
87  WEND : ?
88  LOOP UNTIL 1
89 END WITH
90 
91 DELETE(io)
92 
93 '' help Doxygen to dokument the main code
94 '&/** The main function. */
95 '&int main() {PruIo::PruIo(); CapMod::config(); PruIo::config(); CapMod::Value(); PwmMod::setValue(); PruIo::~PruIo();}
96