libpruio  0.2
Input/Output driver for digital/analog lines on Beagleboard hardware
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pwm_adc.bas
Go to the documentation of this file.
1 /'* \file pwm_adc.bas
2 \brief Example: generate PWM outputs and fetch them as ADC samples, draw graf.
3 
4 This file contains an example on how to use libpruio to generate pulse
5 width modulated (PWM) output. Here the two channels (A + B) of an
6 eHRPWM module and a eCAP module (in PWM) mode generate the PWM signal.
7 The output can get measured by the ADC subsystem at channels AIN-0 to
8 AIN-2 and shown as a line graf in a graphics windows.
9 
10 Licence: GPLv3
11 
12 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
13 
14 
15 Compile by: `fbc -w all pwm_adc.bas`
16 
17 '/
18 
19 ' include libpruio
20 #INCLUDE ONCE "../pruio/pruio.bi"
21 ' include the convenience macros for header pins
22 #INCLUDE ONCE "../pruio/pruio_pins.bi"
23 ' include FB grafics
24 #INCLUDE ONCE "fbgfx.bi"
25 
26 VAR S_W = 0 _ '*< The screen width.
27  , S_H = 0 _ '*< The srceen hight
28  , BPP = 0 _ '*< The bits per plain number.
29  , full = fb.GFX_FULLSCREEN '*< Fullscreen or windowed mode.
30 SCREENINFO S_W, S_H, BPP ' get screen resolution
31 
32 IF LEN(COMMAND) THEN ' customized resolution required?
33  VAR p = INSTR(COMMAND, "x") _ '*< The position of the 'x' character (if any).
34  , w = VALINT(COMMAND) _ '*< The required window width.
35  , h = VALINT(MID(COMMAND, p + 1)) '*< The required window hight.
36  IF p ANDALSO w ANDALSO h THEN
37  IF w < S_W - 4 ANDALSO h < S_H - 24 THEN full = fb.GFX_WINDOWED
38  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
39  S_H = IIF(h < S_H, h, S_H)
40  ELSE
41  PRINT "set resolution like 640x400"
42  END
43  END IF
44 END IF
45 
46 SCREENRES S_W, S_H, BPP, 2, full ' set screen resolution
47 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END
48 
49 '* The colors for the lines (= channels).
50 DIM AS UInt32 _
51  col(...) = { _
52  RGBA(255, 0, 0, 255) _
53  , RGBA( 0, 255, 0, 255) _
54  , RGBA( 0, 0, 255, 255) _
55  }
56 
57 '* The previous data of the channels.
58 DIM AS UInt32 _
59  last(...) = { _
60  0 _
61  , 0 _
62  , 0 _
63  , 0 _
64  , 0 _
65  , 0 _
66  , 0 _
67  , 0 _
68  }
69 
70 '* UDT to hold data for an output channel
71 TYPE PWM_PIN
72  AS UInt8 _
73  Pin '*< The ball number.
74  AS Float_t _
75  Freq _ '*< The frequency to set.
76  , Duty '*< The duty cycle.
77  AS ZSTRING PTR _
78  Nam '*< The name of the header pin.
79 END TYPE
80 
81 '* The structures for the output pins.
82 DIM AS PWM_PIN ch(...) = { _
83  TYPE<PWM_PIN>(P9_14, 2.5, .5, @"P9_14 (A)") _
84  , TYPE<PWM_PIN>(P9_16, 2.5, .2, @"P9_16 (B)") _
85  , TYPE<PWM_PIN>(P9_42, 2.5, .8, @"P9_42 (C)") _
86  }
87 
88 VAR io = NEW PruIo '*< Create a PruIo structure, wakeup subsystems.
89 
90 WITH *io
91  DO
92  IF .Errr THEN ?"NEW failed: " & *.Errr : EXIT DO
93 
94  FOR i AS INTEGER = 0 TO UBOUND(ch) ' configure PWM pins
95  IF .Pwm->setValue(ch(i).Pin, ch(i).Freq, ch(i).Duty) THEN _
96  ?"failed setting " & *ch(i).nam & " (" & *.Errr & ")" : EXIT DO
97  NEXT
98 
99  IF .config(1, &b1110) THEN _ ' configure steps 1, 2, 3 (AIN-[0,1,2])
100  ?"config failed: " & *.Errr & " --> " & .DRam[0] : SLEEP : EXIT DO
101 
102  VAR scale = S_H / 65520 _ '*< The factor to scale values.
103  , x = 0 _ '*< The start x position.
104  , gap = 1 _ '*< The gap between x values.
105  , xmax = S_W - gap - 1 _ '*< The maximal x position.
106  , m = 0 _ '*< The mask for in-active channels.
107  , n = 2 _ '*< The active channel number.
108  , fg = RGB(0, 0, 0) _ '*< The foreground color.
109  , bg = RGB(250, 250, 250) '*< The background color.
110  FOR i AS INTEGER = 0 TO UBOUND(ch) ' get start values
111  last(i) = S_H - CUINT(.Adc->Value[i + 1] * scale)
112  NEXT
113 
114  WINDOWTITLE(*ch(n).nam _
115  & ": Frequency = " & ch(n).Freq & " Hz" _
116  & ", Duty = " & (ch(n).Duty) * 100) & "%"
117  COLOR fg, bg
118  CLS
119  DO
120  VAR k = ASC(INKEY()) '*< The key code.
121  IF k THEN
122  SELECT CASE AS CONST k ' react on user keystrokes
123  CASE ASC("A"), ASC("a") : m = BITRESET(&hFF, 0): n = 256 'PWMSS 1, output PWM-A (P9_14)
124  CASE ASC("B"), ASC("b") : m = BITRESET(&hFF, 1): n = 257 'PWMSS 1, output PWM-B (P9_16)
125  CASE ASC("C"), ASC("c") : m = BITRESET(&hFF, 2): n = 258 'PWMSS 0, output eCAP (P9_42)
126  CASE ASC("0") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.0
127  CASE ASC("1") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.1
128  CASE ASC("2") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.2
129  CASE ASC("3") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.3
130  CASE ASC("4") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.4
131  CASE ASC("5") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.5
132  CASE ASC("6") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.6
133  CASE ASC("7") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.7
134  CASE ASC("8") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.8
135  CASE ASC("9") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.9
136  CASE ASC(",") : m = BITRESET(&hFF, n) : ch(n).Duty = 1.0
137  CASE ASC("+") : IF ch(n).Freq < 5.0 THEN ch(n).Freq += .5
138  CASE ASC("-") : IF ch(n).Freq > 0.9 THEN ch(n).Freq -= .5
139  CASE ASC("*") : ch(n).Freq = 5.0
140  CASE ASC("/") : ch(n).Freq = .5
141  CASE 13 : m = 0 : n += 256
142  CASE ELSE : EXIT DO ' finish
143  END SELECT
144 
145  IF n > UBOUND(ch) THEN
146  n -= 256
147  ELSE
148  IF .Pwm->setValue(ch(n).Pin, ch(n).Freq, ch(n).Duty) THEN _
149  ?"failed setting PWM value (" & *.Errr & ")" : EXIT DO
150  END IF
151 
152  WINDOWTITLE(*ch(n).nam _
153  & ": Frequency = " & ch(n).Freq & " Hz" _
154  & ", Duty = " & (ch(n).Duty) * 100) & "%"
155  END IF
156 
157  LINE (x + 1, 0) - STEP (gap, S_H), bg, BF
158  FOR i AS INTEGER = 0 TO UBOUND(ch) ' draw lines
159  IF BIT(m, i) THEN CONTINUE FOR
160  VAR neu = S_H - CUINT(.Adc->Value[i + 1] * scale) '*< The new value.
161  LINE (x, last(i)) - (x + gap, neu), col(i)
162  last(i) = neu
163  LINE (0, 0) - STEP (gap, S_H), 0, BF
164  NEXT
165  x += gap : IF x > xmax THEN x = 0
166  SLEEP 1
167  LOOP : ?
168  LOOP UNTIL 1
169  IF .Errr THEN SLEEP
170 END WITH
171 
172 DELETE(io)
173 
174 '' help Doxygen to dokument the main code
175 '&/** The main function. */
176 '&int main() {PruIo::PruIo(); PwmMod::setValue(); PruIo::config(); PruIo::~PruIo();}
177