libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
qep.bas
Go to the documentation of this file.
1 
16 
17 ' include libpruio
18 #INCLUDE ONCE "BBB/pruio.bi"
19 ' include the convenience macros for header pins
20 #INCLUDE ONCE "BBB/pruio_pins.bi"
21 
22 
23 #DEFINE PMX 4095
24 
25 #DEFINE VHz 25
26 
27 DIM AS CONST UInt8 PINS(...) = {P8_12, P8_11, P8_16}
28 
29 VAR io = NEW PruIo
30 
31 WITH *io
32  DO
33  IF .Errr THEN ?"NEW failed: " & *.Errr : EXIT DO
34 
35  '' configure PWM-1 for symetric output duty 50% and phase shift 1 / 4
36  '.Pwm->ForceUpDown = 1
37  .Pwm->ForceUpDown OR= 1 SHL 1
38  .Pwm->AqCtl(0, 1, 1) = &b000000000110
39  .Pwm->AqCtl(1, 1, 1) = &b011000000000
40 
41  DIM AS Float_t _
42  freq = 50. _
43  , realfreq
44  IF .Pwm->setValue(P9_14, freq, .0) THEN _
45  ?"failed setting P9_14 (" & *.Errr & ")" : EXIT DO
46 
47  IF .Pwm->setValue(P9_16, freq, .25) THEN _
48  ?"failed setting P9_16 (" & *.Errr & ")" : EXIT DO
49 
50  IF .Pwm->setValue(P9_42, .5, .00000005) THEN _
51  ?"failed setting P9_42 (" & *.Errr & ")" : EXIT DO
52 
53  IF .Pwm->Value(P9_14, @realfreq, NULL) THEN _
54  ?"failed getting PWM value (" & *.Errr & ")" : EXIT DO
55 
56  DIM AS UInt32 pmax = PMX
57  IF .Qep->config(PINS(0), pmax, VHz) THEN _ ' configure QEP pins
58  ?"QEP pin configuration failed (" & *.Errr & ")" : EXIT DO
59 
60  IF .config(1, 0) THEN _ ' configure PRU
61  ?"config failed: " & *.Errr & " --> " & .DRam[0] : SLEEP : EXIT DO
62 
63  STATIC AS CONST ZSTRING PTR t(...) = {@" A", @" A & B", @"A, B & I"}
64  DIM AS UInt32 _
65  posi _
66  , m = -1 _
67  , p = 0
68  DIM AS Float_t velo
69  ?!"\n" & *t(p) & " input, " & freq & "Hz (" & realfreq & "), PMax=" & pmax
70  DO ' print current state (until keystroke)
71  VAR k = ASC(INKEY())
72  IF k THEN
73  SELECT CASE AS CONST k ' react on user keystrokes
74  CASE ASC("a"), ASC("A") : m = 0
75  CASE ASC("b"), ASC("B") : m = 1
76  CASE ASC("i"), ASC("I") : m = 2
77  CASE ASC("0") : m = p : pmax = 0
78  CASE ASC("1") : m = p : pmax = 1023
79  CASE ASC("4") : m = p : pmax = 4095
80  CASE ASC("5") : m = p : pmax = 511
81  CASE ASC("8") : m = p : pmax = 8191
82  CASE ASC("+") : m = 3 : .Pwm->AqCtl(0, 1, 1) = &b000000000110
83  CASE ASC("-") : m = 3 : .Pwm->AqCtl(0, 1, 1) = &b000000001001
84  CASE ASC("p"), ASC("P") : m = 3 : IF freq < 499995 THEN freq += 5 ELSE freq = 500000
85  CASE ASC("m"), ASC("M") : m = 3 : IF freq > 20 THEN freq -= 5 ELSE freq = 25
86  CASE ASC("*") : m = 3 : IF freq < 250000 THEN freq *= 2 ELSE freq = 500000
87  CASE ASC("/") : m = 3 : IF freq > 50 THEN freq /= 2 ELSE freq = 25
88  CASE 13 : m = 1 : freq = 50
89  IF .Pwm->setValue(P9_14, freq, -1) THEN _
90  ?"failed setting PWM value (" & *.Errr & ")" : EXIT DO
91  IF .Pwm->Value(P9_14, @realfreq, NULL) THEN _
92  ?"failed getting PWM value (" & *.Errr & ")" : EXIT DO
93  CASE ELSE : EXIT DO ' finish
94  END SELECT
95  SELECT CASE AS CONST m
96  CASE 3
97  IF .Pwm->setValue(P9_14, freq, -1) THEN _
98  ?"failed setting PWM value (" & *.Errr & ")" : EXIT DO
99  IF .Pwm->Value(P9_14, @realfreq, NULL) THEN _
100  ?"failed getting PWM value (" & *.Errr & ")" : EXIT DO
101  CASE ELSE
102  p = m
103  IF .Qep->config(PINS(p), pmax, VHz) THEN _ 'reconfigure QEP pins
104  ?"QEP pin reconfiguration failed (" & *.Errr & ")" : EXIT DO
105  END SELECT
106  ?!"\n" & *t(p) & " input, " & freq & "Hz (" & realfreq & "), PMax=" & pmax
107  END IF
108  IF .Qep->Value(PINS(p), @posi, @velo) THEN _
109  ?"Qep->Value failed (" & *.Errr & ")" : EXIT DO
110  ?!"\r" & HEX(posi, 8), velo & " ";
111  SLEEP 20
112  LOOP : ?
113  LOOP UNTIL 1
114  IF .Errr THEN SLEEP
115 END WITH
116 
117 DELETE(io)
118 
119 '' help Doxygen to document the main code
120 '&/** The main function. */
121 '&int main() {PruIo::PruIo(); PwmMod::setValue(); PwmMod::Value(); QepMod::config(); PruIo::config(); QepMod::Value(); PruIo::~PruIo();}
122