This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DAC81416: DAC SPI MCU PROGRAM EXAMPLE

Part Number: DAC81416
Other Parts Discussed in Thread: ENERGIA,

Hi ,

I have Teensy 4.1 MCU.

Can I request for a SPI code example for DAC81416EVM ?

Thanks

  • Hi,

    We don't have code examples specific to Teensy MCU board. Below code examples for Energia sketch

    #include <SPI.h>

    #define NUM_BYTES 3 //Number of bytes in SPI frame

    //SPIClass tivacSPI0(0); // SSI0
    //SPIClass tivacSPI1(1); // SSI1
    //SPIClass tivacSPI2(2); // SSI2
    //SPIClass tivacSPI3(3); // SSI3 on port F
    SPIClass tivacSPI3_Q(4); // SSI3 on port Q

    void setup() {
    uint8_t initTx0[NUM_BYTES], initTx1[NUM_BYTES], initTx2[NUM_BYTES], i;
    int8_t ret;
    uint8_t *initPtr;

    Serial.begin(9600);
    Serial.print("Starting DAC81416 Example");

    //tivacSPI0.begin();
    //tivacSPI1.begin();
    //tivacSPI2.begin();
    //tivacSPI3.begin();
    tivacSPI3_Q.begin();

    pinMode(PD_2, OUTPUT); // Configure PD2 as CLR
    digitalWrite(PD_2, HIGH); // set CLR HIGH by default

    //Range: +/- 20V
    initTx0[0] = 0x0A;
    initTx0[1] = 0xCC;
    initTx0[2] = 0xCC;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Range: +/- 20V
    initTx0[0] = 0x0B;
    initTx0[1] = 0xCC;
    initTx0[2] = 0xCC;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Range: +/- 20V
    initTx0[0] = 0x0C;
    initTx0[1] = 0xCC;
    initTx0[2] = 0xCC;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Range: +/- 20V
    initTx0[0] = 0x0D;
    initTx0[1] = 0xCC;
    initTx0[2] = 0xCC;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Power-up device
    initTx0[0] = 0x03;
    initTx0[1] = 0x0A;
    initTx0[2] = 0x84;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Power-up all channels
    initTx0[0] = 0x09;
    initTx0[1] = 0x00;
    initTx0[2] = 0x00;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Enable Broadcast for all Channels
    initTx0[0] = 0x05;
    initTx0[1] = 0xFF;
    initTx0[2] = 0xFF;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);

    //Write Code to all Channels
    initTx0[0] = 0x0F;
    initTx0[1] = 0xFF;
    initTx0[2] = 0xFF;
    initPtr = &initTx0[0];
    ret = tivacSPI3_Q.DACtransfer((uint8_t*)initPtr, (uint8_t)NUM_BYTES);
    }

    void loop() {
    int8_t rv, ldacFlag = 0;
    int32_t sampleCount;
    uint8_t *txPtr;
    uint8_t spiTx0[NUM_BYTES];

    //Write Mid-code
    spiTx0[0] = 0x10;
    spiTx0[1] = 0x7F;
    spiTx0[2] = 0xFF;
    txPtr = &spiTx0[0];
    rv = tivacSPI3_Q.DACtransfer((uint8_t*)txPtr, (uint8_t)(NUM_BYTES));

    delay(100);

    //Write Full-code
    spiTx0[0] = 0x10;
    spiTx0[1] = 0xFF;
    spiTx0[2] = 0xFF;
    txPtr = &spiTx0[0];
    rv = tivacSPI3_Q.DACtransfer((uint8_t*)txPtr, (uint8_t)(NUM_BYTES));

    delay(100);
    }

    //Addition to SPI.cpp

    int8_t SPIClass::DACtransfer(uint8_t* datapointer, uint8_t numBytes) {
    unsigned long rxtxData[MAX_BYTES];

    if(numBytes > MAX_BYTES){
    return -1;
    }
    if(datapointer == NULL){
    return -1;
    }

    for(int byteCount=0; byteCount < numBytes; byteCount++){
    rxtxData[byteCount] = datapointer[byteCount];
    }
    if(SSIBitOrder == LSBFIRST) {
    return -1;
    }
    for(int byteCount=0; byteCount < numBytes; byteCount++){
    ROM_SSIDataPut(SSIBASE, (uint8_t) rxtxData[byteCount]);
    }

    while(ROM_SSIBusy(SSIBASE));

    return (int8_t) 0;
    }


    In case, you need a generic setting for SPI on any processor. It's as follows:

    Mode: Mode-1 (CPOL=0, CPHA=1)
    Order: MSB First
    CS Polarity: Active Low
    Number of Bytes: 3

    You can use GPIOs for LDAC and CLR, if needed.

    Regards,

    AK

  • Hi Akhilesh,

    unsigned long rxtxData[MAX_BYTES];

    What is the variable MAX_BYTE ?

    I get this error

    teensy_SPI:149: error: 'MAX_BYTES' was not declared in this scope
       unsigned long rxtxData[MAX_BYTES];

  • Hi,

    you can define MAX_BYTES in the beginning of the code and should be ok after that.

    Regards,

    AK