Go Down

Topic: Using the 'short' data type in Arduino (Read 2730 times) previous topic - next topic


I'm playing round with some code to run a TLV5618 DAC via Arduino.

It's working fine - but it uses the 'short' data type that (I think) is based on C.

It's used a few times in function declarations - e.g.

Code: [Select]
void setDacAVoltage (short inVoltage) {
 setDacVoltage (inVoltage, 1);

I can't find an explanation for this data type in the Arduino reference - so I'm wondering two things:

1) What exactly is the 'short' data type and 2) Is there a better/more efficient way of writing it in Arduino?

The main reason I'm asking is that I want to try and change the data type for some things these functions are referencing - to try an optimise one part of my code, but don't want to do it before I understand exactly what type of data these functions are looking for now.

Hope that makes sense - thanks in advance.



Dec 17, 2009, 12:34 pm Last Edit: Dec 17, 2009, 12:38 pm by GrooveFlotilla Reason: 1
On the Arduino, "short" is the same as "int".
It's a signed 16 bit quantity, so capable of representing the range -32 768 to +32 767.

IMHO, it's better (more portable) to write "short" which will (almost) always be a 16 bit quantity, whereas "int" depends on the platform.

In fact, probably even better to write "int16" or "int16_t" then there's no argument.
Per Arduino ad Astra


2) Is there a better/more efficient way of writing it in Arduino?

Not sure if it's better or more efficient but I have a C++ DAC library that
supports the TLV5618 and TLV5620 at http://tinyurl.com/y8exe7k
Scroll down to the library links in the sidebar.

(* jcl *)

www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks
blog: http://luciani.org
www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks


Thanks all - that helped.

Groove - the function is being passed an 'int' - so I might change that to 'short' rather than the other way round.

JCL - this is part of JP's MIDI2CV code - which you're familiar with! Like I said, it's working fine (along with Lin to EXP conversion and 2 x 4 way multiplexing now) - just trying to get the code running a bit faster.



I have not looked at JP's code but when doing the code for my numerically controlled
oscillator (NCO) I did notice a major speed improvement when I changed the
Code: [Select]
digitalWrite statements to assignment statements that directly set the bits in the ports. I am thinking of going back to my DAC library and updating it.

Fortunately the NCO only requires integer math.

(* jcl *)

www: http://www.wiblocks.com
twitter: http://twitter.com/wiblocks


I did check out your recent NCO post - it looks great.

Changing the digitalWrite sounds like an option (although it's a bit over my head at the moment).

Yes - the speed improvement I'm looking for is in preparation for getting some waveforms out of the DAC.

Thanks heaps.

Go Up