new

Files at this revision

API Documentation at this revision

Comitter:
magnetone
Date:
Mon Oct 28 12:08:13 2013 +0000
Commit message:
new

Changed in this revision

SoftMCP4922.cpp Show annotated file Show diff for this revision Revisions of this file
SoftMCP4922.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftMCP4922.cpp	Mon Oct 28 12:08:13 2013 +0000
@@ -0,0 +1,93 @@
+/*
+ * MCP4922 (Soft SPI) Library
+ * 2013 Magnetone
+ */
+ 
+#include "SoftMCP4922.h"
+
+SoftMCP4922::SoftMCP4922( PinName sck, PinName sdi, PinName cs, PinName ldac, PinName shdn, int ch):
+    m_sck( sck ),
+    m_sdi( sdi ),
+    m_cs( cs ),
+    m_ldac( ldac ),
+    m_shdn( shdn ),
+    m_ch( ch )
+{
+    
+    m_shdn = 1; // non shutdown
+    m_sck = 0;  // init
+    
+    // out vcc/2
+    write_u12( 2048 );
+    
+}
+
+void SoftMCP4922::write_u16( unsigned short value )
+{
+    dacControl( value >> 4 );
+}
+
+void SoftMCP4922::write_u12( unsigned short value )
+{
+    dacControl( value );
+}
+
+void SoftMCP4922::write_u10( unsigned short value )
+{
+    dacControl( value << 2 );
+}
+
+void SoftMCP4922::write_u8( unsigned char value )
+{
+    dacControl( value << 4 );
+}
+
+// DAC control function
+void SoftMCP4922::dacControl( unsigned char value )
+{
+     int i ;
+
+    // dac setting start
+    m_cs = 0;
+    m_ldac = 1;
+    
+    // channel select
+    if( m_ch == 0 ){
+        m_sdi = 0;
+    }else{
+        m_sdi = 1;
+    }
+    m_sck = 1;
+    m_sck = 0;
+    
+    // vref buffer off
+    m_sdi = 0;
+    m_sck = 1;
+    m_sck = 0;
+    
+    // output gain = 1
+    m_sdi = 1;
+    m_sck = 1;
+    m_sck = 0;
+    
+    // analog output enable
+    m_sdi = 1;
+    m_sck = 1;
+    m_sck = 0;
+     
+    // DAC data out
+    for (i=11 ; i>=0 ; i--) {
+        if ( ( ( value >> i ) & 0x1 ) == 1 )
+            m_sdi = 1;
+        else
+            m_sdi = 0;
+        m_sck = 1;
+        m_sck = 0;
+    }
+
+    // analog out
+    m_cs = 1;
+    m_ldac = 0;
+    
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftMCP4922.h	Mon Oct 28 12:08:13 2013 +0000
@@ -0,0 +1,44 @@
+#ifndef SOFT_MPC4922_H
+#define SOFT_MPC4922_H
+
+#include "mbed.h"
+
+class SoftMCP4922
+{
+public:
+    /*
+     * MCP4922 - soft SPI
+     * param
+     * [in] sck: clock pin
+     * [in] sdi: data pin
+     * [in] cs: chip select pin
+     * [in] ldac: latch pin
+     * [in] shdn: shutdown pin
+     * [in] ch: output channel
+     */
+    SoftMCP4922( PinName sck, PinName sdi, PinName cs, PinName ldac, PinName shdn, int ch );
+    
+    /*
+     * write_u*
+     * param
+     * [in] value: value(*bit)
+     */
+    void write_u16( unsigned short value );
+    void write_u12( unsigned short value );
+    void write_u10( unsigned short value );
+    void write_u8( unsigned char value );
+
+private:
+    void dacControl( unsigned char value );
+    
+    DigitalOut m_sck;
+    DigitalOut m_sdi;
+    DigitalOut m_cs;
+    DigitalOut m_ldac;
+    DigitalOut m_shdn;
+    int m_ch;
+};
+
+
+
+#endif  // SOFT_MPC4922_H
\ No newline at end of file