Go Down

Topic: ArduPilotMEGA + scp1000(spi) need help (Read 728 times) previous topic - next topic

ivereninov

I have spent several nights trying to figure out how to make this work.  My hardware an APM and a scp1000.
I have connected MISO line directly , and MOSI,SCK,SS lines trough a SF logic level converter rx lines.
Code was taken from the BeginningArduino book .

/*
SCP1000 Mega
DRDY N/A
CSB 53 via Logic Level Convertor
MISO 50 (straight through)
MOSI 51 via Logic Level Convertor
SCK 52 via Logic Level Convertor
3.3v 3.3v
GND GND
TRIG GND
PD GND
*/

// SPI PINS
#define SLAVESELECT 53
#define SPICLOCK 52
#define DATAOUT 51 //MOSI
#define DATAIN 50 //MISO
#define UBLB(a,b) ( ( (a) << 8) | (b) )
#define UBLB19(a,b) ( ( (a) << 16 ) | (b) )
//Addresses
#define PRESSURE 0x1F //Pressure 3 MSB
#define PRESSURE_LSB 0x20 //Pressure 16 LSB
#define TEMP 0x21 //16 bit temp
//#define INTERVAL 900 // Time interval in seconds (approx.)
int dots[124], dotCursor = 0, counter = 0;;
char rev_in_byte;
int temp_in;
float hPa;
unsigned long pressure_lsb;
unsigned long pressure_msb;
unsigned long temp_pressure;
unsigned long pressure;

void setup()
{
Serial.begin(115200);
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
SPCR = B01010011; // SPi Control Register
//MPIE=0, SPE=1 (on), DORD=0 (MSB first), MSTR=1 (master), CPOL=0 (clock idle when low),CPHA=0 (samples MOSI on rising edge), SPR1=1 & SPR0=1 (250kHz)
clr=SPSR;// SPi Status Register
clr=SPDR; // SPi Data Register
delay(10);
write_register(0x03,0x09); // High Speed Read Mode
write_register(0x03,0x0A); // High Resolution Measurement Mode
}
void loop()
{
  delay(500);
getPressure();

//Serial.println(hPa);
temp_in = read_register16(TEMP);
float tempC = float(temp_in)/20.0;
float tempF = (tempC*1.8) + 32;
//Serial.print("t=");
//Serial.println(tempC);
}

void getPressure() {
pressure_msb = read_register(PRESSURE);
pressure_msb &= B00000111;
pressure_lsb = read_register16(PRESSURE_LSB);
pressure_lsb &= 0x0000FFFF;
pressure = UBLB19(pressure_msb, pressure_lsb);
Serial.println(pressure);
pressure /= 4;
hPa = float(pressure)/100;
}

char spi_transfer(char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}

char read_register(char register_name)
{
char in_byte;
register_name <<= 2;
digitalWrite(SLAVESELECT,LOW); //Enable SPI Device
spi_transfer(register_name); //Write byte to device
in_byte = spi_transfer(0x00); //Send nothing but get back register value
digitalWrite(SLAVESELECT,HIGH); // Disable SPI Device
delay(10);
return(in_byte); // return value
}

unsigned long read_register16(char register_name)
{
byte in_byte1;
byte in_byte2;
float in_word;
register_name <<= 2;
digitalWrite(SLAVESELECT,LOW); //Enable SPI Device
spi_transfer(register_name); //Write byte to device
in_byte1 = spi_transfer(0x00);
in_byte2 = spi_transfer(0x00);
digitalWrite(SLAVESELECT,HIGH); // Disable SPI Device
in_word = UBLB(in_byte1,in_byte2);
return(in_word); // return value
}

void write_register(char register_name, char register_value)
{
register_name <<= 2;
register_name |= B00000010; //Write command
digitalWrite(SLAVESELECT,LOW); //Select SPI device
spi_transfer(register_name); //Send register location
spi_transfer(register_value); //Send value to record into register
digitalWrite(SLAVESELECT,HIGH);
}




What I get are zeros.
Any advice appreciated)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy