Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Why is my interrupt not working on ADXL345 accelerometer?

  1. #1
    Junior Member
    Join Date
    Feb 2015
    Posts
    4

    Why is my interrupt not working on ADXL345 accelerometer?

    I have set up an ADXL345 accelerometer connected to the Teensy 3.1 in SPI config. and run some sample code from Sparkfun to confirm that the setup is indeed wired up correctly and functions as it should.

    The connections are as follows:

    CS -> Teensy Pin 10
    DOUT ->Teensy Pin 11
    DIN ->Teensy Pin 12
    SCK -> Teensy Pin 13

    And the interrupt from the accelerometer:
    Int1 (ADXL345) -> Teensy Pin 9

    The ubiquitous Sparkfun code (right at the bottom of this post) code complies without error. After uploading, everything functions fine with the relative acceleration being printed over the serial connection, so I don't believe that there are any problems with regards to the setup of the SPI connections.

    The problem I have is that I've tried to modify the code so that every time the accelerometer has data ready, an interrupt is generated by the accelerometer on INT1 which is fed into pin 9 of the Teensy. The intention is then to run a very short ISR which sets a flag. In the main loop, if this flag has been set, then to read the register values which contain the acceleration data. The vast majority of my code below which reads the registers and prints the accelerometer information on the serial line has been copied straight from the Sparkfun sample code. The only main difference is the inclusion of the ISR.

    I have done some simple fault finding by placing a multimeter on Pin 9 on the Teensy (connected to INT1 on the ADXL345). When I enable interrupts ie.

    Code:
        // Enable the Data Ready Interrupts - do this after setting up interrupts
      writeRegister(INT_ENABLE, 0x80);
    the voltage on Pin 9 is 3.3V (Active High has been written to DATA_FORMAT register). When interrupts are disabled ie.

    Code:
        // Enable the Data Ready Interrupts - do this after setting up interrupts
      writeRegister(INT_ENABLE, 0x00);
    the voltage is 0V. It seems (although I may be completely wrong) that the interrupt is being generated by the ADXL345, but my ISR code is not processing it correctly.

    I'm keen to know if my interrupts and ISR are setup correctly and / or any suggestions that might enable me to get this running.

    Many thanks in advance,
    Tim

    Code:
    //Add the SPI library so we can communicate with the ADXL345 sensor
    #include <SPI.h>
    
    //Assign the Chip Select signal to pin 10.
    int CS=10;
    
    // Assign the pin to accept the interrupt from the ADXL345
    #define INTPIN 9
    
    //ADXL345 Register Addresses
    #define  DEVID   0x00  //Device ID Register
    #define OFSX    0x1E  //X-axis offset
    #define OFSY    0x1F  //Y-axis offset
    #define OFSZ    0x20  //Z-axis offset
    #define BW_RATE   0x2C  //Data rate and power mode control
    #define POWER_CTL 0x2D  //Power Control Register
    #define INT_ENABLE  0x2E  //Interrupt Enable Control
    #define INT_MAP   0x2F  //Interrupt Mapping Control
    #define INT_SOURCE  0x30  //Source of interrupts
    #define DATA_FORMAT 0x31  //Data format control
    #define DATAX0    0x32  //X-Axis Data 0
    #define DATAX1    0x33  //X-Axis Data 1
    #define DATAY0    0x34  //Y-Axis Data 0
    #define DATAY1    0x35  //Y-Axis Data 1
    #define DATAZ0    0x36  //Z-Axis Data 0
    #define DATAZ1    0x37  //Z-Axis Data 1
    #define FIFO_CTL  0x38  //FIFO control
    
    
    
    //  Declare the interrupt flag that will be used to check if data is ready
    volatile bool intFlag = LOW;
    
    //This buffer will hold values read from the ADXL345 registers.
    byte values[10];
    byte output[20];
    //These variables will be used to hold the x,y and z axis accelerometer values.
    int x,y,z;
    double xg, yg, zg;
    
    void setup(){ 
      //Initiate an SPI communication instance.
      SPI.begin();
      //Configure the SPI connection for the ADXL345.
      SPI.setDataMode(SPI_MODE3);
      //Create a serial connection to display the data on the terminal.
      Serial.begin(9600);
      
      //Set up the Chip Select pin to be an output from the Arduino.
      pinMode(CS, OUTPUT);
    
      // Mark pin 9 on the Teensy as an INPUT pin for the ADXL345 interrupt
      pinMode(INTPIN, INPUT);
    
      // Attach an interrupt handler to be called whenever
      // the pin changes from LOW to HIGH
      attachInterrupt(digitalPinToInterrupt(INTPIN), DataReadyISR, RISING); 
    
    //  Start Communication with the ADXL345
    
      //Before communication starts, the Chip Select pin needs to be set high.
      digitalWrite(CS, HIGH);
    
      //Put the ADXL345 into +/- 4G range by writing the value 0x00 to the DATA_FORMAT register.
      writeRegister(DATA_FORMAT, 0x01);
    
       //Set the sample rate to 800Hz 
      writeRegister(BW_RATE, 0x0D); //800Hz Sampling mode
        
        // Map the data ready interrupt to INT1 
      writeRegister(INT_MAP, 0x7F);
    
        // Enable the Data Ready Interrupts - do this after setting up interrupts
      writeRegister(INT_ENABLE, 0x80); 
    
      //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
      writeRegister(POWER_CTL, 0x08); //Measurement mode
    
      //Clear any interrupts from the INT_SOURCE register.
      readRegister(INT_SOURCE, 1, values); 
    }
    
    void loop(){
    
      // test the status of the inerrupt flag before anything else.
      Serial.println(intFlag);
      
      if(intFlag == HIGH){
      
      //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
      //The results of the read operation will get stored to the values[] buffer.
      readRegister(DATAX0, 6, values);
    
      //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.
      //The X value is stored in values[0] and values[1].
      x = tenBitTwosComplementToDecimal((((uint16_t)values[1]<<8)|(uint16_t)values[0]) & 1023);
      //The Y value is stored in values[2] and values[3].
      y = tenBitTwosComplementToDecimal((((uint16_t)values[3]<<8)|(uint16_t)values[2]) & 1023);
      //The Z value is stored in values[4] and values[5].
      z = tenBitTwosComplementToDecimal((((uint16_t)values[5]<<8)|(uint16_t)values[4]) & 1023);
      
      //Convert the accelerometer value to G's. 
      //With 10 bits measuring over a +/-4g range we can find how to convert by using the equation:
      // Gs = Measurement Value * (G-range/(2^10)) or Gs = Measurement Value * (8/1024)
      xg = x * 0.0078125;
      yg = y * 0.0078125;
      zg = z * 0.0078125;
      
          Serial.print((float)xg,4);
          Serial.print("g,");
          Serial.print((float)yg,4);
          Serial.print("g,");
          Serial.print((float)zg,4);
          Serial.println("g");
    
      //Clear the int flag (if it hasn't already been cleared by reading data registers
      intFlag = LOW;
      }
         // If no new data is ready, print that nothing's happening
         else{
              Serial.println("intFlag is LOW - Int not triggered");
             }
    }
    
    // Interrupt Service Routine to deal with the interrupt
    void DataReadyISR() {
    // If the pin is HIGH, set the flag
        
        if (digitalRead(INTPIN) == HIGH) {
            intFlag = HIGH;
        } 
    }
    
    // Function to convert twos compliment to decimal
    int16_t tenBitTwosComplementToDecimal(uint16_t x)
    {
      boolean negative = (x & (1 << 9)) != 0;
      if(negative)
        return x | ~((1 << 10) - 1);
       return (int16_t)x;
    }
    
    //This function will write a value to a register on the ADXL345.
    //Parameters:
    //  char registerAddress - The register to write a value to
    //  char value - The value to be written to the specified register.
    void writeRegister(char registerAddress, unsigned char value){
      //Set Chip Select pin low to signal the beginning of an SPI packet.
      digitalWrite(CS, LOW);
      //Transfer the register address over SPI.
      SPI.transfer(registerAddress);
      //Transfer the desired register value over SPI.
      SPI.transfer(value);
      //Set the Chip Select pin high to signal the end of an SPI packet.
      digitalWrite(CS, HIGH);
    }
    
    //This function will read a certain number of registers starting from a specified address and store their values in a buffer.
    //Parameters:
    //  char registerAddress - The register addresse to start the read sequence from.
    //  int numBytes - The number of registers that should be read.
    //  char * values - A pointer to a buffer where the results of the operation should be stored.
    void readRegister(char registerAddress, int numBytes, unsigned char * values){
      //Since we're performing a read operation, the most significant bit of the register address should be set.
      char address = 0x80 | registerAddress;
      //If we're doing a multi-byte read, bit 6 needs to be set as well.
      if(numBytes > 1)address = address | 0x40;
      
      //Set the Chip select pin low to start an SPI packet.
      digitalWrite(CS, LOW);
      //Transfer the starting register address that needs to be read.
      SPI.transfer(address);
      //Continue to read registers until we've read the number specified, storing the results to the input buffer.
      for(int i=0; i<numBytes; i++){
        values[i] = SPI.transfer(0x00);
      }
      //Set the Chips Select pin high to end the SPI packet.
      digitalWrite(CS, HIGH);
    }

    And the original Sparkfun code which works absolutely fine:

    Code:
    //Add the SPI library so we can communicate with the ADXL345 sensor
    #include <SPI.h>
    
    //Assign the Chip Select signal to pin 10.
    int CS=10;
    
    //ADXL345 Register Addresses
    #define  DEVID   0x00  //Device ID Register
    #define THRESH_TAP  0x1D  //Tap Threshold
    #define OFSX    0x1E  //X-axis offset
    #define OFSY    0x1F  //Y-axis offset
    #define OFSZ    0x20  //Z-axis offset
    #define DURATION  0x21  //Tap Duration
    #define LATENT    0x22  //Tap latency
    #define WINDOW    0x23  //Tap window
    #define THRESH_ACT  0x24  //Activity Threshold
    #define THRESH_INACT  0x25  //Inactivity Threshold
    #define TIME_INACT  0x26  //Inactivity Time
    #define ACT_INACT_CTL 0x27  //Axis enable control for activity and inactivity detection
    #define THRESH_FF 0x28  //free-fall threshold
    #define TIME_FF   0x29  //Free-Fall Time
    #define TAP_AXES  0x2A  //Axis control for tap/double tap
    #define ACT_TAP_STATUS  0x2B  //Source of tap/double tap
    #define BW_RATE   0x2C  //Data rate and power mode control
    #define POWER_CTL 0x2D  //Power Control Register
    #define INT_ENABLE  0x2E  //Interrupt Enable Control
    #define INT_MAP   0x2F  //Interrupt Mapping Control
    #define INT_SOURCE  0x30  //Source of interrupts
    #define DATA_FORMAT 0x31  //Data format control
    #define DATAX0    0x32  //X-Axis Data 0
    #define DATAX1    0x33  //X-Axis Data 1
    #define DATAY0    0x34  //Y-Axis Data 0
    #define DATAY1    0x35  //Y-Axis Data 1
    #define DATAZ0    0x36  //Z-Axis Data 0
    #define DATAZ1    0x37  //Z-Axis Data 1
    #define FIFO_CTL  0x38  //FIFO control
    #define FIFO_STATUS 0x39  //FIFO status
    
    //This buffer will hold values read from the ADXL345 registers.
    byte values[10];
    byte output[20];
    //These variables will be used to hold the x,y and z axis accelerometer values.
    int x,y,z;
    double xg, yg, zg;
    char tapType=0;
    
    void setup(){ 
      //Initiate an SPI communication instance.
      SPI.begin();
      //Configure the SPI connection for the ADXL345.
      SPI.setDataMode(SPI_MODE3);
      //Create a serial connection to display the data on the terminal.
      Serial.begin(9600);
      
      //Set up the Chip Select pin to be an output from the Arduino.
      pinMode(CS, OUTPUT);
      //Before communication starts, the Chip Select pin needs to be set high.
      digitalWrite(CS, HIGH);
      
      //Put the ADXL345 into +/- 2G range by writing the value 0x00 to the DATA_FORMAT register.
      writeRegister(DATA_FORMAT, 0x01);
      
      // disable interrupts
      writeRegister(INT_ENABLE, 0x00);  
      
      //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
      writeRegister(POWER_CTL, 0x08); //Measurement mode
      readRegister(INT_SOURCE, 1, values); //Clear the interrupts from the INT_SOURCE register.
    }
    
    int16_t tenBitTwosComplementToDecimal(uint16_t x)
    {
      boolean negative = (x & (1 << 9)) != 0;
      if(negative)
        return x | ~((1 << 10) - 1);
       return (int16_t)x;
    }
    
    void loop(){
      //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
      //The results of the read operation will get stored to the values[] buffer.
      readRegister(DATAX0, 6, values);
    
      //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.
      //The X value is stored in values[0] and values[1].
      x = tenBitTwosComplementToDecimal((((uint16_t)values[1]<<8)|(uint16_t)values[0]) & 1023);
      //The Y value is stored in values[2] and values[3].
      y = tenBitTwosComplementToDecimal((((uint16_t)values[3]<<8)|(uint16_t)values[2]) & 1023);
      //The Z value is stored in values[4] and values[5].
      z = tenBitTwosComplementToDecimal((((uint16_t)values[5]<<8)|(uint16_t)values[4]) & 1023);
      
      //Convert the accelerometer value to G's. 
      //With 10 bits measuring over a +/-4g range we can find how to convert by using the equation:
      // Gs = Measurement Value * (G-range/(2^10)) or Gs = Measurement Value * (8/1024)
      xg = x * 0.0078125;
      yg = y * 0.0078125;
      zg = z * 0.0078125;
      
          Serial.print((float)xg,4);
          Serial.print("g,");
          Serial.print((float)yg,4);
          Serial.print("g,");
          Serial.print((float)zg,4);
          Serial.println("g");
      /*for(unsigned char i = 0; i < 6; i++)
      {
        Serial.print(values[i]);
        Serial.print(",");
      }
      Serial.println();*/
      delay(1); 
    }
    
    //This function will write a value to a register on the ADXL345.
    //Parameters:
    //  char registerAddress - The register to write a value to
    //  char value - The value to be written to the specified register.
    void writeRegister(char registerAddress, unsigned char value){
      //Set Chip Select pin low to signal the beginning of an SPI packet.
      digitalWrite(CS, LOW);
      //Transfer the register address over SPI.
      SPI.transfer(registerAddress);
      //Transfer the desired register value over SPI.
      SPI.transfer(value);
      //Set the Chip Select pin high to signal the end of an SPI packet.
      digitalWrite(CS, HIGH);
    }
    
    //This function will read a certain number of registers starting from a specified address and store their values in a buffer.
    //Parameters:
    //  char registerAddress - The register addresse to start the read sequence from.
    //  int numBytes - The number of registers that should be read.
    //  char * values - A pointer to a buffer where the results of the operation should be stored.
    void readRegister(char registerAddress, int numBytes, unsigned char * values){
      //Since we're performing a read operation, the most significant bit of the register address should be set.
      char address = 0x80 | registerAddress;
      //If we're doing a multi-byte read, bit 6 needs to be set as well.
      if(numBytes > 1)address = address | 0x40;
      
      //Set the Chip select pin low to start an SPI packet.
      digitalWrite(CS, LOW);
      //Transfer the starting register address that needs to be read.
      SPI.transfer(address);
      //Continue to read registers until we've read the number specified, storing the results to the input buffer.
      for(int i=0; i<numBytes; i++){
        values[i] = SPI.transfer(0x00);
      }
      //Set the Chips Select pin high to end the SPI packet.
      digitalWrite(CS, HIGH);
    }

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,280
    I'm not sure wether you need digitalPinToInterrupt()..or not.. did you try it without that ?

    Edit:
    ...and what happens with this variant ?:

    void DataReadyISR() {
    intFlag = HIGH;
    }
    Last edited by Frank B; 11-19-2015 at 10:00 PM.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,080
    Maybe trying INPUT_PULLUP mode on pin 9? Most of those sensor chips have open collector interrupt outputs, which require a pullup.

  4. #4
    Senior Member
    Join Date
    Jan 2014
    Posts
    181
    I have been successful using interrupts on the ADXL345. They don't need a pullup. You can specify how many samples in the FIFO will cause an interrupt (as I recall). I was NOT successful at getting an interrupt if I set this sample number to 1. Also, be careful about reading the values to clear the interrupt flag, but I suspect you are running into the same issue I ran into regarding set this sample number to 1.

  5. #5
    Junior Member
    Join Date
    Feb 2015
    Posts
    4
    Gentlemen,

    Thank you all very much for the suggestions.

    Frank - I removed the digitalPinToInterrupt() and replaced it with the standard syntax. Unfortunately no dice. Thanks however for ISR suggestion - ISR's don't come much shorter than that!

    Paul, thanks for the reminder about the internal pull-ups. Again unfortunately no luck, but I will bear this in mind for the future.

    Richard, thanks for info - good to know that someone else has experienced similar behaviour with this module.

    In light of the above, I think I'll re-write the code slightly to make use of the on-chip FIFO buffer and use the FIFO_CTL register. I will let you know how I get on.

    Kindest Regards and thanks again.
    Tm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •