Attiny2313 interrupt INT0, INT1

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

Hi,

I'm working on a small project that handle 2 buttons using input pin (INT0 and INT1) changes. I'm interested for measuring signal width on rising edge.
The MCU is Attiny2313, 8Mhz (internal clock),8prescaler, T0.
When the inputs are triggered then count should begin and measure the time until is ON.
The "˜full/complete' time is ~380ms and if the signal width is less then ~190ms, should fired an output pin and to not take sample until the switch is off for ~500ms...
if the signal width is more than ~380ms than should wait 2s before take new sample. If the switch ON time is ~380ms than should not take any sample until the switch is off for more than ~500ms
"˜~' means approximately!

I've written something but isn't good at all :(
The test I've make is for INT0:

#include 
#include 

#define Active_on  PORTD |= (1<<1)
#define Active_off   PORTD &= ~(1<<1)

#define Active_on1  PORTD |= (1<<0)
#define Active_off1   PORTD &= ~(1<<0)

volatile uint32_t tick=0;
volatile uint32_t Atick=0;
volatile uint32_t comf=0;

// input
#define sw_on   PIND & (1<<2)
#define sw_off   ~PIND & (1<<2)

ISR(TIMER0_OVF_vect)
{
static unsigned char CountBlinkTime = 0;
static unsigned char one_blink = 0;

   TCNT0 += 6; // Adjust count so we get 250 uS rollover period
	
   if (++CountBlinkTime == 4) //1 ms
   {
     CountBlinkTime = 0;
	
	if (++one_blink==50) // 50 ms
	{
	one_blink = 0;//reset
 	tick++; // 
	Atick++;
	}
  }
   }

int main (void)
{

DDRD |= (0 << 2); // Set as input
DDRD |= (1 << 0); // test as output
DDRD |= (1 << 1); // test as output

  // Set Pin 6 (PD2) as the pin to use for this example
  PCMSK |= (1<<PIND2);

  // interrupt on INT0 pin falling edge 
  MCUCR = (1<<ISC01); //| (1<<ISC00);

  // turn on interrupts!
  GIMSK  |= (1<<INT0);

  // Setup Timer 0 
   TCCR0B |= (1 << CS01); // 8 divider
   TCNT0 = 0;            // Initial value 

   TIMSK |= (1 << TOIE0);      // Timer 0 Interrupt

  sei(); //  Enable global interrupts

 while (1) {

if (tick > 4) 
 {
 comf=0;
// Active_off;
}

else if ((tick > 0) && (tick < 4) && (comf==0)) 
{
Atick=0;
Active_on ;
comf=1;
}

if (Atick==20)
{
Active_off;
} 
else if (Atick==30)
{
comf=0;
}

if (comf==0)
{
Active_off1; 
} else
{
Active_on1;
}
}
}

SIGNAL (SIG_INT0)
{ 
    // Do some stuff
tick=0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I’m working on a small project that handle 2 buttons using input pin (INT0 and INT1) changes

Buttons + external interrupts = extraordinarily bad idea

(you have heard of "bounce" I take it?)

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

clawson wrote:

Buttons + external interrupts = extraordinarily bad idea

Why? What bad?

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

Because buttons bounce. While it is possible to debounce buttons using interrupts, it ends up being more complicated than just using a polling method (and in the process you lose the dedicated interrupt for something it could be useful for).

Regards,
Steve A.

The Board helps those that help themselves.

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

Your very first job is to format your code to make it pretty.

Life is far easier if you use the 16-bit Timer1.

If you are not tied to INT0, INT1 pins, you could use the Timer Capture Pin ICP1.

Google for "switch bounce". It is very important for things like a typewriter key or modal button. For a button that only does one job, you can assume that the very first glitch is 'good enough' to signal the start.

Cats can be skinned in multiple ways. Choose a method that suits you (and your cat).

David.

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

Quote:

Why? What bad?

Do you have any idea why I might have also asked:
Quote:
(you have heard of "bounce" I take it?)

If you read that and you had not heard of "bounce" did this not trigger a thought such as "I wonder what he means by "bounce"? I must use the search engine or Google to find out"

If you did that you would have arrived here:

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

Your thread search would have also lead to interesting threads by "danni", "theusch" and "JohanEkdahl" on the subject.

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

Quote:

For a button that only does one job, you can assume that the very first glitch is 'good enough' to signal the start.

Scene: James-Bond-type movie. Our hero Jimbo is confronting the evil megalomaniac HWMNBN. The key has been inserted into the security cover and it has been lifted, exposing the big red button, one press of which will fire a space-platform-mounted laser and drill a hole through the earth from Lund, Sweden to Baulkham Hills, NSW 2153 Australia. Of course, the mad HWMNBN has no intention of actually doing this since near the beginning of the bore it would ruin the water supply used to brew his favourite ale.

Unfortunately, the walk across the carpet had build up a static charge, and when the threatening finger got near the button an arc discharge resulted in the premature ejaculation of the powerful laser light.

"Oh, no!", he exclaimed, "I figured I didn't need to debounce, because it is a button that only does one job."

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Koshchi wrote:
Because buttons bounce. While it is possible to debounce buttons using interrupts, it ends up being more complicated than just using a polling method (and in the process you lose the dedicated interrupt for something it could be useful for).

I agree that the use of two interrupt for buttons, it's nonsense , but use a polling method only , this lose time MCU useless . If add the signals buttons and use INT0 or INT1 or same other interrupts this will be more is productive.
To debounce buttons you use program processing of buttons, where first command will be - to reset bit in General Interrupt Mask Register.

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

Quote:

this lose time MCU useless

It takes CPU cycles whichever way you do it. The idea of "polling" is that you only poll in a timer ISR so it's only a fraction of a percent of the CPU time that is used to do it and the rest of the time the CPU gets on with other jobs.

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

ILYAUL wrote:
[...this lose time MCU useless...

The reality is, you need to examine the switch over some 10-30ms to debounce it. Interrupts alone can't do that, because interrupts alone know nothing about the passage of time.

You could start a timer (or capture a running timer value) upon an interrupt, and jump through hoops to differentiate a proper button push from a glitch or a button release, but it won't be fun.

Alternately, you check the buttons from a timer tick ISR every X ms and report a debounced button push after Y consecutive identical detected button pushes. The method I like to use takes 10 lines of code not including variable declarations, and that includes a first repeat delay and subsequent repeat delays. Others have different code, but the idea is the same.

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

Timer :shock: - this is not "fatly " for the buttons? :)

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

ILYAUL wrote:
Timer :shock: - this is not "fatly " for the buttons? :)

WTF?
If you don't want to use software debouncing, you can use external SR latches between the switch and the EINT input.
The SR latch will capture the first make & then lock out any debouncing until reset.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

In general ,wherefore the timer, wherefore the of interrupts or external SR which to work with buttons. There is ADC - can to make 64 the buttons on one channel. The bounce - can forget forever.

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

Quote:
In general ,wherefore the timer, wherefore the of interrupts or external SR which to work with buttons. There is ADC - can to make 64 the buttons on one channel. The bounce - can forget forever.

WTF are you on?

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

Quote:

The method I like to use takes 10 lines of code not including variable declarations, and that includes a first repeat delay and subsequent repeat delays. Others have different code, but the idea is the same.


Yes, to debounce a bank of 8 is a few microseconds every few milliseconds. Roughly 0.1% of AVR bandwidth.

If that is a waste, consider all the schemes to >>avoid<< proper debouncing, such as multiple reads and in-line delays.

And even if you say that is efficient, in the end my apps need rising and/or falling edge triggers, as well as key-made state, and often combinations. Held-down timers has already been mentioned. All of those things practically fall out of the debounce.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

You could use the ADC and loop until voltage reachs some very low value. Then start a timer and poll the ADC until it's max. count. Stop timer and ...
Rise time is defined between 10% and 90% of the voltage value, so you can use those for the ADC counts to look for.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

indianajones11 wrote:
You could use the ADC and loop until voltage reachs some very low value. Then start a timer and poll the ADC until it's max. count. Stop timer and ...
Rise time is defined between 10% and 90% of the voltage value, so you can use those for the ADC counts to look for.

Don't need use timer. Only ADC - it's all. I use its allways. It's same "classic" method.
Quote:
WTF are you on?

What is WTF?

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

ILYAUL wrote:

What is WTF?
В буквальном переводе - "Шо за хуйня?" :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

How does the ADC negate bounce? Even when sampling analog values I apply filtering as noise and transients might ruin my day. Any sort of input from the outside world should have some sort of filtering applied to it for this very reason. Even PLCs have this feature built in.

I think the OP needs to read the tutorial I wrote on 'the traps of using interrupts'. We have shared 32bit variables and no means of ensuring atomicity.

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

How will an ADC convert this to a single "button pushed" event?

http://www.google.com/imgres?um=...

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

Clawson asked the following

Quote:
(you have heard of "bounce" I take it?)

We haven't seen a definitive answer from the OP, but I think we can read between the lines!

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

I didn't indicate that it was for debouncing . Just a quick and dirty straight read of what corresponds to 10% and 90% of whatever Vcc is, during the first risetime it should work since that's the info she wants and it hasn't started bouncing during the 1st transition . Of course a Schmitt or other type of debouncer would make it possible to then use the INTx pins.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

MBedder wrote:
ILYAUL wrote:

What is WTF?
В буквальном переводе - "Шо за хуйня?" :lol:

Может дать им рабочий код , пусть попробуют? И усреднение не проходили повидимому.

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

что ебет Я мочу на Волге

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

kk6gm wrote:
How will an ADC convert this to a single "button pushed" event?
Easily. The ADC + resistor ladder + pushbuttons solution is used in virtually any car radio. Consider how many car radios are in use and think again :P

P.S. Let's stop Russian excercises from now on - I have a feeling somebody is not quite ready for these yet :D

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

MBedder wrote:
kk6gm wrote:
How will an ADC convert this to a single "button pushed" event?
Easily. The ADC + resistor ladder + pushbuttons solution is used in virtually any car radio. Consider how many car radios are in use and think again :P

I think the discussion may be diverging here. Yes, ADCs and resistor networks can allow one channel to read multiple buttons. No (I maintain), they alone cannot debounce those buttons without also introducing the element of time into the equation.

Quote:
P.S. Let's stop Russian excercises from now on - I have a feeling somebody is not quite ready for these yet :D

да (I had to cut and paste that...)

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

kk6gm wrote:
I think the discussion may be diverging here. Yes, ADCs and resistor networks can allow one channel to read multiple buttons. No (I maintain), they alone cannot debounce those buttons without also introducing the element of time into the equation.

The element of time is calculated themselves ADC. Take diagram work the buttons (attached) and to add with diagram work ADC and use the formula (Tadc=(14*K )/Fadc , where K may be from 1-1024). You will understand why the debounce don't affect on ADC. He neutralize (smooth,mean)its. And if you will do small corridor for values (+1/0/-1) ADC , you define buttons allways.

Attachment(s): 

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

ILYAUL wrote:
kk6gm wrote:
I think the discussion may be diverging here. Yes, ADCs and resistor networks can allow one channel to read multiple buttons. No (I maintain), they alone cannot debounce those buttons without also introducing the element of time into the equation.

The element of time is calculated themselves ADC. Take diagram work the buttons (attached) and to add with diagram work ADC and use the formula (Tadc=(14*K )/Fadc , where K may be from 1-1024). You will understand why the debounce don't affect on ADC. He neutralize (smooth,mean)its. And if you will do small corridor for values (+1/0/-1) ADC , you define buttons allways.

I'm sorry, I can't follow your English. But I can say that the element of time can be introduced in different ways. By using a hardware timer (efficient use of CPU). By polling and looping (inefficient use of CPU). By adding external components such as RC networks. So which does your ADC method employ?

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

The ADC is sampling an input just once during a short time (Tsamp), then the ADC converts a sampled voltage (stored in a sample-hold capacitor) during a longer time (Tconv). This alone can be used as a simple debouncing aid, and some programming efforts allow for completely debounce the ADC input exactly as it is being done with pushbuttons connected to the digital inputs.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

MBedder wrote:
The ADC is sampling an input just once during a short time (Tsamp), then the ADC converts a sampled voltage (stored in a sample-hold capacitor) during a longer time (Tconv). This alone can be used as a simple debouncing aid, and some programming efforts allow for completely debounce the ADC input exactly as it is being done with pushbuttons connected to the digital inputs.

OK, we're getting tantalizingly close to an understanding. I understand Tsamp and Tconv, but how does the ADC method avoid reporting 10 keypresses when the key bounces 10 times before settling?

To add some numbers (which you are free to change), let's say the key bounces once every 1ms, for 10ms, before settling. Let's also say Tsamp is 10us, and Tconv is 100us. Explain how the debouncing works.

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

Hint: take three subsequent ADC samples separated by 10 mS or so. Find two of the three whos values are close to each other, store any one of the two, then compare this new value with one stored in a previous 3-sample iteration. If these values are almost equal - do nothing, if they differ - you got the pushbutton event.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

wow how many replay :)

