Author Topic: ADXL345 Arduino UNO data_ready interrupt  (Read 5823 times)

0 Members and 1 Guest are viewing this topic.

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
ADXL345 Arduino UNO data_ready interrupt
« on: February 17, 2015, 11:14:34 pm »
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: [Select]
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <avr/io.h>
#include <avr/power.h>


#define F_CPU 16000000UL


volatile int sensor_update=0;


//----------------------------------------------------------------------------------------------
//Write to ADXL345 registers
void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}



//----------------------------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////////////////////////////

//ISR function
 
  void interrupt(void){
  sensor_update=1;
       
  }
 

//////////////////////////////////////////////////////////////////////////////////////////////////


 
void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}


/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);





void setup(void)
{
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  Serial.begin(9600);
  //Serial.println("Accelerometer Test"); Serial.println("");

  pinMode(4, OUTPUT);// buzzer output pin
 
  /* Initialise the sensor */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    //Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while(1);
  }

  /* Set the range to whatever is appropriate for your project */
  accel.setRange(ADXL345_RANGE_16_G);
  accel.setDataRate(ADXL345_DATARATE_100_HZ);
  // displaySetRange(ADXL345_RANGE_8_G);
  // displaySetRange(ADXL345_RANGE_4_G);
  // displaySetRange(ADXL345_RANGE_2_G);
 
 
 
  //Create an interrupt that will trigger when a tap is detected.
  attachInterrupt(0, interrupt, RISING);
 
  writeTo(0x1D, 0x2E, 0);
  writeTo(0x1D, 0x2F, 0);
  writeTo(0x1D, 0x2E, 128);
  writeTo(0x1D, 0x2F, 127);
 
 

}

void loop(void)
{
 
 
 
 
 
  if(sensor_update==1 ){
    //When sensor_update is set to 1 in the ISR,the algorithm process the data from the accelerometer being updated every 10ms(100Hz)
     sensor_update=0;//reset
     
     
  }
 

}
« Last Edit: February 18, 2015, 01:37:50 am by Skyland »
 

Offline alex89

  • Contributor
  • Posts: 30
  • Country: it
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #1 on: February 18, 2015, 12:49:49 am »
Why are you calling interrupt() in the loop ?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #2 on: February 18, 2015, 01:21:46 am »
Quote
Because I update a new sample to the algorithm every time the data ready interrupt comes!

You are not understanding how interrupt works.
================================
https://dannyelectronics.wordpress.com/
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #3 on: February 18, 2015, 01:23:32 am »
Why are you calling interrupt() in the loop ?

good spot thanks.
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #4 on: February 18, 2015, 01:39:59 am »
After fixing that, the interrupt simply never kicks in now. The enable and map registers are set up correctly,could someone confirm that please.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #5 on: February 18, 2015, 02:21:39 am »
Quote
After fixing that, the interrupt simply never kicks in now.

How do you know that?

Also, make sure that you have connected the right pin, set it to the right mode, and cleared the flags in the isr correctly, ... -> read the datasheet for more details.

Quote
The enable and map registers are set up correctly,

How do you know that?
================================
https://dannyelectronics.wordpress.com/
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #6 on: February 18, 2015, 02:38:59 am »
I have read the datasheet, the pin is connected correctly etc.. and set the correct bit to enable the data_ready interrupt and map it to INT1.

Code: [Select]
writeTo(0x1D, 0x2E, 128); // enable data_ready
 writeTo(0x1D, 0x2F, 127); // map data_ready to INT1

What do you mean by clear the flags?
« Last Edit: February 18, 2015, 02:45:51 am by Skyland »
 

Online Mr.B

  • Supporter
  • ****
  • Posts: 1242
  • Country: nz
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #7 on: February 18, 2015, 02:52:23 am »
I have read the datasheet...

Did you read this bit?
"When initially configuring the interrupt pins, it is recommended that the functions and interrupt mapping be done before enabling the interrupts."

« Last Edit: February 18, 2015, 02:58:13 am by Mr.B »
Where are we going, and why are we in a handbasket?
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #8 on: February 18, 2015, 03:02:00 am »
I have read the datasheet...

Did you read this bit?
"When initially configuring the interrupt pins, it is recommended that the functions and interrupt mapping be done before enabling the interrupts."

Thanks for your reply.
INT_SOURCE register is read only, from the datasheet it seems that the INT_SOURCE register can be used instead of the enable and map.

Quote
"When initially configuring the interrupt pins, it is recommended that the functions and interrupt mapping be done before enabling the interrupts."
I tried mapping the pint first before enabling the DATA_READY and it didn't change the outcome.


« Last Edit: February 18, 2015, 03:08:12 am by Skyland »
 

Online Mr.B

  • Supporter
  • ****
  • Posts: 1242
  • Country: nz
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #9 on: February 18, 2015, 03:04:03 am »
Yeah, sorry about the INT_SOURCE bit... I was skim reading the datasheet.

If you don't enable the interrupt you will not be able to use it. Defeats the purpose of an interrupt.
Where are we going, and why are we in a handbasket?
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #10 on: February 18, 2015, 03:14:05 am »
Yeah, sorry about the INT_SOURCE bit... I was skim reading the datasheet.

If you don't enable the interrupt you will not be able to use it. Defeats the purpose of an interrupt.

not sure why you say the interrupt is not enabled?
 

Online Mr.B

  • Supporter
  • ****
  • Posts: 1242
  • Country: nz
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #11 on: February 18, 2015, 03:17:18 am »
I thought you had implied that you were going to do that when you said:
"...the INT_SOURCE register can be used instead of the enable and map..."
Where are we going, and why are we in a handbasket?
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #12 on: February 18, 2015, 06:16:05 pm »
Does pin2 need to be set up as in input I wonder
 

Online Mr.B

  • Supporter
  • ****
  • Posts: 1242
  • Country: nz
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #13 on: February 18, 2015, 06:30:47 pm »
I am pretty sure it does...
Where are we going, and why are we in a handbasket?
 

Offline SkylandTopic starter

  • Contributor
  • Posts: 26
Re: ADXL345 Arduino UNO data_ready interrupt
« Reply #14 on: February 18, 2015, 06:38:33 pm »
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.

edit2: reading the INT_source register doesn't seem to toggle the interrupt either.
« Last Edit: February 19, 2015, 12:19:59 am by Skyland »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf