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

Committer:
whismanoid
Date:
Fri Jun 07 21:47:07 2019 +0000
Revision:
5:4836afabd85b
Parent:
1:3908aba2ea77
TODO: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whismanoid 1:3908aba2ea77 1 /*******************************************************************************
whismanoid 1:3908aba2ea77 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
whismanoid 1:3908aba2ea77 3 *
whismanoid 1:3908aba2ea77 4 * Permission is hereby granted, free of charge, to any person obtaining a
whismanoid 1:3908aba2ea77 5 * copy of this software and associated documentation files (the "Software"),
whismanoid 1:3908aba2ea77 6 * to deal in the Software without restriction, including without limitation
whismanoid 1:3908aba2ea77 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
whismanoid 1:3908aba2ea77 8 * and/or sell copies of the Software, and to permit persons to whom the
whismanoid 1:3908aba2ea77 9 * Software is furnished to do so, subject to the following conditions:
whismanoid 1:3908aba2ea77 10 *
whismanoid 1:3908aba2ea77 11 * The above copyright notice and this permission notice shall be included
whismanoid 1:3908aba2ea77 12 * in all copies or substantial portions of the Software.
whismanoid 1:3908aba2ea77 13 *
whismanoid 1:3908aba2ea77 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
whismanoid 1:3908aba2ea77 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
whismanoid 1:3908aba2ea77 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
whismanoid 1:3908aba2ea77 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
whismanoid 1:3908aba2ea77 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
whismanoid 1:3908aba2ea77 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
whismanoid 1:3908aba2ea77 20 * OTHER DEALINGS IN THE SOFTWARE.
whismanoid 1:3908aba2ea77 21 *
whismanoid 1:3908aba2ea77 22 * Except as contained in this notice, the name of Maxim Integrated
whismanoid 1:3908aba2ea77 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
whismanoid 1:3908aba2ea77 24 * Products, Inc. Branding Policy.
whismanoid 1:3908aba2ea77 25 *
whismanoid 1:3908aba2ea77 26 * The mere transfer of this software does not imply any licenses
whismanoid 1:3908aba2ea77 27 * of trade secrets, proprietary technology, copyrights, patents,
whismanoid 1:3908aba2ea77 28 * trademarks, maskwork rights, or any other form of intellectual
whismanoid 1:3908aba2ea77 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
whismanoid 1:3908aba2ea77 30 * ownership rights.
whismanoid 1:3908aba2ea77 31 *******************************************************************************
whismanoid 1:3908aba2ea77 32 */
whismanoid 1:3908aba2ea77 33
whismanoid 1:3908aba2ea77 34 #include "MAX541.h"
whismanoid 1:3908aba2ea77 35
whismanoid 1:3908aba2ea77 36 MAX541::MAX541(SPI &spi, DigitalOut &cs_pin)
whismanoid 1:3908aba2ea77 37 : m_spi(spi), m_cs_pin(cs_pin) // SPI interface
whismanoid 1:3908aba2ea77 38 {
whismanoid 1:3908aba2ea77 39 VRef = 3.3f;
whismanoid 1:3908aba2ea77 40 m_cs_pin = 1;
whismanoid 1:3908aba2ea77 41
whismanoid 1:3908aba2ea77 42 m_SPI_dataMode = 0; // SPI_MODE0: CPOL=0,CPHA=0: Rising Edge stable; SCLK idle Low
whismanoid 1:3908aba2ea77 43 m_spi.format(8,m_SPI_dataMode); // int bits_must_be_8, int mode=0_3 CPOL=0,CPHA=0
whismanoid 1:3908aba2ea77 44 m_SPI_SCLK_Hz = 4000000; // 4MHz
whismanoid 1:3908aba2ea77 45 m_spi.frequency(m_SPI_SCLK_Hz);
whismanoid 1:3908aba2ea77 46 }
whismanoid 1:3908aba2ea77 47
whismanoid 1:3908aba2ea77 48 MAX541::~MAX541()
whismanoid 1:3908aba2ea77 49 {
whismanoid 1:3908aba2ea77 50 }
whismanoid 1:3908aba2ea77 51
whismanoid 1:3908aba2ea77 52 void MAX541::Set_Voltage(double voltageV)
whismanoid 1:3908aba2ea77 53 {
whismanoid 1:3908aba2ea77 54 if (voltageV >= VRef) {
whismanoid 1:3908aba2ea77 55 Set_Code(max541codeFS);
whismanoid 1:3908aba2ea77 56 } else if (voltageV <= 0.0f) {
whismanoid 1:3908aba2ea77 57 Set_Code(0);
whismanoid 1:3908aba2ea77 58 } else {
whismanoid 1:3908aba2ea77 59 Set_Code(voltageV / VRef * max541codeFS);
whismanoid 1:3908aba2ea77 60 }
whismanoid 1:3908aba2ea77 61 }
whismanoid 1:3908aba2ea77 62
whismanoid 1:3908aba2ea77 63 double MAX541::Get_Voltage() const
whismanoid 1:3908aba2ea77 64 {
whismanoid 1:3908aba2ea77 65 return (VRef * max541_code) / max541codeFS;
whismanoid 1:3908aba2ea77 66 }
whismanoid 1:3908aba2ea77 67
whismanoid 1:3908aba2ea77 68 void MAX541::Set_Code(int16_t max541_mosiData16)
whismanoid 1:3908aba2ea77 69 {
whismanoid 1:3908aba2ea77 70 max541_code = (uint16_t)max541_mosiData16;
whismanoid 1:3908aba2ea77 71 size_t max541_byteCount = 2;
whismanoid 1:3908aba2ea77 72 static char max541_mosiData[2];
whismanoid 1:3908aba2ea77 73 static char max541_misoData[2];
whismanoid 1:3908aba2ea77 74 max541_mosiData[0] = (char)((max541_mosiData16 >> 8) & 0xFF); // MSByte
whismanoid 1:3908aba2ea77 75 max541_mosiData[1] = (char)((max541_mosiData16 >> 0) & 0xFF); // LSByte
whismanoid 1:3908aba2ea77 76 m_cs_pin = 0;
whismanoid 1:3908aba2ea77 77 m_spi.write(max541_mosiData, max541_byteCount, max541_misoData, max541_byteCount);
whismanoid 1:3908aba2ea77 78 m_cs_pin = 1;
whismanoid 1:3908aba2ea77 79 }
whismanoid 1:3908aba2ea77 80
whismanoid 1:3908aba2ea77 81 uint16_t MAX541::Get_Code(void) const
whismanoid 1:3908aba2ea77 82 {
whismanoid 1:3908aba2ea77 83 return max541_code;
whismanoid 1:3908aba2ea77 84 }