MCP4922 Lib

Fork of MCP4922 by Akinori Hashimoto

Files at this revision

API Documentation at this revision

Comitter:
AkinoriHashimoto
Date:
Mon Sep 25 05:48:38 2017 +0000
Commit message:
MCP4922 Lib.

Changed in this revision

MCP4922.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4922.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4922.cpp	Mon Sep 25 05:48:38 2017 +0000
@@ -0,0 +1,156 @@
+#include "MCP4922.h"
+
+// **** Constructor & destructor ****
+// With LathPin
+MCP4922::MCP4922(PinName mosi, PinName sck, PinName _cs, PinName _latch)
+    : p_spi(new SPI(mosi, NC, sck)), spi(*p_spi), cs(_cs, 1), latch(_latch, 1)
+{
+    useLPin= true;  //Latch pin
+}
+MCP4922::MCP4922(SPI &_spi, PinName _cs, PinName _latch)
+    : p_spi(NULL), spi(_spi), cs(_cs, 1), latch(_latch, 1)
+{
+    useLPin= true;  //Latch pin
+}
+// W/O LatchPin
+MCP4922::MCP4922(PinName mosi, PinName sck, PinName _cs)
+    : p_spi(new SPI(mosi, NC, sck)), spi(*p_spi), cs(_cs, 1), latch(NC)
+{
+    useLPin= false;  //Latch pin
+}
+MCP4922::MCP4922(SPI &_spi, PinName _cs)
+    : p_spi(NULL), spi(_spi), cs(_cs, 1), latch(NC)
+{
+    useLPin= false;  //Latch pin
+}
+MCP4922::~MCP4922()
+{
+    if(p_spi != NULL)
+        delete p_spi;
+}
+
+// **** INIT Config ****
+MCP4922::ERR MCP4922::init(int hz, int gainA, int gainB, bool bufA, bool bufB)
+{
+    // valA/B init.
+//    valA= 0x0000;   // bit15 : 0
+//    valB= 0x8000;   // bit15 : 1
+
+    if(hz <= 0 || 20000000 < hz)
+        return ERR_HZ;
+    spi.frequency(hz);
+    spi.format(16);  // 16bit, mode=0
+
+    _bufA= bufA;
+    _bufB= bufB;
+//    valA |= (_bufA << 14);  // if ture; |= 0x0400
+//    valB |= (_bufB << 14);
+
+    if(!(gainA==1 || gainA==2))
+        return ERR_GAIN;
+    if(!(gainB==1 || gainB==2))
+        return ERR_GAIN;
+    _gainA= (gainA==1) ? true : false;  // false=0: 2x
+    _gainB= (gainB==1) ? true : false;  // false=0: 2x
+//    valA |= (_gainA << 13);
+//    valB |= (_gainB << 13);
+
+    // bit12; ShatDown=0
+//    valA |= (1 << 12);
+//    valB |= (1 << 12);
+    valA= 0x0000+ (_bufA << 14)+ (_gainA << 13)+ 0x1000;
+    valB= 0x8000+ (_bufB << 14)+ (_gainB << 13)+ 0x1000;
+
+    return SUCCESS;
+}
+
+
+void MCP4922::setVal(int valA, int valB)
+{
+    this->setVal(chA, valA);
+    this->setVal(chB, valB);
+    this->write();
+    return;
+}
+
+void MCP4922::setVal(float valA, float valB)
+{
+    int a= (int)(valA*4095.0+ 0.5);
+    int b= (int)(valB*4095.0+ 0.5);
+    this->setVal(a, b);
+    return;
+}
+
+void MCP4922::setVal(CH ch, int val)
+{
+    /*    if(val < 0 || 4096 < val)
+            return ERR_VAL;*/
+    if(val < 0)
+        val= 0;
+    if(4096 < val)
+        val= 4095;
+
+    uint16_t tmp= val;    // bit0-bit11; 1B+4b
+    if(ch == chA) {
+        valA &= 0xf000;
+        valA |= tmp;
+    } else {
+        valB &= 0xf000;
+        valB |= tmp;
+    }
+    return;
+}
+
+void MCP4922::setVal(CH ch, float val)
+{
+    /*    if(val < 0.0 || 1.0 < val)
+            return ERR_VAL;*/
+    if(val < 0.0)
+        val= 0.0;
+    if(1.0 < val)
+        val= 1.0;
+
+    this->setVal(ch, (int)(val*4095.0+ 0.5));
+    return;
+}
+
+void MCP4922::write()
+{
+    this->_write(valA);
+    this->_write(valB);
+    this->update();
+    return;
+}
+
+void MCP4922::write(CH ch, bool latch)
+{
+    uint16_t val;
+    if(ch == chA)
+        val= valA;
+    else
+        val= valB;
+    _write(val);
+
+    if(!latch)
+        update();
+
+    return;
+}
+
+void MCP4922::_write(uint16_t val)
+{
+    latch= 1;
+    cs= 0;
+    spi.write(val);
+    cs= 1;
+    return;
+}
+
+void MCP4922::update()
+{
+    if(latch == NULL)
+        return;
+    latch= 0;
+}
+
+// EOF
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4922.h	Mon Sep 25 05:48:38 2017 +0000
@@ -0,0 +1,109 @@
+/**
+ *
+ * 12bit Resolution. Dual-channel and Rail-to-Rail Output.
+ * SPI Interface with 20MHz. Latching Dual output with LDAC.
+ * Seting time of 4.5us. Selectable 1x or 2x Gain.
+ * Vdd supplied 2.7 to 5.5V, Vref < Vdd.
+ * LDAC setup time: 40ns, pulse: 100ns.
+ */
+
+/** @code
+
+ * @endcode
+ */
+ 
+#pragma once
+
+#include "mbed.h"
+
+class MCP4922{
+public:
+    // With LatchPin
+    /** Constructor; 
+     *  @param mosi, sck, cs, latch
+    */
+    MCP4922(PinName mosi, PinName sck, PinName _cs, PinName _latch);
+    /** Constructor; 
+     *  @param spi's ptr, cs, latch
+    */
+    MCP4922(SPI &_spi, PinName _cs, PinName _latch);
+    
+    // W/O LatchPin
+    /** Constructor; 
+     *  @param mosi, sck, cs
+    */
+    MCP4922(PinName mosi, PinName sck, PinName _cs);
+    /** Constructor; 
+     *  @param spi's ptr, cs
+    */
+    MCP4922(SPI &_spi, PinName _cs);
+    ~MCP4922();
+//  ******************** enable printf() future of C-language. ****************
+    
+    enum ERR{
+        SUCCESS, ERR_HZ, ERR_GAIN, ERR_VAL
+    };
+    enum CH{
+        chA, chB
+    };
+    
+    /** ESENTIAL FUNC.; Initialize Configlation.
+     *
+     * @param hz; freq. of SPI.
+     * @param gainA/B; output gain select 1x|2x. Vref*gain.
+     * @param bufA/B; Normally off(false); Buffered= true;
+     */
+    MCP4922::ERR init(int hz= 20000000, int gainA= 1, int gainB= 1, 
+        bool bufA= false, bool bufB= false);
+    
+    /** Set Value
+     * @param A, B; 0-4095 (12bit)
+     */
+    void setVal(int valA, int valB);
+    
+    /** Set Value
+     * @param A, B; 0.0f-1.0f (12bit)
+     */
+    void setVal(float valA, float valB);
+    
+    /** Set Value
+     * @param val; 0-4095 (12bit)
+     */
+    void setVal(CH ch, int val);
+    
+    /** Set Value
+     * @param val; 0.0f-1.0f (12bit)
+     */
+    void setVal(CH ch, float val);
+
+    /** Write ALL with update-output(latch off).
+     */
+    void write();
+    
+    /** Write
+     */
+    void write(CH ch, bool latch= true);
+    
+    /** Update
+     */
+    void update();
+
+private:
+    // using i2c
+    SPI *p_spi;
+    SPI &spi;
+    DigitalOut cs;
+    DigitalOut latch;
+    
+    bool _bufA, _bufB, _gainA, _gainB;
+  
+    uint16_t  valA, valB;
+//    bitset<16> valA(0);  // 16bit 2Byte.
+//    bitset<16> valB(0);  // 16bit 2Byte.
+    
+    void _write(uint16_t val);
+    
+    bool useLPin;
+};
+
+// EOF
\ No newline at end of file