Offline
Newbie
Karma: 0
Posts: 26
|
 |
« on: December 21, 2012, 05:44:27 am » |
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. #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)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #1 on: December 21, 2012, 05:50:06 am » |
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
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #2 on: December 21, 2012, 06:44:40 am » |
You know a better way?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #3 on: December 21, 2012, 06:48:20 am » |
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)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #4 on: December 21, 2012, 07:08:35 am » |
You know a better way?
Basically there is no generic way of telling if an SPI device is connected correctly.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #5 on: December 21, 2012, 11:50:15 am » |
Ok, can you tell me how to get the output to serial when i send something?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #6 on: December 21, 2012, 12:21:29 pm » |
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
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #7 on: December 21, 2012, 12:44:18 pm » |
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)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #8 on: December 21, 2012, 01:05:24 pm » |
Serial.println(x); // prints hello with ending line break I think you will find it does not print hello.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #9 on: December 21, 2012, 01:11:33 pm » |
indeed i screwed up
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #10 on: December 21, 2012, 01:39:02 pm » |
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. // 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
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #11 on: December 21, 2012, 02:09:05 pm » |
transferAndWait ('0x0000'); // add command Why is that value in quotes? 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
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #12 on: December 21, 2012, 02:48:14 pm » |
Im trying to hook up this 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)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #13 on: December 21, 2012, 04:19:38 pm » |
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:- 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
Newbie
Karma: 0
Posts: 26
|
 |
« Reply #14 on: December 21, 2012, 05:05:32 pm » |
Power and wiring is no problem for me, its the coding that is my problem.
|
|
|
|
|
Logged
|
|
|
|
|
|