Hello everyone,
It had been a while since I had been back.
I am currently programming a PCB with a DAC8168 (8 outputs 14bits).
I found a suitable piece of code to program it.
But here is nothing is simple in life, the piece of code that I found is made for a DAC8568 (8 outputs 16 bits), either I just have a modification to make, however I have trouble understanding how it organizes the bits to create a 32bit message. Perhaps you could enlighten me on the subject?
So wouldn't it be easier to use a 32bit variable and send it in SPI?
Cordialement ANOAT
It had been a while since I had been back.
I am currently programming a PCB with a DAC8168 (8 outputs 14bits).
I found a suitable piece of code to program it.
Code:
#include <Arduino.h>
#include <SPI.h>
#define syncPin PB8
unsigned int one;
unsigned int two;
unsigned int three;
unsigned int four;
void DAC8568Write(unsigned int prefix, unsigned int control, unsigned int address, unsigned int data, unsigned int feature) {
one = (prefix << 4)|control;
two = (address << 4)|(data >>12);
three = data >> 4;
four = (data << 4)|feature;
digitalWrite(syncPin,LOW);
SPI.transfer(one);
SPI.transfer(two);
SPI.transfer(three);
SPI.transfer(four);
digitalWrite(syncPin,HIGH);
}
void DAC8568Reset()
{
DAC8568Write(0b0001, 0b1100, 0x00, 0x00, 0x00) ;
}
void DAC8568InternalRef()
{
DAC8568Write(0x00, 0x08, 0x00, 0x0000, 0x01) ;
}
void DAC8568SetVoltage(unsigned int channel,unsigned int voltage)
{
DAC8568Write(0, 3, channel, voltage, 0) ;
}
void setup()
{
pinMode (syncPin, OUTPUT);
digitalWrite(syncPin, HIGH);
SPI.beginTransaction(SPISettings(48000000, MSBFIRST, SPI_MODE1));
delay(200);
DAC8568Reset();
delay(200);
DAC8568InternalRef();
delay(200);
}
void loop()
{
unsigned int i;
for (i=0;i<8;++i)
{
DAC8568SetVoltage(i,2700);
}
delayMicroseconds(100);
}
But here is nothing is simple in life, the piece of code that I found is made for a DAC8568 (8 outputs 16 bits), either I just have a modification to make, however I have trouble understanding how it organizes the bits to create a 32bit message. Perhaps you could enlighten me on the subject?
Code:
void DAC8568Write(unsigned int prefix, unsigned int control, unsigned int address, unsigned int data, unsigned int feature) {
one = (prefix << 4)|control;
two = (address << 4)|(data >>12);
three = data >> 4;
four = (data << 4)|feature;
digitalWrite(syncPin,LOW);
SPI.transfer(one);
SPI.transfer(two);
SPI.transfer(three);
SPI.transfer(four);
digitalWrite(syncPin,HIGH);
}
So wouldn't it be easier to use a 32bit variable and send it in SPI?
Code:
unsigned long message;
void DAC8568Write(unsigned int prefix, unsigned int control, unsigned int address, unsigned int data, unsigned int feature) {
message = (prefix << 28) | (control << 24) | (adress << 20) | (data << 4) | (feature);
digitalWrite(syncPin,LOW);
SPI.transfer(message);
digitalWrite(syncPin,HIGH);
}
Cordialement ANOAT