Pages: [1]   Go Down
Author Topic: Using the 'short' data type in Arduino  (Read 2453 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.

J
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 17, 2009, 06:38:03 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Boston, MA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1024
wiblocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Appreciated.
Logged

Boston, MA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1024
wiblocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 *)

Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: