SMR3000 gyroskooppianturin ja SCA3100 kiihtyvyysanturin käyttöönotto arduino mega 2560 mikro-ohjaimella. Ohjelma ja SPI kirjasto löytyy täältä . Kopioi SPI kirjasto kansioon arduino/libaries.
Johdotus:
Kuvassa on kiinnitetty CMR3000 anturi. SCA3100 kiihtyvyysanturi tulee muuten samanlailla kiinni mutta INT pinniä ei tarvitse liittää ja CSB pinni tulee arduinossa pinniiin 49.
Koodi:
#include <SPI.h> #include <stdio.h> const byte READ = 0b11111100; const byte WRITE = 0b00000010; //Gyron registerit #define REVID 0x01 #define CTRL 0x02 #define I2C_DIS 0x10 #define MODE_80 0x06 #define RESET 0x80 #define STATUS 0x02 // CSB pinnit ja gyron data ready pin const int GyroDataReadyPin = 6; const int GyroChipSelectPin = 48; const int AccChipSelectPin = 49; // Gyron muuttujat unsigned short gyro_msb_ydata; unsigned short gyro_lsb_ydata; short gyro_ydata; unsigned short gyro_msb_xdata; unsigned short gyro_lsb_xdata; short gyro_xdata; unsigned short gyro_msb_zdata; unsigned short gyro_lsb_zdata; short gyro_zdata; // ACC muuttujat unsigned short acc_msb_ydata; unsigned short acc_lsb_ydata; short acc_ydata; unsigned short acc_msb_xdata; unsigned short acc_lsb_xdata; short acc_xdata; unsigned short acc_msb_zdata; unsigned short acc_lsb_zdata; short acc_zdata; void setup() { Serial.begin(115200); SPI.begin(); SPI.setClockDivider(SPI_CLOCK_DIV64); pinMode(GyroDataReadyPin, INPUT); pinMode(GyroChipSelectPin, OUTPUT); pinMode(AccChipSelectPin, OUTPUT); GyroWriteRegister(CTRL, RESET); delay(100); GyroWriteRegister(CTRL,0x16);//, I2C_DIS | MODE_80); delay(100); } void loop() { if (digitalRead(GyroDataReadyPin) == HIGH) { // Gyro x-data gyro_msb_xdata=GyroReadRegister(0x0D, 1); gyro_lsb_xdata=GyroReadRegister(0x0C, 1); gyro_xdata=(gyro_msb_xdata<<8) | gyro_lsb_xdata; // Gyro y-data gyro_msb_ydata=GyroReadRegister(0x0F, 1); gyro_lsb_ydata=GyroReadRegister(0x0E, 1); gyro_ydata=(gyro_msb_ydata<<8) | gyro_lsb_ydata; // Gyro z-data gyro_msb_zdata=GyroReadRegister(0x11, 1); gyro_lsb_zdata=GyroReadRegister(0x10, 1); gyro_zdata=(gyro_msb_zdata<<8) | gyro_lsb_zdata; } //Acc x-data acc_msb_xdata=AccReadRegister(0x05, 1); acc_lsb_xdata=AccReadRegister(0x04, 1); acc_xdata=(acc_msb_xdata<<8) | acc_lsb_xdata; acc_xdata=realValues(acc_xdata); //Acc y-data acc_msb_ydata=AccReadRegister(0x07, 1); acc_lsb_ydata=AccReadRegister(0x06, 1); acc_ydata=(acc_msb_ydata<<8) | acc_lsb_ydata; acc_ydata=realValues(acc_ydata); //Acc z-data acc_msb_zdata=AccReadRegister(0x09, 1); acc_lsb_zdata=AccReadRegister(0x08, 1); acc_zdata=(acc_msb_zdata<<8) | acc_lsb_zdata; acc_zdata=realValues(acc_zdata); // tulostetaan arvot sarjaväylään Serial.print("GYRO: "); Serial.print(gyro_xdata, DEC); Serial.print(" "); Serial.print(gyro_ydata, DEC); Serial.print(" "); Serial.print(gyro_zdata, DEC); Serial.print(" "); Serial.print("ACC: "); Serial.print(acc_xdata, DEC); Serial.print(" "); Serial.print(acc_ydata, DEC); Serial.print(" "); Serial.println(acc_zdata, DEC); } //gyro read unsigned int GyroReadRegister(byte thisRegister, int bytesToRead ) { byte inByte = 0; unsigned int result = 0; thisRegister = thisRegister << 2; byte dataToSend = thisRegister & READ; digitalWrite(GyroChipSelectPin, LOW); SPI.transfer(dataToSend); result = SPI.transfer(0x00); bytesToRead--; if (bytesToRead > 0) { result = result << 8; inByte = SPI.transfer(0x00); result = result | inByte; bytesToRead--; } digitalWrite(GyroChipSelectPin, HIGH); delay(5); return(result); } //gyro write void GyroWriteRegister(byte thisRegister, byte thisValue) { thisRegister = thisRegister << 2; byte dataToSend = thisRegister | WRITE; digitalWrite(GyroChipSelectPin, LOW); SPI.transfer(dataToSend); SPI.transfer(thisValue); digitalWrite(GyroChipSelectPin, HIGH); delay(20); } //acc read unsigned int AccReadRegister(byte thisRegister, int bytesToRead ) { byte inByte = 0; unsigned int result = 0; thisRegister = thisRegister << 2; byte dataToSend = thisRegister & READ; digitalWrite(AccChipSelectPin, LOW); SPI.transfer(dataToSend); result = SPI.transfer(0x00); bytesToRead--; if (bytesToRead > 0) { result = result << 8; inByte = SPI.transfer(0x00); result = result | inByte; bytesToRead--; } digitalWrite(AccChipSelectPin, HIGH); delay(10); return(result); } //acc write void AccWriteRegister(byte thisRegister, byte thisValue) { thisRegister = thisRegister << 2; byte dataToSend = thisRegister | WRITE; digitalWrite(AccChipSelectPin, LOW); SPI.transfer(dataToSend); SPI.transfer(thisValue); digitalWrite(AccChipSelectPin, HIGH); delay(20); } unsigned int shift(unsigned short thisData, int thisBit) { unsigned short data; data=thisData>>(thisBit-1); data=data&0x01; return data; } unsigned short realValues(unsigned short values) { short taulukko[14]; int i; int bitti=3; unsigned short valmis; for(i=0;i<14;i++) { taulukko[i]= shift(values,bitti); bitti++; } valmis=(10/9)*(-taulukko[13]*pow(2,13)+taulukko[12]*pow(2,12)+taulukko[11]*pow(2,11)+taulukko[10]*pow(2,10)+taulukko[9]*pow(2,9)+taulukko[8]*pow(2,8)+taulukko[7]*pow(2,7)+taulukko[6]*pow(2,6)+taulukko[5]*pow(2,5)+taulukko[4]*pow(2,4)+taulukko[3]*pow(2,3)+taulukko[2]*pow(2,2)+taulukko[1]*2+taulukko[0]); return valmis; }
Alla koodit CMR3000 ja SCA3100 antureille yksittäin. Molemmissa CSB pinni on 48.