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.