SPI 32 bits output.

Hi Guys!!

I am having some problems getting 32bits.

Any Idea how do i code it? I need a straight 32bits instead of 4x8bits.

Please advise!

Thank You!

Is there a difference between 32 bits and 4*8 with SPI.transfer ?
What kind of chip is it ?

I need a straight 32bits instead of 4x8bits.

What's the difference?, except a slight pause between bytes that should not matter to anything.

Anyway the answer is no, you cannot do a 32-bit shift on an 8-bit CPU using the SPI hardware. You can if you write a modified version of shiftOut().


Rob

Can do the SPI transfers really fast, will not see any break.
Set SPI divisor to 2 (8 MHz transfers)
Then:

// top of your sketch:
#include <SPI.h>

#define nop asm volatile ("nop")

// this in loop () to send the data out
digitalWrite (ssPin, LOW);
SPDR = array[0]; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
SPDR = array[0]; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
SPDR = array[0]; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
SPDR = array[0]; nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;
digitalWrite (ssPin, HIGH);

Hi Guys,

Thanks for the replies!

May I know how do I modify from this to transfer faster to not see any break?

Thanks!

unsigned long Transfer(unsigned long value)
{
FourByte data = {value};
for(byte i = 0; i < 4; i++)
{
data.bit8 = SPI.transfer(data.bit8*);*
* }*
* return data.bit32;*
}
void loop()
{
* unsigned long address = 0x00100000; //swap*

digitalWrite(53, HIGH); //output SEN high

Transfer(address);

digitalWrite(53,LOW); // Set SEN LOW once 32bits is sent

}

CODE TAGS please. And show all the code.

how do I modify from this to transfer faster to not see any break?

Well Crossroads gave one option. Your function looks a little sus but we need to see ALL the code, how do we know what a "FourByte" is?


Rob

Hi Guys!

Sorry This is the full code!

This can output 4x8bits.

#include <SPI.h> // Links prewritten SPI library into the code

union FourByte
{
unsigned long bit32;
unsigned int bit16[2];
unsigned char bit8[4];
};

void setup()
{

pinMode(53, OUTPUT); // Set SPI pins to be outputs (slave select)
pinMode(52, OUTPUT); // clock
pinMode(51, OUTPUT); // Data
digitalWrite(2, HIGH); // Set Arduino pull-up resistors active
digitalWrite(3, HIGH); // This sets an internal to the chip
digitalWrite(4, HIGH); // pull-up resistor on so an unconnected pin
digitalWrite(5, HIGH); // is reliably at logic 1
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(53, HIGH); // output High SEN
pinMode(52, HIGH);
pinMode(50, HIGH);
digitalWrite(11, HIGH);
digitalWrite(13, HIGH);
SPI.begin(); // Initialize SPI parameters
SPI.setBitOrder(MSBFIRST); // MSB to be sent first
SPI.setDataMode(SPI_MODE0); // Set for clock rising edge
SPI.setClockDivider(SPI_CLOCK_DIV2); // Set clock divider (optional)
digitalWrite(53, LOW);

}

unsigned long Transfer(unsigned long value)
{
FourByte data = {value};
for(byte i = 0; i < 4; i++)
{
data.bit8 = SPI.transfer(data.bit8*);*
* }*
* return data.bit32;*
}
void loop()
{
* unsigned long address = 0x00100000; //swap*

digitalWrite(53, HIGH); //output SEN high

Transfer(address);

digitalWrite(53,LOW); // Set SEN LOW once 32bits is sent

}

