0

I am trying to iterate over an int array and writing those values to a chip. Here is my code.

#include <SPI.h>

int sine[] = {0x7fc ,0x868 ,0x8d4 ,0x93c ,0x9a8 ,0xa10 ,0xa78 ,0xadc 
,0xb3c ,0xba0 ,0xbfc ,0xc58 ,0xcb0 ,0xd04 ,0xd58 ,0xda4 ,0xdf0 ,0xe34 ,0xe74 
,0xeb0 ,0xee8 ,0xf1c ,0xf4c ,0xf74 ,0xf98 ,0xfb8 ,0xfd0 ,0xfe4 ,0xff0 ,0xff8 
,0xffc ,0xff8 ,0xff0 ,0xfe4 ,0xfd0 ,0xfb8 ,0xf98 ,0xf74 ,0xf4c ,0xf1c ,0xee8 
,0xeb0 ,0xe74 ,0xe34 ,0xdf0 ,0xda4 ,0xd58 ,0xd04 ,0xcb0 ,0xc58 ,0xbfc ,0xba0 
,0xb3c ,0xadc ,0xa78 ,0xa10 ,0x9a8 ,0x93c ,0x8d4 ,0x868 ,0x7fc ,0x794 ,0x728 
,0x6bc ,0x654 ,0x5ec ,0x584 ,0x520 ,0x4bc ,0x45c ,0x400 ,0x3a4 ,0x34c ,0x2f4 
,0x2a4 ,0x258 ,0x20c ,0x1c8 ,0x188 ,0x148 ,0x110 ,0xe0 ,0xb0 ,0x88 ,0x64 ,0x44 
,0x2c ,0x18 ,0xc ,0x0 ,0x0 ,0x0 ,0xc ,0x18 ,0x2c ,0x44 ,0x64 ,0x88 ,0xb0 ,0xe0 
,0x110 ,0x148 ,0x188 ,0x1c8 ,0x20c ,0x258 ,0x2a4 ,0x2f4 ,0x34c ,0x3a4 ,0x400 
,0x45c ,0x4bc ,0x520 ,0x584 ,0x5ec ,0x654 ,0x6bc ,0x728 ,0x794};

#define samples 120

const int cs = 9;

int counter = 0;

const int chipSelectPin = 10;

void setup()
{
  pinMode(cs,OUTPUT);
  pinMode(chipSelectPin, OUTPUT);

 SPI.beginTransaction(SPISettings(20000,LSBFIRST,SPI_MODE3));
}

void loop()
{



  digitalWrite(cs,LOW);



  SPI.transfer(sine[counter]);

  digitalWrite(cs,HIGH);


  counter += 1;

  if(counter >= samples)
  {
    counter = 0;
  }

}

Very simple, I am using an Arduino nano and according to this reference the clock pin is 13 and MOSI is 11. I am seeing the clock signal for pin 13 but pin 11 is inactive.

2

You need to use SPI.begin() before the beginTransaction().

The documentation is not 100% clear but beginTransaction does not replace SPI.Begin to setup the SPI hardware.

Documentation not very clear: https://www.arduino.cc/en/Reference/SPIbeginTransaction

3

In addition to @Talk2's answer SPI.transfer(...) transfers one byte of data. Your array is made of int which means each entry is 2 bytes long.

Instead you should use SPI.transfer16(...) to transfer 16-bit values.

  • This is something I also wanted to address thank you for pointing it out. – AlanZ2223 Jul 19 '16 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.