ADXL345 Arduino UNO data_ready interrupt - Page 1
Forum section: electronics / general

author: Skyland
author_rank: Contributor
timestamp: February 17, 2015, 11:14:34 pm
content: 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:#include <Wire.h>#include <Adafruit_Sensor.h>#include <Adafruit_ADXL345_U.h>#include <avr/io.h>#include <avr/power.h>#define F_CPU 16000000ULvolatile int sensor_update=0;//----------------------------------------------------------------------------------------------//Write to ADXL345 registersvoid writeTo(int device, byte address, byte val) {Wire.beginTransmission(device); //start transmission to deviceWire.write(address); // send register addressWire.write(val); // send value to writeWire.endTransmission(); //end transmission}//----------------------------------------------------------------------------------------------///////////////////////////////////////////////////////////////////////////////////////////////ISR functionvoid 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 cyclelong 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 producefor (long i=0; i < numCycles; i++){ // for the calculated length of time...digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphramdelayMicroseconds(delayValue); // wait for the calculated delay valuedigitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphramdelayMicroseconds(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}}

author: alex89
author_rank: Contributor
timestamp: February 18, 2015, 12:49:49 am
content: Why are you calling interrupt() in the loop ?

author: dannyf
author_rank: Super Contributor
timestamp: February 18, 2015, 01:21:46 am
content: QuoteYou are not understanding how interrupt works.

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 01:23:32 am
content: Quote from: alex89 on February 18, 2015, 12:49:49 amgood spot thanks.

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 01:39:59 am
content: After fixing that, the interrupt simply never kicks in now. The enable and map registers are set up correctly,could someone confirm that please.

author: dannyf
author_rank: Super Contributor
timestamp: February 18, 2015, 02:21:39 am
content: QuoteHow 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.QuoteHow do you know that?

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 02:38:59 am
content: 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:writeTo(0x1D, 0x2E, 128); // enable data_readywriteTo(0x1D, 0x2F, 127); // map data_ready to INT1What do you mean by clear the flags?

author: Mr.B
author_rank: Unknown
timestamp: February 18, 2015, 02:52:23 am
content: Quote from: Skyland on February 18, 2015, 02:38:59 amDid 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."

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 03:02:00 am
content: Quote from: Mr.B on February 18, 2015, 02:52:23 amThanks 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.QuoteI tried mapping the pint first before enabling the DATA_READY and it didn't change the outcome.

author: Mr.B
author_rank: Unknown
timestamp: February 18, 2015, 03:04:03 am
content: 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.

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 03:14:05 am
content: Quote from: Mr.B on February 18, 2015, 03:04:03 amnot sure why you say the interrupt is not enabled?

author: Mr.B
author_rank: Unknown
timestamp: February 18, 2015, 03:17:18 am
content: I thought you had implied that you were going to do that when you said:"...the INT_SOURCE register can be usedinsteadof the enable and map..."

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 06:16:05 pm
content: Does pin2 need to be set up as in input I wonder

author: Mr.B
author_rank: Unknown
timestamp: February 18, 2015, 06:30:47 pm
content: I am pretty sure it does...

author: Skyland
author_rank: Contributor
timestamp: February 18, 2015, 06:38:33 pm
content: 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.