不韋 呂 / UITDSP_ADDA

Dependents:   UITDSP_ADDA_Example UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DAC_MCP4921.hpp Source File

DAC_MCP4921.hpp

00001 //------------------------------------------------------
00002 // Class for single DAC in MCP4921 -- Header
00003 //      Fast version
00004 //
00005 // Default pin assign
00006 //      D11  SPI Master Out Slave In
00007 //      D13  SPI Serial Clock
00008 //      D10  SPI Slave Select ----------------------- TIM4
00009 //      D12  to MCP4921 LDAC pin
00010 //      D9   clock for Switched-capacitor filter ---- TIM3
00011 //
00012 // Argument cs in constructor must be output of TIM4,
00013 // i.e. D10(PB_6), PB_7, D15(PB_8), or D14(PB_9)
00014 //
00015 // Argument pin in function ScfClockTim3() can be
00016 // PA_6(D12), PB_4(D5), PC_6, PB_5(D4), PC_7(D9),
00017 // PC_8, or PC_9
00018 //
00019 // 2015/03/27, Copyright (c) 2015 MIKAMI, Naoki
00020 //------------------------------------------------------
00021 
00022 #ifndef DAC_MCP4921_HPP
00023 #define DAC_MCP4921_HPP
00024 
00025 #include "mbed.h"
00026 #include "TIM4_SlaveSelect.hpp"
00027 
00028 namespace Mikami
00029 {
00030     class DAC_MCP4921
00031     {
00032     public:
00033         // Constructor
00034         DAC_MCP4921(
00035             PinName mosi = SPI_MOSI,    // D11
00036             PinName sclk = SPI_SCK,     // D13
00037             PinName cs   = SPI_CS,      // D10
00038             PinName ldac = SPI_MISO);   // D12
00039 
00040         // -1.0f <= value <= 1.0f
00041         void Write(float value)
00042         {
00043             if (value < -1.0f) value = -1.0f;
00044             if (value >  1.0f) value =  1.0f;
00045 
00046             WriteDac((value + 1.0f)*2047);
00047         }
00048 
00049         // 0 <= value <= 4095
00050         void Write(uint16_t value)
00051         {   WriteDac((value > 4095) ? 4095 : value); }
00052 
00053         // Check busy
00054         bool IsBusy()
00055         {   return (mySpi_->SR & SPI_SR_BSY) == SPI_SR_BSY; }
00056 
00057         // Set clock for switched-capacitor filter
00058         void ScfClockTim3(uint32_t clock, PinName pin = D9);
00059 
00060     protected:
00061         void SlaveSelect() { ss_->SlaveSelect(); }
00062         void WriteSpi(uint16_t value) { mySpi_->DR = value; }
00063         
00064         // generate LDAC negative-going pulse
00065         void Ldac()
00066         {
00067             ld_.write(0);
00068             ld_.write(0);   // ensure width of "L" pulse
00069             ld_.write(1);
00070         }
00071 
00072     private:
00073         SPI spi_;               // SPI object of mbed
00074         TIM4_SlaveSelect* ss_;  // Slave Select for DAC
00075         DigitalOut ld_;         // for LDAC
00076 
00077         // Pointer for SPI
00078         SPI_TypeDef* mySpi_;
00079 
00080         // for inhibition of copy constructor
00081         DAC_MCP4921(const DAC_MCP4921&);
00082         // for inhibition of substitute operator
00083         DAC_MCP4921& operator=(const DAC_MCP4921&);     
00084         
00085         // for internal use
00086         virtual void WriteDac(uint16_t value)
00087         {
00088             while (IsBusy()) {}
00089             SlaveSelect();
00090             WriteSpi(value | 0x3000);
00091         }       
00092     };
00093 }
00094 #endif  // DAC_MCP4921_HPP