And the CODE tags? Without them the code loses important details. (click on the # button in the toolbar when editing the post)


Rob

Can this work?

#include <SPI.h>  //  Links prewritten SPI library into the code

union FourByte
 {
    unsigned long bit32;
    unsigned int bit16[2];
    unsigned char bit8[4];
};

void setup()
{

  pinMode(53, OUTPUT);                //  Set SPI pins to be outputs (slave select) 
  pinMode(52, OUTPUT);                //  clock
  pinMode(51, OUTPUT);                // Data
  digitalWrite(2, HIGH);                   //  Set Arduino pull-up resistors active
  digitalWrite(3, HIGH);                   //  This sets an internal to the chip
  digitalWrite(4, HIGH);                   //  pull-up resistor on so an unconnected pin
  digitalWrite(5, HIGH);                   //  is reliably at logic 1
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(53, HIGH);   // output High SEN
  pinMode(52, HIGH);                
  pinMode(50, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(13, HIGH);
  SPI.begin();                                                           //  Initialize SPI parameters
  SPI.setBitOrder(MSBFIRST);                             //  MSB to be sent first
  SPI.setDataMode(SPI_MODE0);                      //  Set for clock rising edge
  SPI.setClockDivider(SPI_CLOCK_DIV2);    //  Set clock divider (optional)
  digitalWrite(53, LOW); 

}


unsigned long Transfer(unsigned long value)
{
  FourByte data = {value};
  for(byte i = 0; i < 4; i++)
  {
    data.bit8[i] = SPI.transfer(data.bit8[i]);
  }
  return data.bit32;
}

void loop()
{
  unsigned long address =  0x00100000;  //swap
 
  
 digitalWrite(53, HIGH); //output SEN high
  
 Transfer(address);
 
 digitalWrite(53,LOW);    // Set SEN LOW once 32bits is sent 
 

}

I think that will work, but why the union?

I need a straight 32bits instead of 4x8bits.

What happened to this?


Rob

Somehow my chip need to read it in a string. Is it possible and how to change my code in order to enable lesser delay between each 8bits.

Yes - get rid of the for:loop here:

unsigned long Transfer(unsigned long value)
{
  FourByte data = {value};
  for(byte i = 0; i < 4; i++)
  {
    data.bit8 = SPI.transfer(data.bit8);
  }
  return data.bit32;
}

Each pass thru the loop adds ~12uS break between transfers.
Get rid of SPI.transfer - the library waits for an interupt to come back before it sends the next byte.
Put your data into an array and send it out like I did.

aboveall:
Somehow my chip need to read it in a string. Is it possible and how to change my code in order to enable lesser delay between each 8bits.

somehow my chip need to read it in a string.

What chip is this? I've never seen one that would object to a small delay between bytes, that's one of the points about a synchronous protocol. So

somearray[0] = SPI.transfer (somearray[0]);
somearray[1] = SPI.transfer (somearray[1]);
somearray[2] = SPI.transfer (somearray[2]);
somearray[3] = SPI.transfer (somearray[3]);

should be all you need, or if you really need no break use Crossroad's example.

I assume you are also receiving data from the device? Otherwise there's no point saving the return value from transfer().


Rob

I need a straight 32bits instead of 4x8bits.

You have an 8-bit computer. Anything that you do with your 32 bit value, will be processed as 4x8 bits.

You entire problem seems to be wholly misconceived.

Hi,

so I would just need to remove the for loop? And input this?

omearray[0] = SPI.transfer (somearray[0]);
somearray[1] = SPI.transfer (somearray[1]);
somearray[2] = SPI.transfer (somearray[2]);
somearray[3] = SPI.transfer (somearray[3]);

How do I go about setting the hex address in each array?

I would track them as 4 bytes.
You could maintain it as a long, unsigned long, with any number of ways to break it into 4 bytes for sending.

What HEX address?

And you haven't answered any of my previous questions. They are the words before a ?


Rob

Hi,

Sorry, as I said I need as less delay as possible. What do you mean word before a?

Cross roads: May I know how you write them? Like if I need this string 0x010000001

Thanks! Sorry I am very new to this.

aboveall:
What do you mean word before a?

You had a good example of this on the next line when you wrote:-

May I know how you write them?

The words before the ? were:-

May I know how you write them

It was a question.
? is called a question mark, mainly because it marks a question.

Help pls!

Thanks!