Source lists for AD converter is explained on "Interface" No.10, CQ publishing Co.,Ltd, 2014. Source lists for DA converter is explained on "Interface" No.11, CQ publishing Co.,Ltd, 2014. 本ライブラリのADコンバータ用のソースリストについては,CQ出版社のインターフェース 2014年10月号に載っています. DAコンバータ用のソースリストについての説明は,CQ出版社のインターフェース 2014年11月号に載っています.

Dependents:   FFT_Sampling FIR_LPF_Direct FIR_LPF_Symmetry IIR_LPF ... more

Fork of SignalProcessingIO by CQpub0 Mikami

MCP4922Single.cpp

Committer:
CQpub0Mikami
Date:
2014-07-13
Revision:
0:a2cdffe24b67

File content as of revision 0:a2cdffe24b67:

//------------------------------------------------------
// Class for 1 DAC in MCP4922
//
// Copyright (c) 2014 MIKAMI, Naoki,  2014/06/18
//------------------------------------------------------

#include "MCP4922Single.hpp"

namespace Mikami
{
    Dac::Dac(DAC dac, PinName mosi, PinName sclk,
             PinName cs, PinName ldac, int hz)
        : wcr_(dac | 0x3000), mySpi_(mosi, NC, sclk),
          myCs_(cs, 1), myLd_(ldac, 0)
    {
        // Set SPI format and bus frequency
        mySpi_.format(16, 0);
        mySpi_.frequency(hz);

        // Set DAC to 0
        WriteDac(0);
    }

    void Dac::Write(float value)
    {
        if (value < -1.0f) value = -1.0f;
        if (value >  1.0f) value =  1.0f;

        WriteDac((uint16_t)((value + 1.0f)*2047));
    }

    void Dac::Write(uint16_t value)
    {
        WriteDac((value > 4095) ? 4095 : value);
    }
        
    void Dac::Ldac()
    {
        myLd_.write(0);
        wait_us(1);       
        myLd_.write(1);
    }

    void Dac::WriteDac(uint16_t value)
    {
        myCs_.write(0); // cs <= L
        mySpi_.write(value | wcr_);
        myCs_.write(1); // cs <= H
    }
}