Class library for internal ADC and DAC connected by SPI. ADC is triggered by TIM2. This library support clock generator using TIM3 for switched-capacitor filter to smooth output signal of DAC. This library includes derivative class to support interrupt occured in end of AD conversion. Slave select of SPI for DAC is generated using TIM4. Validated for ST Nucleo F401RE, F411RE. New version. 内蔵 ADC と,SPI 接続の DAC のためのクラスライブラリ.ADC の変換開始トリガは TIM2 で発生.DAC の出力信号を平滑化するためのスイッチトキャパシタフィルタ用のクロックは TIM3 を使用.DAC の SPI 用スレーブ選択信号は TIM4 で発生.ST Nucleo F401RE,F411RE で動作を確認.新バージョン

Dependents:   UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more

Revision:
18:0bfced9d5526
Parent:
16:0001d3e93bee
Child:
19:900efc6796d4
diff -r fd4f92ad4323 -r 0bfced9d5526 DAC_MCP4922.hpp
--- a/DAC_MCP4922.hpp	Thu Jan 29 05:52:57 2015 +0000
+++ b/DAC_MCP4922.hpp	Thu Jan 29 07:55:04 2015 +0000
@@ -16,7 +16,7 @@
 // PA_6(D12), PB_4(D5), PC_6, PB_5(D4), PC_7(D9),
 // PC_8, or PC_9
 //
-// 2014/12/21, Copyright (c) 2014 MIKAMI, Naoki
+// 2015/01/29, Copyright (c) 2014 MIKAMI, Naoki
 //------------------------------------------------------
 
 #ifndef DAC_MCP4922_HPP
@@ -41,16 +41,29 @@
             PinName ldac = SPI_MISO);   // D12
 
         // -1.0f <= value <= 1.0f
-        void Write(float value);
+        void Write(float value)
+        {
+            if (value < -1.0f) value = -1.0f;
+            if (value >  1.0f) value =  1.0f;
+
+            WriteDac((uint16_t)((value + 1.0f)*2047));
+        }
+
         // 0 <= value <= 4095
-        void Write(uint16_t value);
+        void Write(uint16_t value)
+        {   WriteDac((value > 4095) ? 4095 : value); }
         
         // generate LDAC negative-going pulse
-        void Ldac();
+        void Ldac()
+        {
+            ld_.write(0);
+            ld_.write(0);   // ensure width of "L" pulse
+            ld_.write(1);
+        }
 
         // Check busy
         bool IsBusy()
-        { return (mySpi_->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY; }
+        {   return (mySpi_->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY; }
         
         // Set clock for switched-capacitor filter
         void ScfClockTim3(uint32_t clock, PinName pin = D9);
@@ -71,7 +84,11 @@
         DAC_MCP4922& operator=(const DAC_MCP4922&);     
         
         // for internal use
-        void WriteDac(uint16_t value);
+        void WriteDac(uint16_t value)
+        {
+            ss_->SlaveSelect();
+            mySpi_->DR = value | wcr_;
+        }
     };
 }
 #endif  // DAC_MCP4922_HPP