Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7353

SDK • Help needed programming DAC82001 using SPI

$
0
0
Hello,

I am completely lost as to what I need to do to program this 16-bit DAC using the SPI interface on the Pico 2. I have successfully read an ADC on the same board using SPI and this is using the other spi port. I have pasted my code below, including pinout.h which shows the connections between the DAC and the Pico 2.

The device I am trying to use - Texas Instruments DAC82001 - https://www.ti.com/lit/ds/symlink/dac82001.pdf

My main questions - am I using spi_write_blocking correctly and can anyone work out what the first 8 bits (the command byte if you will) should be from the datasheet? Currently the code is written to just start at 0xFFFF and slowly increment down to 0 and loop (just to see if anything changes on my multimeter - which it doesnt, it's around 0mV).

Pinout:

Code:

#ifndef _PINOUT_H_#define _PINOUT_H_constexpr unsigned int PIN_ADC_OUT = 12;constexpr unsigned int PIN_ADC_CS = 13;constexpr unsigned int PIN_ADC_CLK = 14;constexpr unsigned int PIN_DAC_IN = 19;constexpr unsigned int PIN_DAC_CS = 17;constexpr unsigned int PIN_DAC_CLK = 18;constexpr unsigned int PIN_PICO_LED = 25;#endif
Main.cpp: (the adc code is in a separate header file as is unrelated to my question)

Code:

#include <stdio.h>#include <stdlib.h>#include <hardware/spi.h>#include <hardware/gpio.h>#include <pico/stdio.h>#include <pico/time.h>#include "pinout.h"#include "ads8320.h"#define DEBUG_OUTPUT#define DAC_PORT spi0#define ADC_PORT spi1#define DAC_CLOCK 5e+6#define DAC_BYTES 3#define ADC_CLOCK 1e+6 /*https://www.ti.com/lit/ds/symlink/ads8320.pdf?ts=1754121189562*/constexpr unsigned int DAC_RESOLUTION = 65535; /* 2^N where N = 16 in this case*/constexpr float DAC_VREF = 3.0;constexpr float ADC_VREF = 3.0;constexpr unsigned int T_SAMPLE = 100; // sample time in msvoid setDAC(uint16_t dac_data) {    spi_set_format(DAC_PORT, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);    uint8_t command_byte = 0;    uint8_t bytes[DAC_BYTES] =     {        command_byte,        static_cast<uint8_t>((dac_data >> 8) & 0b11111111),        static_cast<uint8_t>((dac_data) & 0b11111111),    };    spi_init(DAC_PORT, DAC_CLOCK);    startSPI(PIN_DAC_CS);    spi_write_blocking(DAC_PORT, bytes, DAC_BYTES);    endSPI(PIN_DAC_CS);    #ifdef DEBUG_OUTPUT    printf("DAC set to %d\n", dac_data);    #endif }int main() {    uint16_t temp = 0xFFFF;    stdio_init_all();    ADS8320 adc(ADC_PORT, PIN_ADC_OUT, PIN_ADC_CS, PIN_ADC_CLK, ADC_CLOCK, ADC_VREF);    adc.init();    gpio_set_function(PIN_DAC_IN, GPIO_FUNC_SPI);    gpio_set_function(PIN_DAC_CLK, GPIO_FUNC_SPI);        gpio_init(PIN_DAC_CS);    gpio_init(PIN_PICO_LED);    gpio_set_dir(PIN_DAC_CS, GPIO_OUT);    gpio_set_dir(PIN_PICO_LED, GPIO_OUT);    spi_init(DAC_PORT, DAC_CLOCK);    gpio_put(PIN_PICO_LED, 1);    /* Chip select pin must be pulled high (off) initially  */    gpio_put(PIN_DAC_CS, 1);    while (1) {                if (temp > 0) temp -= 16; else temp = 0xFFFF;        setDAC(temp);                #ifdef DEBUG_OUTPUT        printf("DAC Set to: %d ", temp);        printf("ADC Output: %f\n", adc.senseVoltage());        #endif                sleep_ms(T_SAMPLE);            }}
Also, worth noting that the code builds without error
Thank you in advance!

Statistics: Posted by arthurmint — Fri Aug 15, 2025 10:48 am — Replies 4 — Views 91



Viewing all articles
Browse latest Browse all 7353

Trending Articles