Jump to content

ADXL345+MSP430f5529 4-wires SPI mode, wired outputs

Recommended Posts



I am using Energia 17 & 18 to code for the MSP430F5529 and ADXL345. The following is the code I used.

  1. //Add the SPI library so we can communicate with the ADXL345 sensor
  2. #include <SPI.h>
  4. //Assign the Chip Select signal to pin 2.
  5. int CS = 2;
  7. //This is a list of some of the registers available on the ADXL345.
  8. //To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
  9. char POWER_CTL = 0x2D; //Power Control Register
  10. char DATA_FORMAT = 0x31;
  11. char DATAX0 = 0x32; //X-Axis Data 0
  12. char DATAX1 = 0x33; //X-Axis Data 1
  13. char DATAY0 = 0x34; //Y-Axis Data 0
  14. char DATAY1 = 0x35; //Y-Axis Data 1
  15. char DATAZ0 = 0x36; //Z-Axis Data 0
  16. char DATAZ1 = 0x37; //Z-Axis Data 1
  18. //This buffer will hold values read from the ADXL345 registers.
  19. char values[10];
  20. //These variables will be used to hold the x,y and z axis accelerometer values.
  21. long x,y,z;
  23. void setup(){
  24. //Initiate an SPI communication instance.
  25. SPI.begin();
  27. //Configure the SPI connection for the ADXL345.
  28. SPI.setDataMode(SPI_MODE3);
  30. //Create a serial connection to display the data on the terminal.
  31. Serial.begin(115200);
  32. Serial.println("GO!");
  34. //Set up the Chip Select pin to be an output from the Arduino.
  35. pinMode(CS, OUTPUT);
  36. //Before communication starts, the Chip Select pin needs to be set high.
  37. digitalWrite(CS, HIGH);
  39. //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  40. writeRegister(DATA_FORMAT, 0x00);
  41. //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  42. writeRegister(POWER_CTL, 0x08); //Measurement mode
  43. }
  45. void loop(){
  46. //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  47. //The results of the read operation will get stored to the values[] buffer.
  48. readRegister(DATAX0, 6, values);
  50. //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  51. //The X value is stored in values[0] and values[1].
  52. x = ((long)values[1]<<8)|(long)values[0];
  53. //The Y value is stored in values[2] and values[3].
  54. y = ((long)values[3]<<8)|(long)values[2];
  55. //The Z value is stored in values[4] and values[5].
  56. z = ((long)values[5]<<8)|(long)values[4];
  58. //Print the results to the terminal.
  59. Serial.print(x);
  60. Serial.print('\t');
  61. Serial.print(y);
  62. Serial.print('\t');
  63. Serial.println(z);
  64. delay(100);
  65. }
  67. //This function will write a value to a register on the ADXL345.
  68. //Parameters:
  69. // char registerAddress - The register to write a value to
  70. // char value - The value to be written to the specified register.
  71. void writeRegister(char registerAddress, char value){
  72. //Set Chip Select pin low to signal the beginning of an SPI packet.
  73. digitalWrite(CS, LOW);
  74. //Transfer the register address over SPI.
  75. SPI.transfer(registerAddress);
  76. //Transfer the desired register value over SPI.
  77. SPI.transfer(value);
  78. //Set the Chip Select pin high to signal the end of an SPI packet.
  79. digitalWrite(CS, HIGH);
  80. }
  82. //This function will read a certain number of registers starting from a specified address and store their values in a buffer.
  83. //Parameters:
  84. // char registerAddress - The register addresse to start the read sequence from.
  85. // int numBytes - The number of registers that should be read.
  86. // char * values - A pointer to a buffer where the results of the operation should be stored.
  87. void readRegister(char registerAddress, int numBytes, char * values){
  88. //Since we're performing a read operation, the most significant bit of the register address should be set.
  89. char address = 0x80 | registerAddress;
  90. //If we're doing a multi-byte read, bit 6 needs to be set as well.
  91. if(numBytes > 1)address = address | 0x40;
  93. //Set the Chip select pin low to start an SPI packet.
  94. digitalWrite(CS, LOW);
  95. //Transfer the starting register address that needs to be read.
  96. SPI.transfer(address);
  97. //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  98. for(int i=0; i<numBytes; i++){
  99. values = SPI.transfer(0x00);
  100. }
  101. //Set the Chips Select pin high to end the SPI packet.
  102. digitalWrite(CS, HIGH);
  103. }


The output is all -1,-1,-1. When I moved the ADXL345 module, they became 0,0,0 or some wired combinations like -1,0,-678234. Can someone help me to see what is wrong with the code? I changed the chip select pin numbers when I code for them and I am using USCIA port. This code is from other places. 



Yutao Li

Link to post
Share on other sites

What exactly is the issue?  I see your code does a bunch of stuff.  


1. Try just looking at your code and dont include input from the hardware.   Load the registers in software to isolate the inputs....Does that prove the rest of the code is working?


2. Try isolating the output by only getting your inputs then displaying the register values on the monitor. 


I am unclear as to what the issue is.  Can you break it down into 5 or 10 lines of code?



Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...