Arduino Forum

Using Arduino => Project Guidance => Topic started by: SuperR on Feb 24, 2013, 10:44 am

Title: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 24, 2013, 10:44 am
I will be implementing a single channel DAC in my current/upcoming project.

The DAC IC that I have available is the AD669;

ftp://ftp.signalogic.com/data_sheets/AD_DA_Converters/AD669.pdf

It wants to receive a 16-bit parallel input. Fine by me, but what is the best/fastest way to do that? I can hook it up to 16 digital outputs on my Arduino Due, no problem, but maybe there are smarter options.

It would take 19 pins to drive the DAC. I need a lot of pins for the ADC's too, but I think it would be acceptable.

However, if I can use a shift register without adding "loop-time", that would be fine too.



Title: Re: 16bit DAC parallel bits, AD669
Post by: BulletMagnet83 on Feb 24, 2013, 03:56 pm
Why not use a serial DAC instead? I'm pretty sure you can get some reasonably fast single channel ones and you might find it easier to implement. If you can't find one in a package you like then there's always the option of a breakout board.
Title: Re: 16bit DAC parallel bits, AD669
Post by: robtillaart on Feb 24, 2013, 04:00 pm
you might use a couple of shift registers - http://arduino.cc/en/Tutorial/ShiftOut - to control it with 3 lines.
Title: Re: 16bit DAC parallel bits, AD669
Post by: nickgammon on Feb 24, 2013, 10:34 pm
You could use a couple of 595 shift registers:

http://www.gammon.com.au/forum/?id=11518

But by the time you have bought those you may as well buy an SPI DAC chip.
Title: Re: 16bit DAC parallel bits, AD669
Post by: Grumpy_Mike on Feb 24, 2013, 10:46 pm
Quote
if I can use a shift register without adding "loop-time", that would be fine too.

What do you mean by loop time?

You an always use a MCP23S17, this is an SPI 16 bit port expander, that can run quite fast. How fast do you need?
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 24, 2013, 10:52 pm
If you use shift registers it will be a bit slower than writing 2 8-bit ports. But what do you need for speed? The difference is a small fraction of a millisecond.

Title: Re: 16bit DAC parallel bits, AD669
Post by: nickgammon on Feb 25, 2013, 12:38 am
I withdraw my suggestion about the 595 register, that is an output one. But something like the 74HC165 will do input.
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 25, 2013, 12:58 am
Some of these are even bi-directional:
http://www.futurlec.com/cgi-bin/search/search.cgi?search=shift_register&search_base=0&page_no=1

I don't know those people but I have bought from them a few times and never a problem with an order. Obviously though, you don't just buy 1 or 2 50-cent chips on a $4+ shipping charge. But they have component bags and all kinds of goodies. The prices are close enough to eBay for me and these guys do have datasheets so I know what I'm ordering, not just hoping it is what it seems.

