Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include < SPI.h >
  2.  
  3. const int X8 = 0x06;
  4. const int Y8 = 0x07;
  5. const int Z8 = 0x08;
  6.  
  7. /*
  8. bits
  9. 1 0 mode 00: standby, 01: measurement
  10. 5 1: spi 3 wire, 0: 4 wire
  11. 4 self test
  12. 3 2 glevel: 01 2g
  13. 1 0 mode, 00 standby, 01 measurement
  14. */
  15. const int MODE = 0x16;
  16.  
  17. const int STATUS = 0x09;
  18.  
  19. //7 digital filter band width, 0 62.5 Hz, 1 125 Hz
  20. const int CONTROL = 0x18;
  21.  
  22. bool isTesting = false;
  23.  
  24. const int chipSelectPin = 7;
  25.  
  26. void setup() {
  27. Serial.begin(9600);
  28. SPI.begin();
  29. pinMode(chipSelectPin, OUTPUT);
  30.  
  31. //set up standby, spi 4 wire, 2g range,
  32. writeRegister(MODE, 0x04);
  33.  
  34. //set up speed
  35. writeRegister(CONTROL, 0x00);
  36.  
  37. delay(100);
  38. }
  39. void loop() {
  40. if (getSerial() == 't') {
  41. isTesting = true;
  42. writeRegister(MODE, 0x05); // 0000 0101- 0001 0101 0x15
  43. testing();
  44.  
  45. }
  46.  
  47. }
  48. void testing() {
  49. while (isTesting) {
  50. if ((readRegister(STATUS) & 1) == 1) {
  51. Serial.print((char) readRegister(X8), DEC);
  52. Serial.print("x");
  53. Serial.print((char) readRegister(Y8), DEC);
  54. Serial.print("y");
  55. Serial.print((char) readRegisterChar(Z8), DEC);
  56. Serial.print("z");
  57. }
  58.  
  59. if (getSerial() == 'u') {
  60. isTesting = false;
  61. writeRegister(MODE, 0x04);
  62. }
  63. }
  64. }
  65. char getSerial() {
  66. if (Serial.available()) {
  67. char c = Serial.read();
  68. return c;
  69. }
  70. return 'x';
  71. }
  72. byte readRegister(byte thisRegister) {
  73. byte inByte = 0;
  74. SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); // 500khz clock
  75. digitalWrite(chipSelectPin, LOW);
  76. SPI.transfer(thisRegister << 1);
  77. inByte = SPI.transfer(0x00);
  78. digitalWrite(chipSelectPin, HIGH);
  79. return inByte;
  80. }
  81. char readRegisterChar(byte thisRegister) {
  82. char inChar = 0;
  83. SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); // 500khz clock
  84. digitalWrite(chipSelectPin, LOW);
  85. SPI.transfer(thisRegister << 1);
  86. inChar = SPI.transfer(0x00);
  87. digitalWrite(chipSelectPin, HIGH);
  88. return inChar;
  89. }
  90. void writeRegister(byte thisRegister, byte value) {
  91. SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); // 500khz clock
  92. digitalWrite(chipSelectPin, LOW);
  93. SPI.transfer(128 | thisRegister << 1);
  94. SPI.transfer(value);
  95. digitalWrite(chipSelectPin, HIGH);
  96. }
  97.  
  98. const int X10_LSB = 0x00;
  99. const int X10_MSB = 0x01;
  100. const int Y10_LSB = 0x02;
  101. const int Y10_MSB = 0x03;
  102. const int Z10_LSB = 0x04;
  103. const int Z10_MSB = 0x05;
  104.  
  105. //[...]
  106.  
  107. void testing() {
  108. while (isTesting) {
  109. if ((readRegister(STATUS) & 1) == 1) {
  110.  
  111. int xData = (readRegister(X10_MSB) << 8) | readRegister(X10_LSB);
  112.  
  113. // checks if bit 9 is 1 and sign extends
  114. if ((xData & 512) == 512)
  115. xData = (0b1111110000000000 | xData);
  116. int yData = (readRegister(Y10_MSB) << 8) | readRegister(Y10_LSB);
  117. if ((yData & 512) == 512)
  118. yData = (0b1111110000000000 | yData);
  119. int zData = (readRegister(Z10_MSB) << 8) | readRegister(Z10_LSB);
  120. if ((zData & 512) == 512)
  121. zData = (0b1111110000000000 | zData);
  122.  
  123. Serial.print(xData);
  124. Serial.print("x");
  125. Serial.print(yData);
  126. Serial.print("y");
  127. Serial.print(zData);
  128. Serial.print("z");
  129.  
  130. }
  131.  
  132. int xData =(readRegister (X10_LSB));
  133. xData = xData | (readRegister (X10_MSB)<<8);
  134. xData = xData << 6;
  135. int yData =(readRegister (Y10_LSB));
  136. yData = yData | (readRegister (Y10_MSB)<<8);
  137. yData = yData << 6;
  138. int zData = (readRegister (Z10_LSB));
  139. zData = zData | (readRegister (Z10_MSB)<<8);
  140. zData = zData << 6;
  141.  
  142. xData /= 64;
  143. yData /= 64;
  144. zData /= 64;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement