arduino spi

Hii,

Is it possible to build a spi interface between an arduino uno an a 3v operating device? If yes then how it will communicate , or any external circuit needed. If no then why?

thanks...

You will need to drop the 5v output from MOSI, CLK and SS to 3.3v. The input on MISO should work OK.

For any pins that need it you can use a pair of resistors to make a voltage divider to drop the voltage.

What device are you connecting to? Some 3.3v devices can tolerate 5v on their I/O pins - for example the nRF24L01+ transceivers. Note that the nRF24 MUST have 3.3v on its power pin.

...R

I am connecting tdc7200 timer.

It does not look, from the data sheet, like the pins are 5V tolerant. So you will need the resistors as described by Robin, or you could use one of these logic level adaptor modules.
s-l300 (8).jpg

s-l300 (8).jpg

Will MISO work in 3.3 V?

pAbel: Will MISO work in 3.3 V?

Robin2: The input on MISO should work OK.

Hii

For reading data from the slave device what is the instruction. I am using the below format but getting 0 output only.

SPI.transfer(0x00); \ read command result=SPI.transfer(0);\ store the data in result (result is a variable).

Please post all your code. We’re not Snippets R Us. Please use code-tags while doing so. See How to use the forum please.

Full code

#include<SPI.h>
#define enable 3
#define triger 4
#define start 5
#define stopp 6
#define csb 10
#define intb 7
void setup() {
Serial.begin(9600);
SPI.beginTransaction(14000000,MSBFIRST,SPI_MODE1);
pinMode(enable,OUTPUT);
pinMode(triger,INPUT);
pinMode(start,OUTPUT);
pinMode(stopp,OUTPUT);
pinMode(csb,OUTPUT);
pinMode(intb,INPUT);
digitalWrite(enable,LOW);
digitalWrite(enable,HIGH);
delay(1.5);
}

void loop() {
unsigned long result=0;
unsigned long inbyte=0;
digitalWrite(csb,LOW);
SPI.transfer(0x40);
SPI.transfer(0x83);
if(digitalRead(triger)==HIGH){
digitalWrite(csb,HIGH);
digitalWrite(start,HIGH);
digitalWrite(start,LOW);
delayMicroseconds(20);
digitalWrite(stopp,HIGH);
digitalWrite(stopp,LOW);

if(digitalRead(intb)==LOW){
digitalWrite(csb,LOW);
SPI.transfer(0x11);
result=SPI.transfer(0x00);
Serial.println(result);
delay(500);

}
}

}

Please reread reply #7 and edit your post accordingly.

My code

#include<SPI.h>
#define enable 3
#define triger 4
#define start 5
#define stopp 6
#define csb 10
#define intb 7
void setup() {
 Serial.begin(9600);
 SPI.begin();
 SPISettings mySettings(14000000,MSBFIRST,SPI_MODE1);
 pinMode(enable,OUTPUT);
 pinMode(triger,INPUT);
 pinMode(start,OUTPUT);
 pinMode(stopp,OUTPUT);
 pinMode(csb,OUTPUT);
 pinMode(intb,INPUT);
 digitalWrite(enable,LOW);
 digitalWrite(enable,HIGH);
 delay(1.5);
}

void loop() {
  unsigned long result=0;
  unsigned long inbyte=0;
  digitalWrite(csb,LOW);
  SPI.transfer(0x40);
  SPI.transfer(0x83);
  if(digitalRead(triger)==HIGH){
  digitalWrite(csb,HIGH);
  digitalWrite(start,HIGH);
  digitalWrite(start,LOW);
  delayMicroseconds(20);
  digitalWrite(stopp,HIGH);
  digitalWrite(stopp,LOW);
    

  if(digitalRead(intb)==LOW){
    digitalWrite(csb,LOW);
    SPI.transfer(0x11);
    result=SPI.transfer(0x00);
    Serial.println(result);
    delay(500);
    
  }
  }
  

}

SPI.beginTransaction(14000000,MSBFIRST,SPI_MODE1); Max 8MHz on an Uno.

delay(1.5); Integers only. Leo..

Wawa: SPI.beginTransaction(14000000,MSBFIRST,SPI_MODE1); Max 8MHz on an Uno.

As I understand it, the first parameter of beginTransation() is the maximum SPI speed that the slave device can handle, not the speed the master must use. If the Arduino can't go that fast, it will go as fast as it can, which is 8MHz in the case of Uno. But the tdc7200 chip can handle 20MHz. So it would be more correct to use the value 20000000. It won't make a difference with Uno, it will still go at 8MHz. But if you ran the sketch on a faster "Arduino", such as an esp8266 (which can go up to 80MHz SPI clock), then putting 20000000 would limit the faster master to what the slave can handle.

Put another way, specifying 20000000 will select the fastest speed that the master and the slave can both handle. The beginTransaction() function already knows the speeds the master can handle, based on the board type you select in the IDE. So you are telling it the fastest speed that the slave can handle.

From page 21 of the data sheet:

The communication on the SPI bus supports write and read transactions. A write transaction consists of a single write command byte, followed by single data byte. A read transaction consists of a single read command byte followed by 8 or 24 SCLK cycles. The write and read command bytes consist of a 1-bit auto-increment bit, a 1-bit read or write instruction, and a 6-bit register address. Figure 21 shows the SPI protocol for a transaction involving one byte of data (read or write).

So I think you need to study the data sheet in more depth, your code may be too simple as it stands.

Hii, This is write byte and data byte in code. This is writing 83h in 00h register.

 SPI.transfer(0x40);
  SPI.transfer(0x83);

This is read byte followed by clocks. This is reading from 11h register.

SPI.transfer(0x11);
    result=SPI.transfer(0x00);

The register at 11h is 24 bits long. You are only reading 8 bits.

No No I have not updated here,I have changed that. This is new one.

#include<SPI.h>
#define enable 3
#define triger 4
#define start 5
#define stopp 6
#define csb 10
#define intb 7
void setup() {
 Serial.begin(9600);
 SPI.beginTransaction(20000000,MSBFIRST,SPI_MODE1);
 pinMode(enable,OUTPUT);
 pinMode(triger,INPUT);
 pinMode(start,OUTPUT);
 pinMode(stopp,OUTPUT);
 pinMode(csb,OUTPUT);
 pinMode(intb,INPUT);
 digitalWrite(enable,LOW);
 digitalWrite(enable,HIGH);
 delay(2);
}

void loop() {
  unsigned long result=0;
  byte inbyte=0;
  int readbytes=3;
  digitalWrite(csb,LOW);
  SPI.transfer(0x40);
  SPI.transfer(0x83);
  if(digitalRead(triger)==HIGH){
  digitalWrite(csb,HIGH);
  digitalWrite(start,HIGH);
  digitalWrite(start,LOW);
  delayMicroseconds(20);
  digitalWrite(stopp,HIGH);
  digitalWrite(stopp,LOW);
    

  if(digitalRead(intb)==LOW){
    digitalWrite(csb,LOW);
    SPI.transfer(0x11);
    result=SPI.transfer(0x00);
    readbytes--;
    if(readbytes>0){
      result=result << 8;
      inbyte=SPI.transfer(0x00);
      result=result|inbyte;
      readbytes--;
      
    }
    digitalWrite(csb,HIGH);
    Serial.println(result);
    delay(500);
    
  }
  }
  

}

In you code you never call SPI.beginTransaction() and thus never use your mySettings ;)

What you want to say I am not getting.

Have a look at the SPI reference ;) Especially about the use of beginTransaction() and endTransaction()