Pages: [1]   Go Down
Author Topic: Looking for some help with positive edge trigger (flip flop)  (Read 2969 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

Essentially what I want to do is use a hall sensor (non latching) to make an RPM sensor.

Initially, what I was thinking of doing was measuring the time between rising edge pulses from the hall sensor and carrying out the appropriate calculations for RPM.

However, with this method, if the pulse widths are short, and I have other code to execute, I don't want to "miss" detecting a rising edge while executing other code.

Someone suggested that I use a positive edge trigger for more reliability? They said that this component would see a rising edge, and then output the digital HIGH signal for me until the next rising edge is detected, and then it will switch back to low. With this pulse sequencing, I could check for a HIGH signal, get the current time, then go about executing my other code, and keep checking back until a LOW is detected again, get the time and then proceed to find the difference in time. Although I may not detect the very instant that there is a switch from HIGH to LOW or LOW to HIGH, it essentially increases my pulse width greatly so that I will reliably detect a change.

OKAY! So everything sounds great, but I've looked them up and I'm confused. There is a Set, Reset, Q and Qnot. There are also many different types of flip flop logic possibilities. I tried running through the truth tables, but I'm a little confused as I'm still a bit new with this stuff.

Can somebody point me in the right direction with which one I might need? I would really appreciate it! Thank you!

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would point you to a much simpler method that would give you the same results but without using any additional external components. Use the interrupt feature available on either pins 2 or 3:

 http://arduino.cc/en/Reference/AttachInterrupt

Your interrupt could trigger on raising signal and just have a short ISR routine that stores a millis() count ( or micros() depending on the speed of the speed signal) on the first raising pulse and on the next raising count interrupt, subtracts the new current millis() count with the previous saved count. That would give you the time in millsecs (or microseconds) of the period of the signal.

The advantages of using interrupts is that your main loop program can be doing other stuff without having to worry about testing or measuring your speed probe frequency. The interrupt routine could just update a global variable with the last valid period time measurement and your main code could use it anytime it needs to.

Lefty
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8592
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I really don't see the need for external hardware, especially as you are now not detecting the edge so the timing may be off.

Using an interrupt will do the same job and be more determanistic, ie the latency from edge to read is about the same all the time with an interrupt. With the FF you only read the IP when you happen to get around to it.

So ditch the FF, but for future reference what you describe is a toggle or count function for the FF, tie /Q back to D, that way every time it gets a pulse on CLK the output will change state.

Also make sure the clock input is clean (ie debounced) or you may get multiple toggle per revolution.

______
Rob


« Last Edit: February 05, 2011, 12:15:20 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, I think I will try the interrupt feature before anything else. I wasn't aware that this was possible.

Still kinda new with Arduino, programming, etc.

Very helpful!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, I think I will try the interrupt feature before anything else. I wasn't aware that this was possible.

Still kinda new with Arduino, programming, etc.

Very helpful!


Here is something I threw together that might help you get started:

Code:
// frequency counter
// Uses pin 2 interupt to measure frequency of signal input
// Note micros() function has a 4 us resolution so 1000hz can be 996.02 or 1,000 at 1,000
// retrolefty 2/4/11

   
volatile unsigned long isrPeriod;
volatile unsigned long start_time;
volatile unsigned long timestamp;
volatile byte first = 1;
unsigned long speed;


void setup() {

  Serial.begin(57600);
  Serial.println ("Frequency counter starting");     // signal initalization done
  attachInterrupt(0, countP, RISING);
  delay(100);

  }  // End of setup


void loop() {
  delay(100);
  noInterrupts();
  long period = isrPeriod;
  interrupts();
  float freq = 1/(float(period) * .000001);
  speed = long(freq);
  Serial.print("Freq =  ");
  Serial.print(freq);
  Serial.print("   ");
  Serial.print(speed);
  Serial.println("  Hz.");
  delay(100);
 
}  // end of loop

void countP()
 {
   timestamp = micros();
   if (first)
    {
      start_time = timestamp;
      first = 0;
    }
    else
    {
      isrPeriod = timestamp - start_time;
      first = 1;     
    }
 }

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey thanks Lefty, that was going way above and beyond helpful!

I read up a bit more on the interrupt feature and your code was nice and easy to understand.

Big thanks!
Logged

Pages: [1]   Go Up
Jump to: