Maxim Integrated MAX5171 14-bit Force/Sense DAC
Dependents: MAX5171BOB_Tester MAX5171BOB_14bit_Remote_Sense_SPI_DAC MAX5171BOB_Serial_Tester
Diff: MAX5171.cpp
- Revision:
- 3:79839617d532
- Parent:
- 1:e73245718b2a
- Child:
- 4:1984eef51fe3
diff -r d1340b334b0d -r 79839617d532 MAX5171.cpp --- a/MAX5171.cpp Mon Jun 17 05:30:16 2019 +0000 +++ b/MAX5171.cpp Fri Jun 21 08:17:41 2019 +0000 @@ -127,19 +127,19 @@ DigitalIn &UPO_pin, // Digital General-Purpose Output from MAX5171 device // CODE GENERATOR: class constructor definition ic_variant MAX5171_ic_t ic_variant) -// CODE GENERATOR: class constructor initializer list + // CODE GENERATOR: class constructor initializer list : m_spi(spi), m_cs_pin(cs_pin), // SPI interface - // CODE GENERATOR: class constructor initializer list gpio InputPin pins - // m_FB_pin(FB_pin), // Analog Input to MAX5171 device - m_RS_pin(RS_pin), // Digital Configuration Input to MAX5171 device - m_PDLb_pin(PDLb_pin), // Digital Configuration Input to MAX5171 device - m_CLRb_pin(CLRb_pin), // Digital Configuration Input to MAX5171 device - m_SHDN_pin(SHDN_pin), // Digital Configuration Input to MAX5171 device - // CODE GENERATOR: class constructor initializer list gpio OutputPin pins - // m_OUT_pin(OUT_pin), // Analog Output from MAX5171 device - m_UPO_pin(UPO_pin), // Digital General-Purpose Output from MAX5171 device - // CODE GENERATOR: class constructor initializer list ic_variant - m_ic_variant(ic_variant) + // CODE GENERATOR: class constructor initializer list gpio InputPin pins + // m_FB_pin(FB_pin), // Analog Input to MAX5171 device + m_RS_pin(RS_pin), // Digital Configuration Input to MAX5171 device + m_PDLb_pin(PDLb_pin), // Digital Configuration Input to MAX5171 device + m_CLRb_pin(CLRb_pin), // Digital Configuration Input to MAX5171 device + m_SHDN_pin(SHDN_pin), // Digital Configuration Input to MAX5171 device + // CODE GENERATOR: class constructor initializer list gpio OutputPin pins + // m_OUT_pin(OUT_pin), // Analog Output from MAX5171 device + m_UPO_pin(UPO_pin), // Digital General-Purpose Output from MAX5171 device + // CODE GENERATOR: class constructor initializer list ic_variant + m_ic_variant(ic_variant) { // CODE GENERATOR: class constructor definition SPI interface initialization // @@ -246,12 +246,14 @@ cmdLine_microUSBserial.serial().printf(" byteCount:%d", byteCount); } cmdLine_microUSBserial.serial().printf(" MOSI->"); - for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++) { + for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++) + { cmdLine_microUSBserial.serial().printf(" 0x%2.2X", mosiData[byteIndex]); } // hex dump misoData[0..byteCount-1] cmdLine_microUSBserial.serial().printf(" MISO<-"); - for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++) { + for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++) + { cmdLine_microUSBserial.serial().printf(" 0x%2.2X", misoData[byteIndex]); } cmdLine_microUSBserial.serial().printf(" "); @@ -262,12 +264,14 @@ cmdLine_DAPLINKserial.serial().printf(" byteCount:%d", byteCount); } cmdLine_DAPLINKserial.serial().printf(" MOSI->"); - for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++) { + for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++) + { cmdLine_DAPLINKserial.serial().printf(" 0x%2.2X", mosiData[byteIndex]); } // hex dump misoData[0..byteCount-1] cmdLine_DAPLINKserial.serial().printf(" MISO<-"); - for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++) { + for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++) + { cmdLine_DAPLINKserial.serial().printf(" 0x%2.2X", misoData[byteIndex]); } cmdLine_DAPLINKserial.serial().printf(" "); @@ -286,7 +290,11 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::Init(void) { - + + //---------------------------------------- + // Nominal Full-Scale Voltage Reference + VRef = 2.500; + //---------------------------------------- // success return 1; @@ -301,7 +309,7 @@ // @return raw 14-bit MAX5171 code (right justified). uint16_t MAX5171::DACCodeOfVoltage(double voltageV) { - + //---------------------------------------- // Linear map min and max endpoints const double MaxScaleVoltage = VRef; // voltage of maximum code 0x3fff @@ -312,9 +320,11 @@ double codeFraction = (voltageV - MinScaleVoltage) / (MaxScaleVoltage - MinScaleVoltage); double dacRegValueIdeal = ((codeFraction * (double)(MaxCode - MinCode + 1)) + MinCode + 0.5); uint16_t dacRegValue = (uint16_t)dacRegValueIdeal; - if (dacRegValueIdeal > MaxCode) { + if (dacRegValueIdeal > MaxCode) + { dacRegValue = MaxCode; - } else if (dacRegValueIdeal < MinCode) { + } else if (dacRegValueIdeal < MinCode) + { dacRegValue = MinCode; } return dacRegValue; @@ -329,7 +339,7 @@ // @return physical voltage corresponding to MAX5171 code. double MAX5171::VoltageOfCode(uint16_t value_u14) { - + //---------------------------------------- // Linear map min and max endpoints double MaxScaleVoltage = VRef; // voltage of maximum code 0x3fff @@ -348,22 +358,22 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::CODE(uint16_t dacCodeLsbs) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_00dd_dddd_dddd_dddd_CODE - | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd - + | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // shadow of write-only register CMD_00dd_dddd_dddd_dddd_CODE DACCode = dacCodeLsbs; - + //---------------------------------------- // success return 1; @@ -376,22 +386,22 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::CODE_LOAD(uint16_t dacCodeLsbs) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_01dd_dddd_dddd_dddd_CODE_LOAD - | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd - + | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // shadow of write-only register CMD_00dd_dddd_dddd_dddd_CODE DACCode = dacCodeLsbs; - + //---------------------------------------- // success return 1; @@ -405,17 +415,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::LOAD(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_10xx_xxxx_xxxx_xxxx_LOAD; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -428,17 +438,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::NOP(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1100_xxxx_xxxx_xxxx_NOP; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -451,17 +461,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::SHUTDOWN(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1101_xxxx_xxxx_xxxx_SHUTDOWN; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -474,17 +484,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::UPO_LOW(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1110_0xxx_xxxx_xxxx_UPO_LOW; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -497,17 +507,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::UPO_HIGH(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1110_1xxx_xxxx_xxxx_UPO_HIGH; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -520,17 +530,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::MODE1_DOUT_SCLK_RISING_EDGE(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1111_0xxx_xxxx_xxxx_MODE1_DOUT_SCLK_RISING_EDGE; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1; @@ -543,17 +553,17 @@ // @return 1 on success; 0 on failure uint8_t MAX5171::MODE0_DOUT_SCLK_FALLING_EDGE(void) { - + //---------------------------------------- // Define command code uint16_t mosiData16 = CMD_1111_1xxx_xxxx_xxxx_MODE0_DOUT_SCLK_FALLING_EDGE; - + //---------------------------------------- // SPI write 16-bit mosiData16 SPIoutputCS(0); SPIwrite16bits(mosiData16); SPIoutputCS(1); - + //---------------------------------------- // success return 1;