NB2A DAC
The NB2A has a TLV5618 dual 12-bit DAC with a 1.24V reference. The
DAC library contains the functions to initialize the DAC object, set
the output voltage. The example program initializes the DAC and sets
channel A to 0.75V, B to 0.5V.
When initializing the DAC object you need to define which pin number
is connected to the CS pin. The example code below creates a DAC
object using the default pin assignment for the NB2A.
// 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_CS_PIN 4 // Sanguino Pin Number (ATmega644P PB4)
LED_debug led;
DAC_TLV5618 dac = DAC_TLV5618(DAC_CS_PIN);
NB2A 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
// DAC demo program for an NB2A.
// The DAC is a TI TLV5618 with a 1.225V reference.
// the chip select pin is attached to pin 5 (PB4)
#define DAC_CS_PIN 4 //PB4
#define SPI_TIMEOUT 10 // mS
LED_debug led(15);
SPI spi = SPI(SPI_TIMEOUT, NB2A_MOSI_PIN, NB2A_MISO_PIN, NB2A_SCK_PIN);
DAC_TLV5618 dac = DAC_TLV5618(DAC_CS_PIN);
void setup() {
//
// The NB2A uses a 12MHz XTAL
// The Sanguino uses a 16MHz XTAL.
// If f_cpu frequency is set to 16000000L in your
// boards.txt file then correct for the
// difference in XTAL frequency
// using one of the values below --
//
// 12800 = (16/12) * 9600
// 25600 = (16/12) * 19200
//
Serial.begin(9600); // f_cpu set to 12000000L in boards.txt
// Setup the SPI control register (SPCR)
// and the status register (SPSR)
// Page 169 ATmega644P Datasheet Rev 8011L-AVR-02/09
// SPIE = 0 =0 SPI INT disabled, =1 enabled
// SPE = 1 =0 SPI disabled, =1 enabled
// DORD = 0 =0 MSB First, =1 LSB First
// MSTR = 1 =0 Slave, =1 Master mode
// CPOL = 1 =0 SCK high when idle, =1 SCK low when idle,
// CPHA = 1 =0 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) | (0<<CPHA) | (1<<CPOL)), (1<<SPI2X));
}
void loop() {
// DACA is U21 pin 4
// DACB is U21 pin 7
dac.set_voltage(DAC_WRITE_B_AND_BUFFER, 1);
dac.set_voltage(DAC_WRITE_A_UPDATE_B, 1.75);
while(1) {
led.blink(2);
delay(1000);
}
}