Maxim Integrated 16-Bit, Unbuffered Output Voltage DAC. Driver/library for MAX541

Dependents:   MAX541_Hello_DAC_on_MAX32625MBED MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester ... more

MAX541.h

Committer:
whismanoid
Date:
2019-04-22
Revision:
0:4847beac14e9
Child:
1:3908aba2ea77

File content as of revision 0:4847beac14e9:

/*******************************************************************************
* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************
*/

class MAX541 {
public:
    MAX541(SPI &spi, DigitalOut &cs_pin)
        : m_spi(spi), m_cs_pin(cs_pin) // SPI interface
    {
        VRef = 3.3f;
        m_cs_pin = 1;

        m_SPI_dataMode = 0; // SPI_MODE0: CPOL=0,CPHA=0: Rising Edge stable; SCLK idle Low
        m_spi.format(8,m_SPI_dataMode);         // int bits_must_be_8, int mode=0_3 CPOL=0,CPHA=0
        m_SPI_SCLK_Hz = 4000000; // 4MHz
        m_spi.frequency(m_SPI_SCLK_Hz);
    }
    ~MAX541()
    {
    }

private:
    // SPI object
    SPI &m_spi;
    int m_SPI_SCLK_Hz;
    int m_SPI_dataMode;

    // Selector pin object
    DigitalOut &m_cs_pin;

public:
    double VRef;
    const float max541codeFS = (float)((uint32_t)0xffff);
    uint16_t max541_code;

    // set the MAX541 output voltage
    void Set_Voltage(double voltageV)
    {
        if (voltageV >= VRef) {
            Set_Code(max541codeFS);
        }
        else if (voltageV <= 0.0f) {
            Set_Code(0);
        }
        else {
            Set_Code(voltageV / VRef * max541codeFS);
        }
    }

    // get the MAX541 output voltage
    double Get_Voltage() const
    {
        return (VRef * max541_code) / max541codeFS;
    }

    // set the MAX541 output code
    void Set_Code(int16_t max541_mosiData16)
    {
        max541_code = (uint16_t)max541_mosiData16;
        size_t max541_byteCount = 2;
        static char max541_mosiData[2];
        static char max541_misoData[2];
        max541_mosiData[0] = (char)((max541_mosiData16 >> 8) & 0xFF); // MSByte
        max541_mosiData[1] = (char)((max541_mosiData16 >> 0) & 0xFF); // LSByte
        m_cs_pin = 0;
        m_spi.write(max541_mosiData, max541_byteCount, max541_misoData, max541_byteCount);
        m_cs_pin = 1;
    }

    // get the MAX541 output code
    uint16_t Get_Code(void) const
    {
        return max541_code;
    }

};