Pages: [1]   Go Down
Author Topic: Does this work  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is a modified example from Gammon's code.
Just wanna know if this works, to test if an spi device is hooked up correctly.

Code:
#include <SPI.h>


void setup (void)
{

  Serial.begin(9600);
  digitalWrite(SS, HIGH);  // ensure SS stays high for now
  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();

  // Slow down the master a bit
  SPI.setClockDivider(SPI_CLOCK_DIV8);
 
}  // end of setup


void loop (void)
{

  char c;

  // enable Slave Select
  digitalWrite(SS, LOW);    // SS is pin 10

SPI.transfer (0xCD);  // are you there?
byte x = SPI.transfer (0); // get response

if (x == 0xEF)
  {
  Serial.println("SPI correctly hooked up");  // prints hello with ending line break
  }

  // disable Slave Select
  digitalWrite(SS, HIGH);

  delay (1000);  // 1 seconds delay
}  // end of loop
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to know that your device will respond with an 0xef when you send it an 0xce, most devices will not respond like this.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You know a better way?
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Send the device something it actually expects, and check the return for the predicted value.  Studying the data sheet for the device is the first step.

-br
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You know a better way?
Basically there is no generic way of telling if an SPI device is connected correctly.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, can you tell me how to get the output to serial when i send something?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok, can you tell me how to get the output to serial when i send something?
Not without a lot more context. What are you sending? To what? From what?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, i mean now it sends 0xCD true SPI to the slave and if it gets 0xEF back it sends a serial message.
Now i want to send 0xCD true SPI to the slave, and i want to send the respons from the slave as a serial message so i can check the output.

Edit: Never mind, did it like this:
byte x = SPI.transfer (0); // get response

 
  Serial.println(x);  // prints hello with ending line break
« Last Edit: December 21, 2012, 12:48:08 pm by Tyger » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Serial.println(x);  // prints hello with ending line break
I think you will find it does not print hello.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

indeed i screwed up
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the dubble post, i used gammons example with a command below, but i just wanna check if this is correct or my wiring is wrong.

Code:
// Written by Nick Gammon
// April 2011


#include <SPI.h>
#include "pins_arduino.h"

void setup (void)
{
  Serial.begin (115200);
  Serial.println ();
 
  digitalWrite(SS, HIGH);  // ensure SS stays high for now

  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();

  // Slow down the master a bit
  SPI.setClockDivider(SPI_CLOCK_DIV8);
 
}  // end of setup

byte transferAndWait (const byte what)
{
  byte a = SPI.transfer (what);
  delayMicroseconds (20);
  return a;
} // end of transferAndWait

void loop (void)
{

  byte a, b, c, d;
 
  // enable Slave Select
  digitalWrite(SS, LOW);   

  transferAndWait ('0x0000');  // add command
  transferAndWait (10);
  a = transferAndWait (17);
  b = transferAndWait (33);
  c = transferAndWait (42);
  d = transferAndWait (0);

  // disable Slave Select
  digitalWrite(SS, HIGH);

  Serial.println ("Adding results:");
  Serial.println (a, DEC);
  Serial.println (b, DEC);
  Serial.println (c, DEC);
  Serial.println (d, DEC);
 
  // enable Slave Select
  digitalWrite(SS, LOW);   

  transferAndWait ('s');  // subtract command
  transferAndWait (10);
  a = transferAndWait (17);
  b = transferAndWait (33);
  c = transferAndWait (42);
  d = transferAndWait (0);

  // disable Slave Select
  digitalWrite(SS, HIGH);

  Serial.println ("Subtracting results:");
  Serial.println (a, DEC);
  Serial.println (b, DEC);
  Serial.println (c, DEC);
  Serial.println (d, DEC);
 
  delay (1000);  // 1 second delay
}  // end of loop
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  transferAndWait ('0x0000');  // add command
Why is that value in quotes?

Quote
Just wanna know if this works, to test if an spi device is hooked up correctly.
Did you go to mouser and buy a "generic SPI device"? Or do you have some specific SPI device in mind?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im trying to hook up this
Code:
http://ww1.microchip.com/downloads/en/devicedoc/70590c.pdf
And im a noob at arduino coding, did not notice that.
« Last Edit: December 24, 2012, 11:08:24 am by Tyger » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First off the device is a 3V3 device, so are you powering it with this voltage?
What have you done about the signals from the arduino, these are 5V and will need converting to 3V3.
This is a complex chip, it uses 16 bit words to communicate. The arduino SPI uses 8 bits so you have to use two transfers to get anything into the chip. Page 42 shows a register map with a value on power up or reset shown in the last column. I would try and read these and see if you get the same values.
Page 18 tells you what commands to send to read each register.

It also says:-
Quote
In general, MRF49XA registers are read only. Hence the chip status can only be read by the Status Read Register. During write, only appropriate byte is written to the desired register. It is not desired to read/write all registers and there is no way to read back the register.
So your technique of writing something and reading it back is not going to work.
« Last Edit: December 21, 2012, 04:27:30 pm by Grumpy_Mike » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Power and wiring is no problem for me, its the coding that is my problem.
Logged

Pages: [1]   Go Up
Jump to: