system
August 8, 2012, 2:41pm
1
ciao a tutti,
ho appena acquistato un accelerometro BMA180 Triple Axis Accelerometer Breakout - BMA180 - SEN-09723 - SparkFun Electronics ? che volevo connettere all'arduino2009 tramite SPI visto che l'I2C è gia occupato.
ho fatto il collegamento come su http://www.drotek.fr/shop/en/31-bma180-accelerometer-sensor-pcb.html
ma non ho trovato il codice per arduino e non ho esperienza con SPI.
potreste aiutarmi?
grazie per la collaborazione.
PaoloP
August 8, 2012, 3:41pm
2
All'I2C puoi collegare fino a 128 sensori, sempre che abbiano indirizzi differenti.
La prossima volta apri il topic nelle sotto sezioni: Hardware o Software a seconda dell'argomento che vuoi trattare.
Ciao.
system
August 9, 2012, 9:43am
3
Ciao, scusate se ho postato nella sezione sbagliata. Non posso collegare il sensore in i2c perchè ne ho già molti altri collegati e il controllo sarebbe troppo lento.
Esiste un sito che mostra le funzioni da usare per leggere i dati in SPI?
PaoloP
August 9, 2012, 10:01am
4
Mark116:
Ciao, scusate se ho postato nella sezione sbagliata. Non posso collegare il sensore in i2c perchè ne ho già molti altri collegati e il controllo sarebbe troppo lento.
Esiste un sito che mostra le funzioni da usare per leggere i dati in SPI?
Devi cercare il datasheet del sensore.
PaoloP
August 10, 2012, 10:26am
5
Mark116:
Esiste un sito che mostra le funzioni da usare per leggere i dati in SPI?
Ho trovato questo esempio
/**********************************************************
* BMA180_SPI_Example.pde ----- Sample sketch for BMA180 Accelerometer using SPI Mode 3 *
* A sample sketch that shows the basic functions of the BMA180 acclerometer. *
**********************************************************/
#include <SPI.h>
#define ee_w_MASK 0x10
#define mode_config_MASK 0x03
#define bw_MASK 0xF0
#define range_MASK 0x0E
#define lat_int_MASK 0x01
#define lat_int 0x01
#define START PORTB &= ~0x04
#define STOP PORTB |= 0x04
#define READ 0x80
#define CSB 10 //slave select pin
int x,y,z,temp;
void setup() {
Serial.begin(115200);
pinMode (CSB, OUTPUT); // set the slaveSelectPin as an output
SPI.begin();
SPI.setDataMode(SPI_MODE3);
initializeBMA180();
}
void loop(){
readAccel();
printAccel();
delay(200);
}
void readAccel(){
START;
SPI.transfer(0x02|READ);
x = SPI.transfer(0xFF);
x |= SPI.transfer(0xFF) << 8;
x >>= 2;
y = SPI.transfer(0xFF);
y |= SPI.transfer(0xFF) << 8;
y >>= 2;
z = SPI.transfer(0xFF);
z |= SPI.transfer(0xFF) << 8;
z >>= 2;
temp = SPI.transfer(0xFF);
STOP;
}
void printAccel()
{
Serial.print("X = ");
Serial.print((int)x,DEC);
Serial.print(" Y = ");
Serial.print((int)y,DEC);
Serial.print(" Z = ");
Serial.print((int)z,DEC);
Serial.print(" Temperature(C) = ");
Serial.println(map((int8_t)temp,-128,127,-400,875)/10.0,1);
}
byte initializeBMA180(){
/*Set EEPROM image to write mode so we can change configuration*/
delay(20);
START;
SPI.transfer(0x0D|READ);
byte ee_w = SPI.transfer(0xFF);
STOP;
delay(1);
START;
ee_w |= ee_w_MASK;
SPI.transfer(0x0D);
SPI.transfer(ee_w);
STOP;
delay(20);
//disable I2C as per the datasheet
START;
SPI.transfer(0x27|READ);
byte dis_I2C = SPI.transfer(0xFF);
dis_I2C |= 0x01;
STOP;
delay(1);
START;
SPI.transfer(0x27);
SPI.transfer(dis_I2C);
STOP;
delay(20);
/*Set mode configuration register to Mode 00*/
START;
SPI.transfer(0x30|READ);
byte mode_config = SPI.transfer(0xFF);
mode_config &= ~(mode_config_MASK);
STOP;
delay(1);
START;
SPI.transfer(0x30);
SPI.transfer(mode_config);
STOP;
delay(20);
/*Set bandwidth to 10Hz*/
START;
SPI.transfer(0x20|READ);
byte bw = SPI.transfer(0xFF);
bw &= ~(bw_MASK);
bw |= 0x00 << 4;
STOP;
delay(1);
START;
SPI.transfer(0x20);
SPI.transfer(bw);
STOP;
delay(20);
/*Set acceleration range to 2g*/
START;
SPI.transfer(0x35|READ);
byte range = SPI.transfer(0xFF);
range &= ~(range_MASK);
range |= (0x02 << 1) ;
STOP;
delay(1);
START;
SPI.transfer(0x35);
SPI.transfer(range);
STOP;
delay(20);
/*Set interrupt latch state to non latching*/
START;
SPI.transfer(0x21|READ);
byte latch_int = SPI.transfer(0xFF);
latch_int &= ~(0x01);
STOP;
delay(1);
START;
SPI.transfer(0x21);
SPI.transfer(latch_int);
STOP;
delay(20);
/*Set interrupt type to new data*/
START;
SPI.transfer(0x21|READ);
byte int_type = SPI.transfer(0xFF);
int_type |= 0x02;
STOP;
delay(1);
START;
SPI.transfer(0x21);
SPI.transfer(int_type);
STOP;
delay(20);
return(0);
}