This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi Nandish,
Do a search in the forum for related posts which you can learn from. Here are some below.
What is correct initialization for Launchpad TM4C123 button interrupts?
Doubt on switches in Tiva Launchpad
- kel
Again same beginner and [guru beginner] standard answer...
You cannot use an interrupt like this to answer to a button pressure, when you press/release button some ten to hundred pulses occur due to mechanical contact. This is known as mechanical bounce of button, so your counter if not properly "debounced" NEVER reach the number you think and never can work.
A debugging inspection to counter can confirm this is single issue or non working program other issue added to this main one.
Please post your result of inspection and if this can be solved too.
Right way is to use a timer and check when button get pressed, stay in stable state for a min time then mark as pressed, after some time other action like repeat can be added.. When depressed same reverse procedure to do debounce on both transition.
Example proposed are not solving main issue, first one use printf on an interrupt handler too and this has to be deprecated forever!!! printf can use other interrupt can hang processor if served task is higher priority than char I/O service.
Interrupt handler must complete in the shortest time and long time consuming need to be demanded on main loop spare time.
Edit: Also great care has to be done on interrupt nesting and worst interleaved to prevent deadlocking of resource never get served..
Second link proposed by, address this same problem but just Petrei suggested link really address problem, problem is DEBOUNCING and was more better to point where problem is than post useless link...
A good programming course can solve all these issue...
Hi everyone,
Roberto pretty much explained it so i will just add this (self publicity much? :p): 
Note that this handles the "bouncing" in the worst way possible for a interrupt!! I probably should have added a timeout with th System Tick by now, like Roberto referred.
Nandish H S,
Can you try one exercise?
Forget interrupts for this.
Just do in the main a loop that toggles the Red LED every time you press down the SW2. Just once! If you press it once then it has to switch once. If you notice sometimes the LED doesn't behave as expected. That's because of the "bounce" effect. The LED would actually change state more than once since the button would bounce around states. 1 way to avoid this is using a "delay" so that the "if" or "while" that checks for a button state change only happens once in the time window that the button bounces. Of course that blocks your code and in a interrupt that's really bad!! Again, Roberto suggestion to use a timer for that is the best to avoid a code that blocks everything
Roberto Romano said:Again same beginner and [guru beginner] standard answer..
Roberto - that's classic - and (most often) apt. Not a whole lot of "growth" in evidence during the (endless) response to eased/beginner posts...
Is not the (often) suggested, "Do a search" the [guru beginner] equivalent of, "Does not Work?"
cb1_mobile said:Is not the (often) suggested, "Do a search" the [guru beginner] equivalent of, "Does not Work?"
Hi CB1, you are undoubltely right and you catched mee in wrong statement too.. But "errare humanum est, perseverare autem diabolicum" I think also if forum get split this is forever dangerous to everyone expecially when beginner need a real guide to learn.
I am not contrary to have a learning C forum, but not on TIVA.. maybe some general but indeed real programming guru has to teach "ZEN" from his experience and knowledge not getting some useless garbage got from collection.
Luis Afonso said:Just do in the main a loop that toggles the Red LED every time you press down the SW2. Just once! If you press it once then it has to switch once. If you notice sometimes the LED doesn't behave as expected. That's because of the "bounce" effect. The LED would
Hi Luis, this is a good point but not changing how to detect an edge so:
prepare a timer firing 100 times a second, this mode if button is not dead bouncing is terminated, simple example to see how to do thing
volatile int keynumber = 0; // this need be global external to be accessed from reading key service
// timer interrupt
void TimerServe(void)
{
static int oldkey = 0; // static preserve value when exiting, otherwise got destroyed
static int repeattmr=100; // time to repeat .. also this need be preserved to count time internally and fire repeat when expire
static int repeatctr = 10; // repeat rate .. same as repeat but act when repeat exhaust then this one get timing of repeat...
int scancode=0; // this is the processing key and don't need be preserved from one scan to the other, when procedure end ig got flushed away
if (keypressed1==condition1)
then scancode=1; // key 1 pressed
if (keypressed2==condition2)
then scancode=2; // key 2 pressed
// scancode if no key are pressed remain 0 as end of key
if (scancode!=oldkey) // if new key is pressed
{
keynumber=scancode; // write new code to external variable , external reader clean after use, if not read last pressed remain
repeattmr=100; // 1 second delay before repeat
} // end of new key pressed
....... autorepeat logic here ......
oldkey=scancode; // rememer last key pressed to reset edge detect (not returned code)
}
int readkey(void)
{
int temp; // temp var to hold value of key...
if (keynumber)
{ // if key is pressed
temp = keynumber; //copy key value (this has to be done monitor or critical section, can also work this way few key can be lost if not read)
keynumber=0; // clear it
return(temp); // return key
}
return(0);
}
EDIT Jan 4 2014: Code is for example, it can run when adapted to your board, application, green part are more code guide and cannot work as they are, reading from GPIO has to be instead, also for autorepeat function it is left as exercise. This is a uController support not a teaching programming.
Also code to initialize timer and assignment of service to timer interrupt is missing and can be got from timer example.
Hi Luis,
thanks foe the reply i have tried and able to achieve the proper response so next step is to achieve interrupt so i have posted my code .
Regards
Nandish