use of timer and interrupt to read ADXL345 values

Hi All,
Please can some one help me to write ISR to read data from ADXL345. I have done calculations for timer to read after every 500us. Also please can you tell me do I have to add another connection from arduino pin to interrupt PIN on ADXL345 or all this will happen internally in micro controller. Please find my code below. If i take timer and ISR bit from my code it is working and giving me correct values for xg, yg and zg. I want to use timer and interrupt to read accelerometer after every few milli/micro second. Thank you for help.

#include <SPI.h>
#define PINNUMBER
#include <TimerOne.h>

#define   DEVID      0x00         //Device ID Register

//offset adjustment register
#define   OFSX      0x1E         //X-axis offset
#define   OFSY      0x1F         //Y-axis offset
#define   OFSZ      0x20         //Z-axis offset

//Activity and Inactivity threshold Register
#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

//Interrupt 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

//Data Register
#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

//Data rate and power control register
#define   BW_RATE      0x2C    //Data rate and power mode control
#define POWER_CTL   0x2D       //Power Control Register

int CS=10;
long values[10];

int x,y,z; 
double xg, yg, zg; 
char ff=0;
long line = 0;

void setup(){ 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);                //configure accelerometer for SPI connecttion
  Serial.begin(9600);
  pinMode(CS, OUTPUT);                      //set chip select to be output
  digitalWrite(CS, HIGH);                   //set chip select to be high
 
  writeRegister(DATA_FORMAT, 0x0B);         //put accelerometer into +-16G range
  writeRegister(POWER_CTL, 0x08);           //Measurement mode 
  writeRegister (OFSX, 0x05);
  writeRegister (OFSY, 0x02);
  writeRegister (OFSZ, 0x10);
  
  cli();//stop interrupts
  //set timer0 interrupt at 2kHz or 500 micro second
  TCCR0A = 0;                               // set entire TCCR0A register to 0
  TCCR0B = 0;                               // same for TCCR0B
  TCNT0  = 0;                               //initialize counter value to 0
  OCR0A = 124;                              // set compare match register for 2khz increments = (16*10^6) / (2000*64) - 1 (must be <256)
  TCCR0A |= (1 << WGM01);                   // turn on CTC mode
  TCCR0B |= (1 << CS01) | (1 << CS00);      // Set CS01 and CS00 bits for 64 prescaler   
  TIMSK0 |= (1 << OCIE0A);                  // enable timer compare interrupt 
  sei();//allow interrupts
}

void loop()
{
    readRegister(DATAX0, 6, values);
    int i;
    x = ((int)values[1]<<8)|(int)values[0];
    y = ((int)values[3]<<8)|(int)values[2];
    z = ((int)values[5]<<8)|(int)values[4];

  Serial.println(); 
  for (i=0; i<6; i++)
   {
     Serial.print(values[i], DEC);
     Serial.print(" ");
   } 
    //convert accelerometer value to G
    xg = x * 0.0038;
    yg = y * 0.0038;
    zg = z * 0.0038;
  
    //Print the results to the terminal so that i can monitor the reading using the serial monitor.
    Serial.println ();
    Serial.print("xg: ");
    Serial.print((float)xg,2);
    Serial.print("g,");
    Serial.print("yg: ");
    Serial.print((float)yg,2);
    Serial.print("g,");
    Serial.print("zg: ");
    Serial.print((float)zg,2);
    Serial.println("g");
    delay(2000); 
  
}
  
    
void writeRegister(long registerAddress, long value)  //char registerAddress - The register to write a value to char value - The value to be written to the specified register.
{    //Set Chip Select pin low to signal the beginning of an SPI packet.
    digitalWrite(CS, LOW); // to signal beginning of SPI packet 
    SPI.transfer(registerAddress);
    SPI.transfer(value);
    digitalWrite(CS, HIGH); // to signal end of SPI packet
}

void readRegister(long registerAddress, long numBytes, long * values)
{
    long address = 0x80 | registerAddress; // to perform read operation the most significant bit of the register address must be set
    if(numBytes > 1)address = address | 0x40;   
    digitalWrite(CS, LOW);   
    SPI.transfer(address);
    for(int i=0; i<numBytes; i++)
    {
    values [i] = SPI.transfer(0x00);
    }
    digitalWrite(CS, HIGH);
}

//timer0 interrupt service routine to read accelerometer after every 500 micro second
ISR(TIMER0_COMPA_vect)
{

}                                                           PLEASE CAN YOU ADVISE WHat i should write in my ISR

Kind Regards
Shehzad

I want to use timer and interrupt to read accelerometer after every few milli/micro second.

Let's start with WHY.

You can use the blink without delay philosophy to read from the device whenever you want. Why add the complexity of timers and interrupts?

Hi PaulS,

Thank you for your reply. I am doing a project for my course work to design fall detection system to help elderly people. I want to use timer and interrupt to read accelerometer after every few milli second instead of getting constant readings . Also at later stage I have to use interrupt to turn by buzzer on if activity or inactivity values exceed defined threshold. Third main reason is I want to learn how to use interrupt. Theoretically I know what the interrupt is but I am struggling to implement this in code.

I am new to Arduino Please can you explain what is the blinky?

Thanks again for your interest.

Kind Regards#Shehzad

I’d forget about trying to read the accelerometer data via I2C in interrupt context - it’ll drive you crazy.
As suggested, take a look at blink without delay.

I want to use timer and interrupt to read accelerometer after every few milli second instead of getting constant readings .

You've made that statement twice now. What you have NOT explained is WHY? I could think of a few reasons, but my reasons don't matter. YOUR reasons are what is important. How you proceed depends on YOUR reason, and how critical it is to NOT miss a bump (rather, in my opinion).

Blink Without Delay is one of the example sketches that comes with the Arduino IDE. It shows how to use millis() to manage timing. You could also use micros() instead of millis() if necessary.

Don't use interrupts unless you are good at debugging your code.

...R