SCK and CLK in SPI communication

I am trying to establish communication with a device via SPI with the Arduino Due.

The device is a ATA6870: http://www.atmel.com/Images/doc9116.pdf

The datasheet makes me believe the system requires a system clock CLK of 450-550khz (maximum) and an SCK of half the system clock.

The Due has an 84MHz system clock and the SPI library allows us to divide the clock by 255 maximum. This would give the slowest SPI SCK of approximately 330kHz so in order to meet the half system clock criteria the system clock would need to be 660Hz which is more than its allowed maximum.

I am not confident enough in my knowledge to understand how I can overcome this problem or even if it is in fact a problem!. Any pointers in the right direction would be much appreciated.

Chris

It would seem to me that you are correct: The SPI clock on the ATA6870 is meant to be up to 250KHz. You cannot get down to 250KHz with the master clock running at 84MHz on the Due. This would require a scale value of 336 but you can't go past 255.

The solution, of course, is to slow the master clock down. Cutting it in half to 42MHz would allow you to set an SPI prescaler of 168 and get the 250KHz you need. Now, it sounds easy to do that but of course the Arduino IDE does not expose any way for you to slow down the master clock. So, you're kind of stuck there. You can directly edit the Arduino core files to make the master clock slower but then you're stuck with a specially modified version of Arduino.

An alternative would be to write a sketch to actually mess with the clock settings. So, your sketch could turn the clock down. The problem with doing that is that every other peripheral will also clock down and this will mess up all of the settings for things like UART. Also, I'll bet that the delay routines expect a constant 84MHz as well. God only knows what other things on the board are expecting the 84MHz and will break if they don't get it. In short, a lot of crap will go south for you in a real hurry.

I wrote all that to reinforce my reason for saying this: your best bet is to find a different battery measuring chip. You really want something that can handle a 1MHz or higher clock so that you can drive it without messing with the main clock frequency.

How did you accomplish the impossible task to connect such an impossible package to an arduino?!? :fearful:

You can use a technique called "bit-banging" i which you write the code yourself to drive the SCK, MSIO and MISO lines. THat way you can run the clock at whatever speed you like.
Fortunately the SPI way or working is fairly simply and the code required for bit-banging can be found - it took me about 20 seconds to find some bit-banging SPI code in Wikipedia (http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Example_of_bit-banging_the_SPI_master_protocol).
Of course, you may not way a blocking function and this could fairly easily be converted into an interrupt driven state machine.
Susan