Thanks for trying to help!
I've thought for button debouncing ....software one, but unfortunately doesn't work as i expect.
Now i'm using simply just the timer0 and check the switch status.
At this moment cannot test it properly because don't have a signal generator. I will open a new topic after the test, because here is a mess and the subject will not be anymore INTx inputs.

And back to INT0, now the question is how do i measure signal width on rising edge and falling edge? not in the same time on one input!
The signal is coming from a HALL. no mechanical switch.

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

MBedder wrote:
...separated by 10 mS or so...

Oh, well yeah. :)

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

mzchristensen wrote:
And back to INT0, now the question is how do i measure signal width on rising edge and falling edge? not in the same time on one input!
The signal is coming from a HALL. no mechanical switch.

Use an input capture pin. Let the associated timer free-run (no CTC modes), configure the capture pin to capture on rising edge and generate an interrupt. Inside the ISR for rising capture, save the captured time and re-configure to capture and interrupt on falling edge. Inside the ISR for falling capture, save the captured time and re-configure back to rising edge, and set a flag for background code.*

In background code, when flag is set, calculate pulse width as time_at_falling_edge - time_at_rising_edge. This will be valid as long as the pulse width is less than the period of the timer.

* or do the whole calculation in the ISR, whatever makes the most sense.

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

Quote:
Hint: take three subsequent ADC samples separated by 10 mS or so.
Yes, you can debounce using ADC. What ILYAUL seems to be saying is that simply reading the ADC does the debouncing (i.e. one sample).

