This Question is Answered (go to answer)

2 "helpful" answers available (2 pts)
324 Views 5 Replies Last post: Dec 15, 2009 2:41 AM by FrankL RSS
KaZaNoVa64 Newbie 2 posts since
Jan 1, 2000
Currently Being Moderated

Dec 12, 2009 6:00 AM

int interrupt problem

Hi all,

 

I am using m32c87 with starter kit and i am "starter" to microcontroller wants to learn a lot.

 

I have a program that displays a menu which has 5 or more submenus. When i push the button, submenus in the display goes below. Program is like that;

 

unsigned volatile char mmm = 0x00;


void main(void)   
{
unsigned int ii;
char ucStr[5][6]= {"menu0","menu1","menu2","menu3","menu4"};
char far *p;
char degis[8]=0;
InitialiseDisplay();
p=&degis;
while(1)
{
  for(ii=0;ii<8;ii++)
  {
   degis[ii]=ucStr[mmm][ii];
  }
  DisplayString(LCD_LINE1,p);
}
}       

 

 

DisplayString is obviously take the char to LCD. I directly copied from tutorial. And this is my ISR;

 

 

     extern unsigned char mmm;
     unsigned char sayac;

 

#pragma INTERRUPT INT_INT0
void INT_INT0(void)
{
 
unsigned long delay=0x3F;
while(delay--);
mmm++%5;
asm("NOP");
sayac++;
return;
}

 

 

Sayac means nothing. mmm goes to +1 and LCD displays another submenus. This is very simple for c.However this is not working.

 

The problem is, when i push the switch, interrupt service rutine works 2 or 3 times. So firstly, menu0 appear, than menu2 than again menu0.

 

What do you think about that? What can be the problem?

 

And also, i am trying to do some kind of works with the starter kit(using timers adc's or anything else) Do you have an idea that there are some ways to improve myself correctly and fast?

 

Thanks a lot. Good Days...

 

FrankL Apprentice 1,100 posts since
Mar 16, 2009
Currently Being Moderated
Dec 14, 2009 1:48 AM in response to: KaZaNoVa64
Re: int interrupt problem

Do you know that switches are bouncing when you push them? Connect an oscilloscope to the switch and look at the signal. I think INT0 interrupt is executed much more often than only 2 or 3 times.

Usually switches are sampled with an interval of several milliseconds, and only if the value dfoesn't change in 2 or 3 consecutive samples the key stroke is accepted.

In your case, you could for example insert a big wait loop (may be 10ms) in the interrupt routine. Not very efficient, but should work.

Or you could

- start a timer (something like 1ms) in the INT0 interrupt routine.

- disable INT0 interrupt

- in the timer interrupt routine check INT0 port by software. Increment a counter when the port level still says the key is still pushed. If the port level changes enable INT0 again and reset the counter.

- if the counter reaches 4 or 5 accept the key and execute the associated function. Enable INT0 again.

 

But the best would be not to use INT0 and sample the key input port only by software every 5 or 10ms.

Icecap Newbie 222 posts since
Mar 16, 2009
Currently Being Moderated
Dec 14, 2009 11:24 AM in response to: KaZaNoVa64
Re: int interrupt problem

You NEVER exits a ISR with return!!!!!!

 

And reading switches through a timer-interrupt at a rate between 10Hz and 200Hz is quite sufficient to ensure debouncing, no further delay is needed!

And delays in a ISR is a big no-no! An ISR should be hit-and-run, no stopping or waiting.

 

To read keys with debounce it's merely to read the input port ONCE each interrupt. To remove all possibilities of interferrence it's simply enough to save the previous read valuen to "next time" and then AND the current reading with the previous reading, that way only switches that has been active both readings is registered. This double-reading does however make it nessesary to rais lowest read-speed to 20Hz.

DJ Delorie Novice 301 posts since
Mar 12, 2009
Currently Being Moderated
Dec 15, 2009 12:26 AM in response to: Icecap
Re: int interrupt problem

Just FYI, gcc has no problem if you "return" from an ISR - it knows what kind of return you need.  Don't know about other compilers, but I'd assume they'd work that way too.

FrankL Apprentice 1,100 posts since
Mar 16, 2009
Currently Being Moderated
Dec 15, 2009 2:41 AM in response to: DJ Delorie
Re: int interrupt problem

Also other compiler like NC30 or EWM16C don't have a problem with an explicit return from an ISR.

More Like This

  • Retrieving data ...

Bookmarked By (0)