SPI Sync Problem

Hi,

Can somebody help me?

I try to communicate with the V2Xe digital compass but I have problem with the synchronization when I use SPI master-slave method.
The V2Xe act as an slave. You send commands to the device and get sesponse but.

For example this piece of code.

// Written by Reinier Pechler
// 31 Januari 2013

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

// used pins
#define SS 10 // SPI "slave select" pin. active low. (same as the analog pin 0, used in digital)
#define SYNC_PIN 7 // Sync resets the compass' communication buffers on raising front
#define kSyncChar 0xAA
#define kTerminator 0x00

char a[8];

enum{
// commands/frame types
kGetModInfo = 1, // 0x01
kModInfoResp, // 0x02
};

void setup (void)
{
Serial.begin (115200);
Serial.println ();
SPI.begin ();

pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(SS, OUTPUT);

digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(SS, HIGH);

pinMode(SYNC_PIN, OUTPUT);
// 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

// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
delay(10);

// sync the compass
v2xe_sync();

} // end of setup

void v2xe_sync(){
digitalWrite(SYNC_PIN, LOW);
delay(10);
digitalWrite(SYNC_PIN, HIGH);
delay(10);

}

byte transferAndWait (const byte what)
{
byte a = SPI.transfer (what);
delayMicroseconds (10);

return a;
}

bool v2xe_check_module(){
bool ok = false;

// enable Slave Select
digitalWrite(SS, LOW);
delay(1);

for (int i = 0; i< 50; i++){
if (kSyncChar == transferAndWait (0)) {
ok = true;
break;
}
}
Serial.println (ok);

if (ok) {
// receive the command
if (kModInfoResp == transferAndWait (0)) {
for (int i = 0; i< 8; i++){
a = transferAndWait (0);
}
}
}

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

return ok;
}

void loop (void)
{

digitalWrite(SS, LOW);
delay(1);
// transmit the command
transferAndWait (kSyncChar); // all frames always start with a sync character
transferAndWait (kGetModInfo); // the frame type
transferAndWait (kTerminator); // don't forget the terminator
// enable Slave Select
digitalWrite(SS, HIGH);
delay(1);

if (v2xe_check_module()) {
Serial.print ("Module Type: ");
Serial.print (a[0]);
Serial.print (a[1]);
Serial.print (a[2]);
Serial.println (a[3]);
Serial.print ("Firmware Version: ");
Serial.print (a[4]);
Serial.print (a[5]);
Serial.print (a[6]);
Serial.println (a[7]);
}
delay(1000);
} // end of loop

I send module info and most of the time I see nothing return.
Please help.

Take these out of void setup, the SPI.begin( ) sets them up.

pinMode(SCK, OUTPUT );
pinMode(MOSI, OUTPUT );

digitalWrite(SCK, LOW );
digitalWrite(MOSI, LOW );

Probably want this Before the SPI.begin( ) also:
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8 );
delay(10 ); << this should not be needed.

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Did you get anywhere with this? Im currently stuck here

Hi
i am facing the same problem kindly help me :~ :.

Is this a class exercise?

Read this before posting a programming question

Your code, sherwani91 and AhmedQ?

Inside code tags, thanks.

Using '#define' to declare numeric constants is bad practice. Declare them as 'const int' or 'const byte' etc. instead. Had you done so, the compiler would have made you aware that SS is already declared. In fact it is the name of the pin that has to be used as the SS pin when the hardware SPI is used in slave mode, and has to be configured as an output when the SPI hardware is used in master mode (even if you don't actually use it for communication with the slave).

Hi Nick Gammon
Thanx for reply i am new to arduino honestly its not even my code + it doesnot compile due to multiple functions of void type as arduino requires void setup(){} i have studied its (v2xe electronic compass)datasheet & found that it works on SPI prototcol so i studied it too but still dont know how to program the code for it so i searched for it & found a code that doesnt even compile :frowning:
i have attatched the file as the message length exceeded linits

v2xe cod.txt (10.6 KB)

Add them and it compiles:

 void setup ()
   {
 
   }  // end of setup
 
 void loop ()
  {
   
  }  // end of loop

Got the datasheet on the part finally
Looks pretty benign to work with - just some SPI commands.
Desribes its SPI operation pretty well even.
I don't have time to draft it out tonight.

V2Xe User Manual - May 2012.pdf (497 KB)

Hi
i am working on haversine formula but i am facing problem regarding accuracy
see the Gps coordinates comes in decimal value i convert them to Deg'min'sec'' & Deg'min'sec'' to radians(for haversine formula)like this

 float latitude= radians(gpslatitute)
float longitude=radians(gpslongitude)

now for accuracy i want the radians value in 9 to 8 decimal places like 1.346563267 but the value which is entering in haversine formula is 1.34 due to which i am unable to calculate accurate distance for the data type i tried using float ,long, double but still problems remains the same =(

Are you certain that the value entering the haversine formula is 1.34? If you are printing it, then the print function you are using may be rounding to 2 decimal places.

yes i am sure for that reason i implemented & checked it with that value

I suggest you post the code you are using.

I think you forgot something here. Maybe an array index?

// this is a global 8 character array
char a[8];

      // but later you use this
      for (int i = 0; i< 8; i++){
       a = transferAndWait (0);
     }

I think you meant this:

       a[i] = transferAndWait (0);
        for (int i=2;i<3;i++){

So, while i is exactly 2, huh? What is the point of the loop?


        for (int i=4;i<5;i++){

Now while i is exactly 4?


  byteGPS=Serial.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
    delay(100); 
  } 
  else {
    linea[conta]=byteGPS;        // If there is serial port data, it is put in the buffer

This is completely wrong. Waiting 1/10 of a second does not give you serial data.

The post containing the code seems to have vanished - presumably it was the one before Nick's reply?

Perhaps I should just ban him?

How to use this forum