Regards,
Steve A.

The Board helps those that help themselves.

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

Mzchristensen,so the Hall output is digital, right ? If so then using the ICP is probably the best as KK6gm suggested, since it does evertyhing in one unit. Were you using the buttons in your OP code to test code before using the Hall sensor ?

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Koshchi wrote:
What ILYAUL seems to be saying is that simply reading the ADC does the debouncing (i.e. one sample).
Maybe. I just couldn't reverse engineer his English back to our Russian then forth to our English :D

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Hall effect switches have hysteresis and produce clean bounce free outputs, that can be directly interfaced to EINT's.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

MBedder wrote:
The ADC is sampling an input just once during a short time (Tsamp), then the ADC converts a sampled voltage (stored in a sample-hold capacitor) during a longer time (Tconv). This alone can be used as a simple debouncing aid, and some programming efforts allow for completely debounce the ADC input exactly as it is being done with pushbuttons connected to the digital inputs.

I do so. I take 128 or 256 samples ADC ( high byte) and adding up them ( is used interupt ADC) got the mean value. Compared with table and is accepted a solution - button or not. If do 256 samples I have the mean value in the higher register , that economize the time. (for 128 - need one shift ) . In the table is used only one value for every the buttons. Can compare with one , two or three. Usualy is the higher value from three feasible.

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

LDEVRIES wrote:
Hall effect switches have hysteresis and produce clean bounce free outputs, that can be directly interfaced to EINT's.
OK then, I've checked and the t2313 doesn't have high-level ISR setting, so you can't config. for rising and then re-config. for high-level, to do a rise time measurement .

1) How about the comparator set to the ~90%*Vcc mark, do a rising INTx then start a timer in it and have a comparator ISR and stop the timer in it ?

2) The ADC would work but you'd have to do it all in the rising edge INTx .

3) Put an inverter on the INTx pin, config. for falling edge, start the timer, set a flag and reconfig. for low-level. When it fires the 2nd time, stop the timer .

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

I need 4 separate inputs (no ADC) and the MCU only one ICP have(i've check a few of them). The MCU is not critical, i could use Atmega 8 without problem.

I don't need to change state of the input...from falling to rising edge! for ex. INT0 => rising edge and INT1 => falling edge.

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

Quote:
I do so. I take 128 or 256 samples ADC ( high byte) and adding up them ( is used interupt ADC) got the mean value.
But why do you think that this is not a polling method?

Regards,
Steve A.

The Board helps those that help themselves.

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

Probably because -

Quote:
( it uses interrupt ADC)

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.