Go Down

Topic: Interrupts and Timers (Read 209 times) previous topic - next topic

kulkarnipurvesh

HI,
I am trying to write a code for arduino due which starts a 32bit timer on external interrupt , saves the value in a variable and then resets the value when a new external interrupt  comes and does this in loop.
Can someone please help me with the sketch.

MorganS

The problem is in the code you didn't post. Please use code tags otherwise the forum software eats some of your code and turns it into smileys. 8)
GoForSmoke: "What GShield? You never mentioned a shield."

kulkarnipurvesh

okay so this is the sketch
what it does is , it writes value in t1 whenever interrupt is generated and then stores the previous value in t2. "X" stores the difference value i.e x=t1-t2
By this i can find the time difference between two interrupts.
I have generated the interrupt by using delay function and digitalWrite (as you can see in the sketch) and connected the output to pin2 (int pin of arduino).
by using this i get a resolution of 2^23 bit in arduino due . As arduino due is a 32 BIT uc i could use its resolution to count the same. If i could use the internal timers of arduino due that would help me a lot.
Help is highly appreciated.


Code: [Select]
volatile unsigned long t1;
volatile unsigned long t2;
volatile unsigned long x;
int flashpin = 10;

void isr()
{
  t1=micros();
}

void setup()
{
  pinMode(flashpin,HIGH);
}

void loop()
{
  attachInterrupt(0,isr,RISING);
  flash();
  x=t1-t2;
  t2=t1;
}

void flash()
{
  digitalWrite(flashpin,HIGH);
  delay(1000);
  digitalWrite(flashpin,LOW);
  delay(1000);
}

MorganS

Do you need this to be a blocking function? That is it sits and waits until the input pin rises? Then just use pulseIn() or the NewPing library

The code you have seems to have no way to tell if the interrupt has happened. It just delays for thousands of milliseconds and then you may or may not have had an interrupt. There may have been several. Additionally, you re-attach the interrupt on every loop, which is wasteful, and you never zero out t1 for the next loop so you might be looking at the previous event, not the current one.

To do this with the highest possible resolution, you should be using the "input capture" mode of the timer. I have not successfully made this work myself, although I have tried. Look for some tutorials on input capture.
GoForSmoke: "What GShield? You never mentioned a shield."

kulkarnipurvesh

Thanks for the reply MorganS  :) , but i have got something else to ask.
okay framing my question in different way now.
I want to read the time between two input impulse sensed by using interrupt function of arduino and the time can be read by starting a counter on rising edge and storing the value just after a new rising edge comes and reseting the counter. The resolution of the time depends on the bits of counter whether it is 16 or 32.
so far i am not able to use the internal counters directly.
Could you please help me with the sketch?

MorganS

I don't have a good sketch that demonstrates input capture. Maybe Nick Gammon does. Use Google. It knows him well. The Arduino playground might have an example. Google works on that too. Anything in "instructables" should be avoided though.

You did not answer my question: Can this block and wait or does your sketch have other work to do while it's waiting for the rising edge?
GoForSmoke: "What GShield? You never mentioned a shield."

ninja2

Then just use pulseIn() or the NewPing library
Does NewPing support Due? In the bitbucket notes for V1.7 it says:

"Removed support for the Arduino Due and Zero because they're both 3.3 volt boards and are not 5 volt tolerant while the HC-SR04 is a 5 volt sensor. Also, the Due and Zero don't support pin manipulation compatibility via port registers which can be done (see the Teensy 3.2)."


MorganS

Good catch. I started following links from the old Ping library and I did read that line, but at that point I had forgotten that the OP had quite clearly specified "for Arduino Due".
GoForSmoke: "What GShield? You never mentioned a shield."

ninja2

Curious ... I don't see 5V intolerance as a good reason to remove Due support from the library. After all there's many many ways to damage an Arduino and components (don't ask !  :) ), and it would be easy enough to build a 5V tolerant circuit to drive the HC-SR04

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy