ADXL345 Arduino UNO data_ready interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by Skyland, Feb 17, 2015.

  1. Skyland

    Thread Starter New Member

    Jul 1, 2014
    28
    0
    I would like the sensor values to be updated each 10ms(100Hz) and then run the algorithm and repeat the same process. However, after timing the algorithm it is taking only 2ms, I think the data_ready interrupt is not working as expected. Physical hardware connection is from INT1 of ADXL345 to pin2 of the UNO.

    Code (Text):
    1. #include <Wire.h>
    2. #include <Adafruit_Sensor.h>
    3. #include <Adafruit_ADXL345_U.h>
    4. #include <avr/io.h>
    5. #include <avr/power.h>
    6.  
    7.  
    8. #define F_CPU 16000000UL
    9.  
    10.  
    11. volatile int sensor_update=0;
    12.  
    13.  
    14. //----------------------------------------------------------------------------------------------
    15. //Write to ADXL345 registers
    16. void writeTo(int device, byte address, byte val) {
    17.    Wire.beginTransmission(device); //start transmission to device
    18.    Wire.write(address);        // send register address
    19.    Wire.write(val);        // send value to write
    20.    Wire.endTransmission(); //end transmission
    21. }
    22.  
    23.  
    24.  
    25. //----------------------------------------------------------------------------------------------
    26.  
    27. /////////////////////////////////////////////////////////////////////////////////////////////
    28.  
    29. //ISR function
    30.  
    31.   void interrupt(void){
    32.   sensor_update=1;
    33.      
    34.   }
    35.  
    36.  
    37. //////////////////////////////////////////////////////////////////////////////////////////////////
    38.  
    39.  
    40.  
    41. void buzz(int targetPin, long frequency, long length) {
    42.   long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
    43.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
    44.   //// there are two phases to each cycle
    45.   long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
    46.   //// multiply frequency, which is really cycles per second, by the number of seconds to
    47.   //// get the total number of cycles to produce
    48.   for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    49.     digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    50.     delayMicroseconds(delayValue); // wait for the calculated delay value
    51.     digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    52.     delayMicroseconds(delayValue); // wait againf or the calculated delay value
    53.   }
    54. }
    55.  
    56.  
    57. /* Assign a unique ID to this sensor at the same time */
    58. Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
    59.  
    60.  
    61.  
    62.  
    63.  
    64. void setup(void)
    65. {
    66.   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
    67.   Serial.begin(9600);
    68.   //Serial.println("Accelerometer Test"); Serial.println("");
    69.  
    70.   pinMode(4, OUTPUT);// buzzer output pin
    71.  
    72.   /* Initialise the sensor */
    73.   if(!accel.begin())
    74.   {
    75.     /* There was a problem detecting the ADXL345 ... check your connections */
    76.     //Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    77.     while(1);
    78.   }
    79.  
    80.   /* Set the range to whatever is appropriate for your project */
    81.   accel.setRange(ADXL345_RANGE_16_G);
    82.   accel.setDataRate(ADXL345_DATARATE_100_HZ);
    83.   // displaySetRange(ADXL345_RANGE_8_G);
    84.   // displaySetRange(ADXL345_RANGE_4_G);
    85.   // displaySetRange(ADXL345_RANGE_2_G);
    86.  
    87.  
    88.  
    89.   //Create an interrupt that will trigger when a tap is detected.
    90.   attachInterrupt(0, interrupt, RISING);
    91.  
    92.   writeTo(0x1D, 0x2E, 0);
    93.   writeTo(0x1D, 0x2F, 0);
    94.   writeTo(0x1D, 0x2E, 128);
    95.   writeTo(0x1D, 0x2F, 127);
    96.  
    97.  
    98.  
    99. }
    100.  
    101. void loop(void)
    102. {
    103.  
    104.  
    105.   /* Get a new sensor event */
    106.   interrupt();
    107.  
    108.  
    109.  
    110.   if(sensor_update==1 ){
    111.     //When sensor_update is set to 1 in the ISR,the algorithm process the data from the accelerometer being updated every 10ms(100Hz)
    112.      sensor_update=0;//reset
    113.    
    114.    
    115.   }
    116.  
    117.  
    118. }
    119.  
     
  2. Skyland

    Thread Starter New Member

    Jul 1, 2014
    28
    0
    After fixing the interrupt call from the loop, the interrupt simply never kicks in now. The enable and map registers are set up correctly,could someone confirm that please.
     
  3. Skyland

    Thread Starter New Member

    Jul 1, 2014
    28
    0
    after some troubleshooting: putting pin2 as an input doesn't change anything.
    testing if the output from ADXL INT1 is toggling with 1Hz output rate with a multimeter give 0V so the problem is coming from the accelerometer, somehow it is not generating anything.

    edit: I have changed the device to 0x53 instead of 0x1D and now the LED is always on, still not toggling tough.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Page 26 of the ADXL345 seems to suggest that you'd need to read from DATAX, DATAY, and DATAZ to clear the interrupt.
     
  5. Skyland

    Thread Starter New Member

    Jul 1, 2014
    28
    0
    the library is doing that for me, ]inside the loop I have
    Code (Text):
    1. if(sensor_update==1){
    2. sensors_event_t event;
    3. accel.getEvent(&event);
    4. //call the the magnitude calculation function
    5. mag_calculation(event.acceleration.x,event.acceleration.y,event.acceleration.z);
    6. // rest of algorithm
    7. sensors_update=0; }
    8.  
     
Loading...