Wiblocks --- NB1A DAC

NB1A DAC

The NB1A has a TLV5620 quad 8-bit DAC with a TLV431 1.24V reference. The DAC library contains the functions to initialize the DAC object, set the output voltage and change the DAC range. The example program initializes the DAC and sets channel A to 0.5V, B to 1V, C to 1.5V and D to 2V.

When initializing the DAC object you need to define which pin numbers are connected to the LOAD and LATCH pin. The example code below creates a DAC object using the default pin assignments for the NB1A.

// the load pin is attached to pin 10 (PB2)
// and the latch pin is attached to pin 9 (PB1)
// the spi port is attached to spi0

#define DAC_LOAD_PIN  10 //PB2
#define DAC_LATCH_PIN  9 //PB1

LED_debug led;

DAC_TLV5620 dac  = DAC_TLV5620(DAC_LOAD_PIN, DAC_LATCH_PIN);

NB1A DAC Example

#include <avr/interrupt.h>    
#include <avr/io.h>  

#include <SPI.h>
#include <DAC.h>
#include <LED_debug.h>

#define ON  HIGH
#define OFF LOW

// The DAC on an NB1A is a TLV5620. 

// the load pin is attached to pin 10 (PB2)
// and the latch pin is attached to pin 9 (PB1)
// the spi port is attached to spi0

#define SPI_TIMEOUT   10  //mS

#define DAC_LOAD_PIN  10 //PB2
#define DAC_LATCH_PIN  9 //PB1

LED_debug led;

SPI spi = SPI(SPI_TIMEOUT, NB1A_MOSI_PIN, NB1A_MISO_PIN, NB1A_SCK_PIN);
DAC_TLV5620 dac  = DAC_TLV5620(DAC_LOAD_PIN, DAC_LATCH_PIN);

void setup() {

  // Compensating for the 12MHz XTAL
  // 12800 = (16/12) * 9600
  // 25600 = (16/12) * 19200

  Serial.begin(12800);

  // Setup the SPI control register (SPCR)
  // and the status register (SPSR)

  // Page 174 ATmega328P Datasheet Rev 8025I-AVR-02/09

  // SPIE = 0  SPI interrupts disabled (=1 enabled)
  // SPE  = 1  SPI enabled          (=0 disabled)
  // DORD = 0  Data Order MSB First (=1 LSB First)
  // MSTR = 1  Master mode          (=0 Slave)
  // CPOL = 0  SCK low when idle    (=1 SCK high when idle)
  // CPHA = 1  Sample on leading edge (=1 falling edge)
  // SPR1, SPR2 = 0 fosc/4 (= 1 fosc/16)
  //                       (= 2 fosc/64)
  //                       (= 3 fosc/128)

  // SPSR is read only except for the double speed bit

  // SPI2X = 1 double speed

  spi.init(((1<<SPE) | (1<<MSTR) | (1<<CPHA)), (1<<SPI2X));


}

void loop() {

  led.blink(1);
  dac.set_voltage(DAC_CH_A, 0.5);
  delay(1000);

  led.blink(2);
  dac.set_voltage(DAC_CH_B, 1);
  delay(1000);
  
  led.blink(3);
  dac.set_voltage(DAC_CH_C, 1.5);
  delay(1000);

  led.blink(4);
  dac.set_voltage(DAC_CH_D, 2);
  delay(1000);
  
  while(1) {
    led.blink(1);
    delay(1000);
  }

}