Problem with SPI

I'm trying to use an Arduino UNO to programme a DAC via SPI, using the Arduino SPI library. I was having problems getting my code working so I hooked up a 'scope to the clock and data pins of the SPI bus. I noticed that if I just ran my code once then I got junk out of the SPI bus. In particular, rather than getting a clock pulse for each bit of my bytes, I would get a single, long clock pulse for each byte. I found that if I put my code in a loop so it repeatedly sent data over the SPI port then the Arduino would begin by sending junk however, after a second or so, it seemed to sort itself out and behave nicely.

Naturally, I assumed that this was something to do with my code, so I copied various bits of example code from the web. But always found this same behaviour. I also (for the sake of paranoia) swapped in a fresh Arduino to check for faulty hardware. Is there something I'm missing here? Is this known behaviour for the SPI library?


The "one long clock pulse for a byte" sounds like the Slave Select line rather that the Serial Clock line. Are you sure you were looking at the right pin?

If you can provide a pointer to the DAC specification and your code, perhaps someone can see what is going wrong.

Here is a SPI sketch I wrote to test out a D/A module. Check to see if your code is missing something.

// Continous ramping from 0-5vdc analog output on 2 channel DAC module based on MCP4922 12 bit DAC device
// Hardware SPI version
// uses pin 13 (or 52) for SCK, pin 11 (or 51) for MOSI, pin 10 (or 53) for SS
// outputs both channels 0-5vdc in continous loop
// leftyretro 3/31/11

#include <SPI.h>
#include "pins_arduino.h"  // defines hardware SPI pins to use

const char channelA =  0;  // module has two independent D/A outputs, A & B avalible
const char channelB =  1;

void setup() {
 SPI.begin();  // initialize hardware SPI: uses pin 13 (or 52) for SCK and pin 11 (or 51) for MOSI
               // pin 10 (or 53) for slave select, SS 
void loop()
   for (int i=0; i < 4096; i++)   // DAC 12 bit count value range is 0-4095
    dacWrite(channelA, i);
    dacWrite(channelB, i);

// MCP4922 Write Data 
// *********************

void dacWrite(char DAC_Channel, int DAC_Data)	  	
  switch (DAC_Channel)														
    case 0x00: DAC_Data |= 0x3000;   //selects channal A, gain = 1, + 12 bit DAC value										
    case 0x01: DAC_Data |= 0xB000;   //selects channel B, gain = 1, + 12 bit DAC value										
  digitalWrite(SS,LOW);  //enable Slave Select  SS is pin 10 (or 53)  
  SPI.transfer(highByte(DAC_Data)); // shift out high 8 bits
  SPI.transfer(lowByte(DAC_Data));  // shift out low 8 bits
  digitalWrite(SS,HIGH); // disable Slave Select, DAC value is transfered and latched in module   	  


Thank you both for the comments. I hadn't realised that when you open a connection to an Arduino from a computer it resets itself and runs the boot loader. The junk I was seeing on the SPI clock line was just it resetting itself and initialising the lines.