Let's Make Robots!

SPI problem

Hello guys. I'm trying to make an spi connection between the arduino and an IC. Actually it's not an arduino, it's an atmega328 on a breadboard, but i'm programming it with the arduino IDE through ICSP. I've got a problem, this sketch doesn't work:

void setup(){

byte clr; SPCR=B01010011;



digitalWrite(10, LOW);  //10 is basically the SLAVE SELECT pin   

spi_transfer(B00000100); //powerup delay(10);

digitalWrite(10, HIGH);  

digitalWrite(10, LOW);

spi_transfer(B00000000);  //setrec


digitalWrite(10, HIGH);   


digitalWrite(10, LOW);   

spi_transfer(B00001010); //STOPPWRDN com

digitalWrite(10, HIGH);   


void loop(){}

void spi_transfer(volatile char data)


while(!(SPSR & (1<<SPIF)))

{}; }


Never mind about the commands, i just wanted to focus your attention on the spi_transfer function.

Curious thing is that the same program on picaxe works (of course the commands are different: hspiout instead of spi_transfer). I've also tried the SPI library and the shiftOut command and they don't work, so i'm starting to think it is a problem related to something else, something like the fact that my chip doesn't have the bootloader (i don't know why that would matter though, but i have no more ideas!) or the fact that i have to set fuse bits in some strange way to make SPI work. Any ideas?

Thank you!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Some suggestions:

  • I'm missing the SPI ports pins initialisation. MOSI, SCLK and SS must be set as output.
  • What is the clock rate of your processor. Arduino is running at 16MHz. This would give a SPI clock of 125kHz (16MHz/128) for your setup.
  • Are the fuses set correct for the clock source? A new ATMega is running with 1MHz and internal clock.
  • The ISP uses some of the SPI pins. You may get problems when both the ISP programmer and your SPI hardware is connected to the controller. Vice versa your ISP programmer maybe get problems during programming.
  • Put some delay after writing SPCR register before writing to SPI
  • Using a Bootloader or not should not be important

Nothing. :=(

I've set the pins as output, i've set the fuses the same way as they are set on the arduino duemilanove:




i've disconnected the iscp programmer while making the program run, i've added a 50ms delay after writing to the SPCR. I don't know what's wrong. You can notice that there is activity though, because when you connect the wires to SS, MOSI and SCLK you hear noise on the speaker (my project involves the use of a speaker). 

When i made it work with picaxe i was using SPI at 63KHz, but i do not know how to set that speed with the arduino if i want to keep my 16MHz resonator. I think that by setting the last two bits to 1 (of the SPCR) you get the slowest possible speed.


Yes, with 16Mhz the slowest possible SPI speed is 125kHz.

What kind of IC do you want to control? What  does the datasheet say about the used SPI mode?

There are different flavours of SPI. At the moment you are using mode0. Important is the clock polarity and phase between clock and data. Also the way the data where shifted out, LSB or MSB first. Most options can be controlled with the SPCR. Further reading here:




I've read a lot of things about it in the last few days. I've seen a guy make the chip work (an ISD4002) with CPOL and CPHA set to 0. Additionally, i've made the same IC work with picaxe using spi mode 0. As for MSB and LSB i'm 100% sure it is set correctly (because of codes i've found on the internet, and because i've set it the same way as i had for the picaxe). 

I really do not know what to do now. I was thinking of getting an arduino board to see if it would work that way, but that would mean giving up on trying to make it work this way. 

I've found this code example:


One important thing I've found in the datasheet is to use a delay of 50ms after the power command. Before writing any other commands to the chip.

you won't probably believe me but that's one of the first sketches i tried to run on the atmega....and you know the result. :=(

..and yes i've used the 50ms pause in every one of my sketches. I decided i'm gonna get an arduino and then i'll let you know. By the way, i'm starting to think (well i was already thinking about it) that it is an hardware problem, even if i've connected the atmega the same way as described in the datasheet. MOSI, SCK and SS get connected directly to the same pins on the ISD, no pullups/pulldowns no diodes no nothing.

Everything works fine on the arduino duemilanove. So the problem is with the breadboard, quite strange, because most other sketches work fine (serial transmission, making LEDs blink).