Pulse counting

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would like to count interrupts on rising edge using int0 and int1 pins and display them on LCD screen here is relevant code(I am using 328p):

 

EICRA |= (1 << ISC00) || ( 1 << INT1 ) ;    // set INT0 and INT1 to trigger on rising edge
EIMSK |= (1 << INT0) || ( 1 << INT1 );        // Enable both interrupts

ISR(INT0_vect)
{
PL++;       // Increment the pulse counter by one each time
}

ISR(INT1_vect)
{
AL++;       // Increment the pulse counter by one each time
}

PL value is already going crazy going from 0 to 9999 in a second. Any idea why this is happening?

This topic has a solution.
Last Edited: Mon. Jun 10, 2019 - 04:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hirajus wrote:
Any idea why this is happening?
Most likely "Bounce":

 

http://www.ganssle.com/debouncin...

 

That affects any kind of metal/mechanical "switch" including encoders. As the contacts close or open there are multiple (could be as many as 100 make/breaks) before it settles into a constant open/closed state.

 

Either use suggestions such as RC components to debounce or do it in software (which involves timing and looking for the input state remaining stable for a reasonable amount of time.

 

What are connected to your INT0/INT1 pins? Is it something "bouncy" ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please fix it like this.

 

EICRA = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00)) ;    // set INT0 and INT1 to trigger on rising edge
EIMSK = (1 << INT0) | ( 1 << INT1 );        // Enable both interrupts

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Hirajus wrote:
Any idea why this is happening?
Most likely "Bounce":

 

http://www.ganssle.com/debouncin...

 

That affects any kind of metal/mechanical "switch" including encoders. As the contacts close or open there are multiple (could be as many as 100 make/breaks) before it settles into a constant open/closed state.

 

Either use suggestions such as RC components to debounce or do it in software (which involves timing and looking for the input state remaining stable for a reasonable amount of time.

 

What are connected to your INT0/INT1 pins? Is it something "bouncy" ?

 

Well here's the thing, a photodiode is connected to INT0, and I noticed that INT1 allways stay at 0 even when I connected a button and pushed it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See kabasan's reply above - I just gave the "stock answer" (ie "INT + multiple = bounce") but didn't stop to examine your code. Clearly EICRA is set wrong

Last Edited: Mon. Jun 10, 2019 - 02:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kabasan wrote:

Please fix it like this.

 

EICRA = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00)) ;    // set INT0 and INT1 to trigger on rising edge
EIMSK = (1 << INT0) | ( 1 << INT1 );        // Enable both interrupts

 

 

It didn't fix the my problem but thanks for suggestion.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

See kasban's reply above - I just gave the "stock answer" (ie "INT + multiple = bounce") but didn't stop to examine your code. Clearly EICRA is set wrong

 

I corrected my code according to that suggestion but sadly the problem persists.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show schematic of exactly what is attached to INT0 (and hence increments PL). You say "photodiode" but do you have a schmitt trigger or something else for hysteresis on that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Here it is but I doubt it is going to help because I tried putting there a push button or just disconnecting it all together but result doesn't change so I believe my code is at fault, should I just post all of it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please check your current code again.
Maybe this is something like this?

EICRA |= (1 << ISC11) || (1 << ISC10) || (1 << ISC01) || (1 << ISC00) ;
EIMSK |= (1 << INT0) || ( 1 << INT1 );

Here is the code I recommend. Do you see the difference?

EICRA = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00) ;
EIMSK = (1 << INT0) | ( 1 << INT1 );

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kabasan wrote:

Please check your current code again.
Maybe this is something like this?

EICRA |= (1 << ISC11) || (1 << ISC10) || (1 << ISC01) || (1 << ISC00) ;
EIMSK |= (1 << INT0) || ( 1 << INT1 );

Here is the code I recommend. Do you see the difference?

EICRA = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00) ;
EIMSK = (1 << INT0) | ( 1 << INT1 );

 

 

I'm absolutely sure that I am using:

EICRA = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00) ;
EIMSK = (1 << INT0) | ( 1 << INT1 );

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hirajus wrote:
Here it is but I doubt it is going to help because I tried putting there a push button or just disconnecting it all together

INT0 is connected to the LCD display???   How can this be used as an input?

 

If disconnected, then a pin will float (assuming its an input) if internal pull up is not enabled, a floating input can generate many interrupts as it wonders around the 0/1 threshold!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Hirajus wrote:
Here it is but I doubt it is going to help because I tried putting there a push button or just disconnecting it all together

INT0 is connected to the LCD display???   How can this be used as an input?

 

If disconnected, then a pin will float (assuming its an input) if internal pull up is not enabled, a floating input can generate many interrupts as it wonders around the 0/1 threshold!

 

Jim

 

 

Not sure where you got that since INT0 is connected to diode. And it doesn't seem to matter how I connect INT0 and INT1 since result is always the same.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The photodiode is connected to "PCINT0", and not to "INT0".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the bottom line is that INT0 is "E" to the LCD. Not surprising if there's quite a lot of activity on that particular line !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your photodiode is connected to PCINT0, not INT0. Why not just use it as input to a counter?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kabasan wrote:

The photodiode is connected to "PCINT0", and not to "INT0".

 

Thanks dude, you just solved my problem. I just realized I might be retarded though but still lots of thanks.