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