## HiPi::Interface::MCP49XX

This module provides an interface to the MCP49XX and MCP48XX series of digital to analog converters with SPI interface.

The interface should work for any of the MCP4801, MCP4811, MCP4821, MCP4802, MCP4812, MCP4822, MCP4901, MCP4911, MCP4921, MCP4902, MCP4912, and MCP4922 converters according to their datasheets. Code actually tested with a 4902 dual channel converter.

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

## Object Constructor and Methods

##### my $dac = HiPi::Interface::MCP49XX->new( %params );

        Returns a new instance of the HiPi::Interface::MCP49XX class.

        required key => value pair in %params and its default

        type  =>   MCP4902

        the type parameter accepts one of the constants below that defines
        which type of converter you have

        MCP4801 MCP4811 MCP4821 MCP4802 MCP4812 MCP4822
        MCP4901 MCP4911 MCP4921 MCP4902 MCP4912 MCP4922

        you can import these constants into your namespace using:

        use HiPi::Interface::MCP49XX qw( :mcp );

        optional key => value pairs that affect the converter

        gain       =>   0,
        buffer     =>   0,
        shiftvalue =>   0,

        gain -- boolean flag -- some of the converters supported allow the
        option of multiplying the output voltage to 2x the proportion of the
        reference voltage specified by your value setting.
        e.g. If you have a 12 bit resolution convertor (max value 4095) and you
        specify the max value ( 4095 ), if the reference voltage is 2V5 then
        the output voltage will be 5V0.
        Set gain => 1 to switch this voltage doubling on.

        buffer -- boolean flag determining  whether to set the input buffer
        control bit on writes. Specified in the MCP4XXX data sheets, I've no
        idea what it does. Leaving at 0 ( false ) works for me.

        shiftvalue -- boolean flag -- by default ( shiftvalue == 0 ) whatever
        the resolution of your particular converter, you should specify values
        between 1 and 4095 when writing to the device. This means that all the
        supported devices are interchangable and you don't have to alter your
        code to suit the different device resolutions.

        If, however, you want to specify values in terms of the specific
        resolution of your converter
        i.e.  for an 8 bit converter you want to specify values  0 - 255
              for a 10 bit converter you want to specify values  0 - 1023
              for a 12 bit converter you want to specify values  0 - 4095
        then you should set shiftvalue => 1

        optional key => values pairs that are passed to HiPi::Device::SPI

        devicename   => '/dev/spidev0.0',
        speed        => SPI_SPEED_MHZ_1,
        bitsperword  => 8,
        delay        => 0,

        You probably want to specify the devicename explicitly depending
        on which cable select pin you have connected the MCP4XXX

        For SPI0_CEO_N
        my $adc = HiPi::Interface::MCP49XX->new(
            devicename   => '/dev/spidev0.0',
        );

        For SPI0_CE1_N
        my $adc = HiPi::Interface::MCP49XX->new(
            devicename   => '/dev/spidev0.1',
        );

        See  Raspberry Pi GPIO Pin layout for pin locations.

##### $dac->write( $value, $channel );

        $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.
        i.e.  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

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

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

##### $dac->shutdown( $channel );

        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->shiftvalue( $optionalnewval );

        get/set  shiftvalue flag true/false

##### $dac->gain( $optionalnewval );

        get/set  gain  flag true/false

##### $dac->buffer( $optionalnewval );

        get/set  buffer flag true/false

HiPi Modules Copyright © 2013 Mark Dootson