| 1 | package VIC::PIC::Functions::ADC; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 31 | our $VERSION = '0.29'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | 31 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 31 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | sub adc_enable { | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
adc_enable: called CC=14 SCAR=31.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | return unless $self->doesroles(qw(GPIO ADC Chip CodeGen)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | unless (defined $self->adc_channels and $self->adc_channels > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->adc_channels and $self->adc_channels > 0
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels
$self->adc_channels > 0
Truth table33%1/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | carp $self->type, " has no ADC"; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | unless (exists $self->registers->{ADCON0} and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADCON1"}
MC/DC0%0/2
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADCON1"}
exists $self->registers->{"ADCON0"}
exists $self->registers->{"ADCON1"}
Truth table33%1/3
A and B
A= exists $self->registers->{"ADCON0"}B= exists $self->registers->{"ADCON1"}
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | exists $self->registers->{ADCON1}) { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | carp $self->type, " has no valid ADCON0/ADCON1 registers"; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | my ($clock, $channel) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | my $f_osc = $self->f_osc; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | ▶ | my $scale = POSIX::ceil(($f_osc / 4) / $clock) if $clock > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | $scale = 2 unless $clock; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ▶ | $scale = 2 if $scale < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | my $adcs = $self->adcs_bits->{$scale}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | $adcs = $self->adcs_bits->{internal} if $self->code_config->{adc}->{internal}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | my $adcon1 = "0$adcs" . '0000'; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | my $code = << "..."; | |||||||||||||||||||||||||||||||||||||||||
| 32 | \tbanksel ADCON1 | ||||||||||||||||||||||||||||||||||||||||||
| 33 | \tmovlw B'$adcon1' | ||||||||||||||||||||||||||||||||||||||||||
| 34 | \tmovwf ADCON1 | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | if (defined $channel) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | my $adfm = defined $self->code_config->{adc}->{right_justify} ? | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | $self->code_config->{adc}->{right_justify} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | my $vcfg = $self->code_config->{adc}->{vref} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->code_config->{'adc'}{'vref'} || 0
MC/DC0%0/1
$self->code_config->{'adc'}{'vref'} || 0
$self->code_config->{'adc'}{'vref'}
Truth table50%1/2
A
A= $self->code_config->{'adc'}{'vref'}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | my $chs = $self->adc_chs_bits->{$channel}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | my $adcon0 = "$adfm$vcfg$chs" . '01'; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | $code .= << "..."; | |||||||||||||||||||||||||||||||||||||||||
| 43 | \tbanksel ADCON0 | ||||||||||||||||||||||||||||||||||||||||||
| 44 | \tmovlw B'$adcon0' | ||||||||||||||||||||||||||||||||||||||||||
| 45 | \tmovwf ADCON0 | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # no arguments have been given | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return << "..."; | |||||||||||||||||||||||||||||||||||||||||
| 52 | \tbanksel ADCON0 | ||||||||||||||||||||||||||||||||||||||||||
| 53 | \tbsf ADCON0, ADON | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub adc_disable { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
adc_disable: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return unless $self->doesroles(qw(ADC)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | unless (defined $self->adc_channels and $self->adc_channels > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->adc_channels and $self->adc_channels > 0
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels
$self->adc_channels > 0
Truth table0%0/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | carp $self->type, " has no ADC"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | unless (exists $self->registers->{ADCON0}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | carp $self->type, " has no valid ADCON0/ADCON1 registers"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return << "..."; | |||||||||||||||||||||||||||||||||||||||||
| 69 | \tbanksel ADCON0 | ||||||||||||||||||||||||||||||||||||||||||
| 70 | \tbcf ADCON0, ADON | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | sub adc_read { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | #TODO: if the variable is 16-bit the varlow should be auto-adjusted | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | my ($self, $varhigh, $varlow) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
adc_read: called CC=9 SCAR=28.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | return unless $self->doesroles(qw(ADC GPIO Chip)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | unless (defined $self->adc_channels and $self->adc_channels > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->adc_channels and $self->adc_channels > 0
MC/DC0%0/2
defined $self->adc_channels and $self->adc_channels > 0
defined $self->adc_channels
$self->adc_channels > 0
Truth table33%1/3
A and B
A= defined $self->adc_channelsB= $self->adc_channels > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | carp $self->type, " has no ADC"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | $varhigh = uc $varhigh; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | $varlow = uc $varlow if defined $varlow; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | unless (exists $self->registers->{ADCON0} and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"} and exists $self->registers->{"ADRESL"}
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"}
MC/DC0%0/3
exists $self->registers->{"ADCON0"} and exists $self->registers->{"ADRESH"} and exists $self->registers->{"ADRESL"}
exists $self->registers->{"ADCON0"}
exists $self->registers->{"ADRESH"}
exists $self->registers->{"ADRESL"}
Truth table25%1/4
A and B and C
A= exists $self->registers->{"ADCON0"}B= exists $self->registers->{"ADRESH"}C= exists $self->registers->{"ADRESL"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | exists $self->registers->{ADRESH} and | ||||||||||||||||||||||||||||||||||||||||||
| 86 | exists $self->registers->{ADRESL}) { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | carp $self->type, " has no valid ADCON0/ADRESH/ADRESL registers"; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | my $code = << "..."; | |||||||||||||||||||||||||||||||||||||||||
| 91 | \t;;;delay 5us | ||||||||||||||||||||||||||||||||||||||||||
| 92 | \tnop | ||||||||||||||||||||||||||||||||||||||||||
| 93 | \tnop | ||||||||||||||||||||||||||||||||||||||||||
| 94 | \tnop | ||||||||||||||||||||||||||||||||||||||||||
| 95 | \tnop | ||||||||||||||||||||||||||||||||||||||||||
| 96 | \tnop | ||||||||||||||||||||||||||||||||||||||||||
| 97 | \tbsf ADCON0, GO | ||||||||||||||||||||||||||||||||||||||||||
| 98 | \tbtfss ADCON0, GO | ||||||||||||||||||||||||||||||||||||||||||
| 99 | \tgoto \$ - 1 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | \tmovf ADRESH, W | ||||||||||||||||||||||||||||||||||||||||||
| 101 | \tmovwf $varhigh | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | $code .= "\tmovf ADRESL, W\n\tmovwf $varlow\n" if defined $varlow; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 31 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||||||||||||||||