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;
DisplayString is obviously take the char to LCD. I directly copied from tutorial. And this is my ISR; #pragma INTERRUPT INT_INT0 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...
void main(void)
{
unsigned int ii;
char ucStr[5][6]= {"menu0","menu1","menu2","menu3","menu4"};
char far *p;
char degis[8]=0;
InitialiseDisplay();
p=°is;
while(1)
{
for(ii=0;ii<8;ii++)
{
degis[ii]=ucStr[mmm][ii];
}
DisplayString(LCD_LINE1,p);
}
}
unsigned char sayac;
void INT_INT0(void)
{
unsigned long delay=0x3F;
while(delay--);
mmm++%5;
asm("NOP");
sayac++;
return;
}
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.
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.
©2003–2010 Renesas Electronics Corporation. All rights reserved. Using Our Website | Privacy
Contact us