HiPi
Perl Modules for Raspberry Pi
Version 0.92 - released 28 March 2024

HiPi::Interface::MCP4DAC

This module provides an interface to the the MCP 4801, 4802, 4811, 4812, 4821, 4822, 4901, 4902, 4911, 4912, 4921, 4922 digital to analog converters

It uses HiPi::Device::SPI as a backend.

Methods

Create a new instance of the module class.

use HiPi qw( :mcp4dac );
use HiPi::Interface::MCP4DAC;
my $dac = HiPi::Interface::MCP4DAC->new(
    devicename   => '/dev/spidev0.0',
    ic           => MCP4902,
);

devicename gives the SPI device and CS number that the IC is connected to.

ic gives the chip family type. It can be one of the following:

ID Resolution Channels Can Buffer
MCP4801 8 bit 1 No
MCP4802 8 bit 2 No
MCP4811 10 bit 1 No
MCP4812 10 bit 2 No
MCP4821 12 bit 1 No
MCP4822 12 bit 2 No
MCP4901 8 bit 1 Yes
MCP4902 8 bit 2 Yes
MCP4911 10 bit 1 Yes
MCP4912 10 bit 2 Yes
MCP4921 12 bit 1 Yes
MCP4922 12 bit 2 Yes

You can optionally set the following properties at object creation. (default values shown)

use HiPi qw( :mcp4dac );
use HiPi::Interface::MCP4DAC;
my $dac = HiPi::Interface::MCP4DAC->new(
    devicename   => '/dev/spidev0.0',
    ic           => MCP4902,
    gain         => 0,
    shiftvalue   => 0,
    buffer       => 0,
);

$value is the setting you want to apply.

if the current setting of $dac->shiftvalue is false then $value should be a number between 0 and 4095

if the current setting of $dac->shiftvalue is true then the range for $value is determined by the resolution of your converter.

  • for an 8 bit device you must specify values 0 - 255
  • for a 10 bit device you must specify values 0 - 1023
  • for a 12 bit device you must specify values 0 - 4095

$channel is only used when your converter has two channels.
If $channel is 0 or undefined then the write is to channel A
If $channel is any true value and your converter has a second channel then the write is to channel B.

In your code if dealing with a known 2 channel converter you may wish to always be explicit

$dac->write( $aval, 0 );
$dac->write( $bval, 1 );

Code that does not specify a channel always writes to channel A and is portable across all supported converters.

$dac->write( $val );

Flags are included in the write operation according to the current property settings

gain          If 'gain' is true, the gain bit is set in the write message.
buffer          If 'buffer' is true, and your IC can buffer, the buffer bit is set in the write message.

Shutdown the specified channel by passing the flag defined in the converter datasheets.

$channel is 0 or 1 for channel A or B. Defaults to channel A if $channel undefined.

$dac->shutdown( 0 );
$dac->shutdown( 1 );

Get or set the gain property. True / False ( see 'write' method )

my $gain = $dac->gain();
$dac->gain( 0 );
$dac->gain( 1 );

Get or set the buffer property. True / False ( see 'write' method )

my $buffer = $dac->buffer();
$dac->buffer( 0 );
$dac->buffer( 1 );

Get or set the shiftvalue property. True / False ( see 'write' method )

my $shift = $dac->shiftvalue();
$dac->shiftvalue( 0 );
$dac->shiftvalue( 1 );