(ships from Hong Kong, there's a how-long table)

Quote
SHIPPING METHODS
For Australian, Canada, UK, EU and USA customers, we can offer domestic postage rates as follows,

USA, Canada, UK, European Union(EU) and Australia

   Standard Post*
   For orders up to US$29, Delivery Charge - US$4.00.
   For orders US$30 to US$49, Delivery Charge - US$6.00
   For order US$50 to US$99, Delivery Charge - US$9.00
   For order US$100 and above, Delivery Charge - US$14.00

   Express Post - Express Pak Only
   For orders up to US$99, Delivery Charge - US$22.00.
   For order US$100 and above, Delivery Charge - US$44.00
   For orders shipped in Cartons, Express Charges are charged at actual cost.

   Courier - Courier Pak Only
   For orders up to US$99, Delivery Charge - US$33.00.
   For orders US$100 and above, Delivery Charge - US$66.00
   For orders shipped in Cartons, Courier Charges are charged at actual cost.


All Other Countries

   Standard Post - Air Mail*
   For orders up to US$29, Delivery Charge - US$5.00.
   For orders US$30 to US$49, Delivery Charge - US$7.00
   For order US$50 to US$99, Delivery Charge - US$12.00
   For order US$100 and above, Delivery Charge - US$18.00

   Express Post - Express Pak Only - Where Available
   For orders up to US$99, Delivery Charge - US$28.00.
   For order US$100 and above, Delivery Charge - US$55.00
   For orders shipped in Cartons, Express Charges are charged at actual cost.

   Courier - Courier Pak Only
   For orders up to US$99, Delivery Charge - US$45.00.
   For orders US$100 and above, Delivery Charge - US$75.00
   For orders shipped in Cartons, Courier Charges are charged at actual cost.



Hmmmm, after the USPS charges thread I have to wonder if the site needs updating or they subsidize the difference. And heyyy, ship to Down-Under is the same as to the US!
Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 27, 2013, 01:26 pm
Ok, I think I got it up and running. I now need to write some code to transform a value of desire to 16 parallel bits. I have enough digital output pin available so I would like to use just the 16 output pins for the data.

Any suggestions?

I have looked at bitRead. That seems to work but puts out a value of 1 or 0 while ideally, I want a LOW or HIGH.

for instance, for bit 0, I would like to output;

I did not expect this to work but it does;
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 27, 2013, 02:04 pm
Here's you pin map:
http://arduino.cc/en/Hacking/PinMappingSAM3X

You have Ports A, B, C, D with pins as SAM3X pin names PA##, PB##, etc.
If you can get 16 pins in a row on the same port then you should be able to write 16 bits to that port in one go. Once the bits are written however you do it, the pins should reflect what you wrote. It's a lot quicker than setting 16 pins 1 at a time.



Title: Re: 16bit DAC parallel bits, AD669
Post by: graynomad on Feb 27, 2013, 02:22 pm
Quote
I can hook it up to 16 digital outputs on my Arduino Due, no problem, but maybe there are smarter options

I read the entire thread before I realised you're using a Due, just hook up 16 (or 19) pins and learn how to directly write to a port. Job done, there's no "better" way unless you're desperate to save pins for another purpose.

______
Rob
Title: Re: 16bit DAC parallel bits, AD669
Post by: Grumpy_Mike on Feb 27, 2013, 03:08 pm
Quote
That seems to work but puts out a value of 1 or 0 while ideally, I want a LOW or HIGH.

So what is the difference between a 1 and a high?
I will tell you, nothing at all.
See it is ideal!
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 27, 2013, 03:36 pm

Quote
That seems to work but puts out a value of 1 or 0 while ideally, I want a LOW or HIGH.

So what is the difference between a 1 and a high?
I will tell you, nothing at all.
See it is ideal!


Once here I was warned that TRUE/FALSE and HIGH/LOW might some day not be 1/0 so my code should not assume that and won't be compatible. I had to LOL since hey, I should live so long! But then that person advocates using C++ Strings on UNO's and not being hardware-centered.

Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 27, 2013, 04:00 pm
Ok, I got it working in "slow"-mode.
I indeed put each individual pin either High or Low. It is more of a proof-of concept than it is the end result. The loop now takes as much a 80-100uS with a analogread and normal digitalwrites (16 of them)
Now I know what kind of signals are required to use the DAC, I  will try to use one of the ports for a shorter looptime.

I will try to use portC to driving the DAC.

"So what is the difference between a 1 and a high?"
In the digitalWrite help it says explicitly it want either HIGH or LOW so I was under the assumption that those two inputs were the only ones accepted.

More to come.
Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 27, 2013, 04:13 pm
For the ADC I will be implementing I will use this code, which speeds up the digitalwrites and reads drastically. I will first try to implement the same type of bitsetting on the DAC and if that works nicely, keep it that way.

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

inline int digitalReadDirect(int pin){
  return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);
Title: Re: 16bit DAC parallel bits, AD669
Post by: Grumpy_Mike on Feb 27, 2013, 05:59 pm
Quote
Once here I was warned that TRUE/FALSE and HIGH/LOW might some day not be 1/0 so my code should not assume that and won't be compatible. I had to LOL since hey, I should live so long! But then that person advocates using C++ Strings on UNO's and not being hardware-centered.

Yes but two things, one that is rubbish, two he was talking about a chip outputting something, so that means a physical output. Lets see the guardians of C redefine a TTL output.

Quote
In the digitalWrite help it says explicitly it want either HIGH or LOW so I was under the assumption that those two inputs were the only ones accepted.


As an engineer once told be assumption is the mother of all **** ups.

Quote
For the ADC I will be implementing I will use this code,

Won't that only work with 8 bits?
Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 27, 2013, 06:08 pm
I have not tried it yet with more than 4 pins. For the ADC I will read 1 byte per ADC. When I put "byte" high, the lowbyte and highbyte flip so per ADC I can read the 16 bits with "just" 8 inputs. Therefore I can use 2 ADC's and a DAC without running out of pins.
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 27, 2013, 09:20 pm

Quote
Once here I was warned that TRUE/FALSE and HIGH/LOW might some day not be 1/0 so my code should not assume that and won't be compatible. I had to LOL since hey, I should live so long! But then that person advocates using C++ Strings on UNO's and not being hardware-centered.

Yes but two things, one that is rubbish, two he was talking about a chip outputting something, so that means a physical output. Lets see the guardians of C redefine a TTL output.


I'm not sure that person is a he but was talking about source code. I called it garbage too. But perhaps that person doesn't recognize the relation of bits to wire states as anything but incidental or thinks that code now should be compatible to some future age where binary logic is outdated and the crass obfuscation of using tricky operations like XOR will die with the ancient ones who lived close to the metal.
I've seen it in new compsci degree holders back since 87 but we're still here and LOW is still 0 and HIGH is still 1 without any sign of a big change, and XOR is still useful and used!
Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 28, 2013, 02:30 pm
Ok, now the loop takes 12.5 us. So every 12.5us, a new value is put into the dac and outputted. I think I can reduce this period by smarter bitsetting, but I don't know how to do best. Ideally I want to stick to the pins I use now, 30-45, so not an unique port. I expect the for loop to take most of the time.

Code: [Select]

//Definieer digitalwrite en digitalread-direct

inline void digitalWriteDirect(int pin, boolean val){
  if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
  else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

inline int digitalReadDirect(int pin){
  return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);


}


int update = 8;

//trim DC output to 0, depends on resistor specs and board layout
int offset = 23;

//Halfway ideal value 2^16 / 2 + the offset adjustment
int zero = 32768+offset;

//2^16 / 20 volts is value per volt
float scale = 3276.8;

//DAC reference, what should the output be? In volts.
float input = 0.1;

//Define the value which is send to the DAC, with scaling and offset.
int value = 0; //Value to DAQ;

void setup() {               
 
 
  //Define DAC PINS
  pinMode(update, OUTPUT);
  digitalWrite(update, LOW);
  pinMode(A0, INPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(43, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(45, OUTPUT);
                         
 
}

// the loop routine runs over and over again forever:
void loop() {

//Put value out to DAC

//Determine integer to DAC
value = round(zero+input*scale);

//Put DAC pins high 
for(int x = 30; x < 46; x++){digitalWriteDirect(x, bitRead(value,x-30)); }

//Update DAC output value
digitalWriteDirect(update, HIGH);
digitalWriteDirect(update, HIGH);
//Fix outpur value
digitalWriteDirect(update, LOW);
   
}
Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 28, 2013, 02:39 pm
Get rid of the floats and it will speed up.

scaling integers ---- scaled_value = value x scale_numerator / scale_denominator

don't forget to use an integer type that will hold intermediate results.


Title: Re: 16bit DAC parallel bits, AD669
Post by: SuperR on Feb 28, 2013, 03:38 pm
I took apart the for loop and gained another us. After that, I tested the time it actually takes to put the pins high by copying the 16 lines of code and pasted it below. The loop now takes 5 us more time. So that is the most amount of time I can gain by smarted output writing.

Concerning the float;
The signal that will be output by the DAC will be the outcome of some equations which may vary anywhere between -10 V and +10 V. If using integers really speeds up the progress I will keep that in mind when I built de code which does the calculations.

I think the DAC is going to be ok. Now it's time for the ADC, but I am waiting for some logc level converters before I can use the DUE for that purpose.
Title: Re: 16bit DAC parallel bits, AD669
Post by: robtillaart on Feb 28, 2013, 08:20 pm
Quote
Get rid of the floats and it will speed up.
scaling integers ---- scaled_value = value x scale_numerator / scale_denominator
don't forget to use an integer type that will hold intermediate results.


biggest step can be gained by removing float divisions as these are the worst.

Volts = analogRead(A0) * 5.0/1024;
performs worse than
Volts = analogRead(A0) * 0.004882812;

but yes removing floats completely gains even more.

Title: Re: 16bit DAC parallel bits, AD669
Post by: GoForSmoke on Feb 28, 2013, 09:03 pm
Integer scaling is done completely with integers. The order of operations, * before / preserves the most accuracy.
If I want to ensure 3 places extra accuracy I might multiply by an extra 1000 that I don't divide by and treat the result as 1000x bigger. Like in printing I would insert a decimal point before the last 3 digits in text.

Pick your working units to suit your methods and leave what the humans see have it's own routines.