blib/lib/PINE64/MCP3208.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 #!/usr/bin/perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use PINE64::GPIO;
Subroutine BEGIN: called CC=1 SCAR=2.6
5  
6 package PINE64::MCP3208;
7  
8 our $VERSION = '0.9';
9  
10 =pod
11 routines for MCP3208 8-channel
12 12-bit SPI analog to digital converter.
13 Implemented as bit-banged SPI using
14 any GPIO pins you wish.
15 =cut
16  
17 #SPI / bitbang varables
18 my ($clk, $din, $dout, $chpsel);
19  
20 #channels: first bit is set to single, not differential
21 #5 bits because the first is the start bit
22 my @ch0 = (1,1,0,0,0);
23 my @ch1 = (1,1,0,0,1);
24 my @ch2 = (1,1,0,1,0);
25 my @ch3 = (1,1,0,1,1);
26 my @ch4 = (1,1,1,0,0);
27 my @ch5 = (1,1,1,0,1);
28 my @ch6 = (1,1,1,1,0);
29 my @ch7 = (1,1,1,1,1);
30  
31 #instantiate PINE64 gpio device
32 my $p64 = PINE64::GPIO->new();
33  
34 sub new{
35 my $class = shift;
36 my $self = bless {}, $class;
37  
38 #
39 $clk = $_[0]; #clock
40 $din = $_[1]; #instructions to adc
41 $dout = $_[2]; #10-bit output from adc
42 $chpsel = $_[3]; #latch / load to init comm to adc
43  
44 #init gpio lines
45 $p64->gpio_enable($clk, 'out');
46 $p64->gpio_enable($din, 'out');
47 $p64->gpio_enable($dout, 'in');
48 $p64->gpio_enable($chpsel, 'out');
49
50 #init chpsel high, comm begins when
51 #chipsel goes from high to low
52 $p64->gpio_write($chpsel, 1);
53  
54 return $self;
55  
56 }#end new
57  
58 sub read3208{
59 #init empty array that will
60 #contain 10-bit reading from ADC
61 my @reading = ();
62 #get reading from the MCP3004 ADC
63 #starts comm when cs goes from high to low
64
65 #ADC channel number
66 #channel is an array reference
67 my $channel = $_[1];
68  
69 #delay between clock pulses
70 #needs to operate at 10KHz so
71 #sample is accurate, so min usleep(100)
72 my $delay = $_[2];
73  
74 #reference voltate, used only for calculations
75 #can be omitted
76 my $vref = $_[3];
77  
78 #main clock variable
79 my $i=0;
80
81 #high flag for data gpio line
82 my $hf = 0;
83  
84 #number of clock pulses,
85 my $ncp = 38; #for 20 clock pulses
86  
87 #high or low state of clock pulse
88 my $state = 0;
89 my $seed = 3; #seed used to determine high or low, start 3 so first pulse is high
90
91 #toggles CS line to init communication with the adc
92 #start low, go high, then low. comm begins when CS
93 #brought from high to low
94 $p64->gpio_write($chpsel, 0);
95 Time::HiRes::usleep($delay);
96
97 #main loop
98 while($i<$ncp){
99 $state = $seed%2;#toggles between 1 and 0
100 $seed++;
101  
102 #clock pulse high
103 if(($i%2) eq 0){
104 #print "i: $i\tcp high\tstate: $state\n";
105  
106 if($channel->[$i/2] eq 1 && $i <=8){
107 $p64->gpio_write($din, 1);
108 $hf = 1;#set high flag
109 #print "ch[".($i/2)."]: ".$channel->[$i/2]."\n";
110 }#end if
111 if($channel->[$i/2] eq 0 && $i <=8){
112 $p64->gpio_write($din,0);
113 }#end if zero on data line
114 }#end if high
115  
116 #if($i>8){#data line
117 #everything after D0 bit is don't care,
118 #set to state
119 #gpio_write($din, $state);
120 #}#end else
121  
122 #clock pulse
123 $p64->gpio_write($clk, $state);
124  
125 #read data out
126 #data clocked out on falling
127 #edge of clk
128 if(($i%2) == 1 && $i >12){
129 #read state of gpio pin connected
130 #to data out of ADC
131 push @reading, $p64->gpio_read($dout);
132 #print "i[$i]: ".$reading[($i-14)/2]."\n"
133 }#end read data out
134  
135 #lower data if high flag set
136 if($hf eq 1){
137 #gpio_write($din,0);
138 $hf = 0;#reset high flag
139 }#end if
140
141 #pause between clk pls
142 Time::HiRes::usleep($delay);
143  
144 #increment counter
145 $i++;
146 }#end while
147
148 #make din low
149 $p64->gpio_write($din, 0);
150  
151 #make chip select high
152 $p64->gpio_write($chpsel, 1);
153  
154 #perform calcuations, return a voltage
155 #based of 12-bit reading, and vref val
156 my $bindig = 2048;
157 my $rdgval = 0;
158 my $voltage = 0;
159 for(my $x=0; $x<12;$x++){
160 if(@reading[$x] == 1){
161 $rdgval = $rdgval + $bindig;
162 }#end if
163 $bindig = $bindig / 2;
164 }#end for
165  
166 #voltage calculation
167 $voltage = ($rdgval*$vref)/4096;
168  
169 return (\@reading, $rdgval, $voltage);
170 }#end read3208
171  
172 1;
173 __END__
↑ Summary