Hi,
I write the code that works only I can’t display the float values correctly. When I change the float in int I see results.
Maybe can somebody help me with this.
I have used a logic level converter. I don’t know if it is working when you connect the V2Xe directly to the Arduino Uno board.
Otherwise you can use resistors I have seen somewhere.
Good luck.
#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 SCK_PIN 13
#define MISO_PIN 12
#define MOSI_PIN 11
#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
kSetDataComponents, // 0x03
kGetData, // 0x04
kDataResp, // 0x05
kSetConfig, // 0x06
kGetConfig, // 0x07
kConfigResp, // 0x08
kSaveConfig, // 0x09
kStartCal, // 0x0A
kStopCal, // 0x0B
kGetCalData, // 0x0C
kCalDataResp, // 0x0D
kSetCalData, // 0x0E
// data types
kRawX = 1, // 0x01
kRawY, // 0x02
kCalibratedX, // 0x03
kCalibratedY, // 0x04
kHeading, // 0x05
kMagnitude, // 0x06
kTemperature, // 0x07
kDistortion, // 0x08
kCalStatus, // 0x09
// config types
kDeclination = 1, // 0x01
kTrueNorth, // 0x02
kCalSampleFreq, // 0x03
kSampleFreq, // 0x04
kPeriod, // 0x05
kBigEndian, // 0x06
kDampingSize, // 0x07
// cal data types
kXOffset = 1, // 0x01
kYOffset, // 0x02
kXGain, // 0x03
kYGain, // 0x04
kPhi, // 0x05
kCalMagnitude // 0x06
};
typedef struct
{
int x,y;
float xe, ye;
float heading;
float magnitude;
float temperature;
byte distortion, calstatus;
} V2XEData;
V2XEData* data;
void setup (void)
{
Serial.begin (115200);
Serial.println ();
SPI.begin ();
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SYNC_PIN, OUTPUT);
digitalWrite(SYNC_PIN, LOW);
// 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_DIV128);
delay(20);
// sync the compass
v2xe_sync();
} // end of setup
void v2xe_sync(){
digitalWrite(SYNC_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SYNC_PIN, HIGH);
delayMicroseconds(100);
digitalWrite(SYNC_PIN, LOW);
delay(20);
}
byte spi_transmit (const byte what) {
byte a = SPI.transfer (what);
return a;
}
byte spi_receive_byte () {
return spi_transmit(0);
}
void ask_mod_info(){
byte index, count, buffer[64];
index = 0;
// transmit the command
buffer[index++] = kSyncChar; // all frames always start with a sync character
buffer[index++] = kGetModInfo; // the frame type
buffer[index++] = kTerminator; // don’t forget the terminator
// now transmit the command
count = index;
index = 0;
while (count–) {
// just throw away whatever is receive
spi_transmit(buffer[index++]);
}
}
bool spi_receive_info() {
byte frame;
bool found = false;
while (spi_receive_byte() != kSyncChar) delayMicroseconds(250);
frame = spi_receive_byte();
// the next byte will be the response info type
if (frame == kModInfoResp) {
// the next 8 bytes will be the module type and firmware version
for (int i = 0; i< 8; i++){
a = spi_receive_byte();
found = true;
}
}
return found;
}
void ask_heading() {
byte index, count, buffer[64];
index = 0;
// transmit the command
buffer[index++] = kSyncChar; // all frames always start with a sync character
buffer[index++] = kSetDataComponents; // the frame type
buffer[index++] = 9; // number of components to retrieve
buffer[index++] = kRawX; // x
buffer[index++] = kRawY; // y
buffer[index++] = kCalibratedX; // xe
buffer[index++] = kCalibratedY; // ye
buffer[index++] = kHeading; // heading
buffer[index++] = kMagnitude; // magnitude
buffer[index++] = kTemperature; // temperature
buffer[index++] = kDistortion; // distortion
buffer[index++] = kCalStatus; // calstatus
buffer[index++] = kTerminator; // don’t forget the terminator
// now transmit the command
count = index;
index = 0;
//Serial.println(“Ask heading”);
while (count–) {
// just throw away whatever is receive
spi_transmit(buffer[index++]);
}
}
void get_data() {
byte index, count, buffer[64];
index = 0;
// transmit the command
buffer[index++] = kSyncChar; // all frames always start with a sync character
buffer[index++] = kGetData; // the frame type
buffer[index++] = kTerminator; // don’t forget the terminator
// now transmit the command
count = index;
index = 0;
//Serial.println(“Get data”);
while (count–) {
// just throw away whatever is receive
spi_transmit(buffer[index++]);
}
}
bool receive_data() {
byte frame, count, type;
bool found = false;
//Serial.println(“Wait for Sync”);
while (spi_receive_byte() != kSyncChar) delayMicroseconds(250);
frame = spi_receive_byte();
//Serial.println(frame);
// the next byte will be the response frame type
if (frame == kDataResp) {
// the next byte will be the data component count
count = spi_receive_byte();
//Serial.println(count);
while (count–) {
// get the component data identifier
type = spi_receive_byte();
//Serial.println(type);
switch(type) {
case kRawX:
//Serial.println(spi_receive_byte());
data->x = int(spi_receive_byte());
found = true;
break;
case kRawY:
//Serial.println(spi_receive_byte());
data->y = int(spi_receive_byte());
found = true;
break;
case kCalibratedX:
//Serial.println(spi_receive_byte());
data->xe = float(spi_receive_byte());
found = true;
break;
case kCalibratedY:
//Serial.println(spi_receive_byte());
data->ye = float(spi_receive_byte());
found = true;
break;
case kHeading:
//Serial.println(spi_receive_byte());
data->heading = float(spi_receive_byte());
found = true;
break;
case kMagnitude:
//Serial.println(spi_receive_byte());
data->magnitude = float(spi_receive_byte());
found = true;
break;
case kTemperature:
//Serial.println(spi_receive_byte());
data->temperature = float(spi_receive_byte());
found = true;
break;
case kDistortion:
//Serial.println(spi_receive_byte());
data->distortion = spi_receive_byte();
found = true;
break;
case kCalStatus:
//Serial.println(spi_receive_byte());
data->calstatus = spi_receive_byte();
found = true;
break;
default:
// error condition
break;
}
}
// cleanup of the eventual garbage
while (spi_receive_byte() != kTerminator);
}
return found;
}
void loop (void)
{
ask_mod_info();
delay(10);
if (spi_receive_info()) {
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(500);
ask_heading();
delay(10);
get_data();
if (receive_data()) {
Serial.print ("Heading : ");
Serial.println ((data->heading));
Serial.print ("Magnitude : ");
Serial.println ((data->magnitude));
Serial.print ("XRaw : ");
Serial.println ((data->x));
Serial.print ("YRaw : ");
Serial.println ((data->y));
Serial.print ("XCal : ");
Serial.println ((data->xe));
Serial.print ("YCal : ");
Serial.println ((data->ye));
Serial.print ("Temperature : ");
Serial.println ((data->temperature));
Serial.print ("Distortion : ");
Serial.println ((data->distortion));
Serial.print ("CalStatus : ");
Serial.println ((data->calstatus));
}
delay(500);
} // end of loop