Hello,
I’m developing an application where I need to communicate with the Dual Channel DAC MCP4922.
I made all the connections and I’ve written the code in C# following the official toradex example but I don’t see any result because the output voltage of the DAC is always ZERO.
I’m using the latest Toradex libraries toradexcelibraries_2.3b4555-20190107.
The connections of the DAC are the following:
MCP4922 – IRIS
Pin 1 (VDD) → 3.3V
Pin 3 (CS) → SSPFRM
Pin 4 (SCK) → SSPSCLK
Pin 5 (SDI) → SSPTXD
Pin 8 (SHDN) → 3.3V
Pin 9 (LDAC) → 3.3V
Pn 10 (VOUTB)
Pin 11 (VREFB) → 3.3V
Pin 12 (VSS) → GND
Pin 13 (VREFA) → 3.3V
Pin 14 (VOUTA)
The code I use is the following:
public class MCP4922
{
IntPtr spiHandle = IntPtr.Zero;
uint returnValue = 0;
private const float LSB = 1 / 4095;
public const int MAX_VALUE = 4095; //VOUT = InputCode*LSB/MAX_VAçLUE 4095
public const int MIN_VALUE = 0;
public uint DeviceAddress;
public MCP4922()
{
spiHandle = spi_imx6.Imx6Spi_Init("SPI2"); ///< Init SPI library
if (spiHandle == IntPtr.Zero)
{
Program.cGlobals.cLogging.LogMessage("MCP4922 --> Error initializing the SPI Library");
}
spi_imx6.Imx6Spi_SetConfigInt(spiHandle, "SpiMode", 0, TdxCommon.ParamStorageType.StoreVolatile); ///< Set SPI on Mode 0
spi_imx6.Imx6Spi_SetConfigInt(spiHandle, "BitRateHz", 10000, TdxCommon.ParamStorageType.StoreVolatile); ///< Set SPI clock 26000 Lhz
if (!spi_imx6.Imx6Spi_Open(spiHandle))
{
Program.cGlobals.cLogging.LogMessage("MCP4922 --> Failed to open SPI");
}
spi_imx6.Imx6Spi_SetConfigInt(spiHandle, "ioCS", 86, TdxCommon.ParamStorageType.StoreVolatile); ///< Set Chip Select pin sodimm 86
}
public void WriteRegister(int DACNumber, int BufferControl, int Gain, int Shutdown, int iValue)
{
//DACNumber=0 => Registro A - DACNumber=1 => Registro B
//BufferControl=0 => Unbuffered - BufferControl=1 => Buffered
//Gain=0 => VOUT = 2 * VREF * D/4096 - Gain=1 => VOUT = VREF * D/4096 dove D è il valore del registro D11-D0
//Shutdown=0 => Shutdown the selected DAC channel - Shutdown=1 => Active mode operation. VOUT is available
if (DACNumber==0 && bReverse_A_Reg)
iValue = MAX_VALUE - iValue;
if (DACNumber == 1 && bReverse_B_Reg)
iValue = MAX_VALUE - iValue;
uint bufferLenght = 2;
Byte[] DataToWrite = new Byte[bufferLenght];
int SetupValue = DACNumber * 8 + BufferControl * 4 + Gain * 2 + Shutdown;
//DataToWrite[0] = (byte)(((iValue & 3840) >> 8) | (SetupValue * 16));
//DataToWrite[1] = (byte)(iValue & 255);
DataToWrite[0] = (byte)(63);
DataToWrite[1] = (byte)(63);
try
{
unsafe
{
fixed (byte* for_Casting_Intptr_to_Byte = DataToWrite)
{
//returnValue = spi.Spi_Write(spiHandle, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght);
returnValue = spi_imx6.Imx6Spi_Write(spiHandle, (IntPtr)for_Casting_Intptr_to_Byte, bufferLenght);
if (returnValue == 0)
throw new Exception(); ///< If Write operation returns 0
}
}
}
catch (Exception ex)
{
Program.cGlobals.cLogging.LogMessage("Scrittura Configurazione Fallita - Codice Errore: " + ex.Message);
}
}
public void Exit()
{
spi_imx6.Imx6Spi_Close(spiHandle);
spi_imx6.Imx6Spi_Deinit(spiHandle); ///< De-Initializes SPI
}
}
I don’t receive any error during the initialization and after a write the returned value is 2.
I’ve tryied to change the init string to SPI1 and I’ve tryied all the SPI modes but nothing, the VOUT is allways ZERO.
I’ve checked with the oscilloscope and strange I’ve found that no clock signal is revealed out of the pin SSPSCLK.
Probably I’m missing something and I hope you can help me.
Thanks in advance,
Giuseppe