hint for a code organization

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

hint for a code organization

1,541 Views
Halom
Contributor I
Hi,
I am starting interrupt  (with MC9S12C32) and I need some help in the organiztion of one of mu program.
 
I have a program that is doing some function. Then when I press and realease a button (set up at PTAD0) the program stops and the LED's are supposed to flash every second using timer interrupt.When we press and release the button again the LEDs stop flashing and the main program continues.
 
Our teacher want us to us the following when dealing with interrupt:
- have a LED thread_begin function that start the thread( intialization of the device here  timer)
- have the ISR - doing the work
- have a thread " end" that stop the LED thread
 
I have one issue here:
One is how to implement the part when the button is pressed and released.
 
My  code organization is something like this:
...main program working...
Check if  button pressed and released
     IF yes
        call LED thread_begin
            In the ISR function
            Does 1 second passed?
                IF yes  flash the LED
                ELSE wait
Check if button is pressed and released
If yes 
  Call LED thread_stop
(return to main program)
Also wher should I place my 'check button' code?
 
Please can I have some help with the button and my program organization?
thank you
B
 
Labels (1)
0 Kudos
2 Replies

260 Views
Lundin
Senior Contributor IV
The way of implementing that makes most sense to me is to let the thread handle the button scanning, which is actually the only real work as such.

You will need to read the button status, then read it again after some miliseconds to check if it is the same. This is known as "debouncing" and the purpose is to take care of signal bounces caused by the button mechanics as well as possible EMI glitches etc. When you have made clear that the button is stable, you have to save its position (pressed/not pressed) into a variable.

This variable can be polled from another thread or the main program. If it changed status from pressed to not pressed, the button was released and another timer which controlls the led can be started. And similar, if the button was pressed again, stop the timer.

It is very important to make all variables shared between threads/ISRs volatile, and possibly also protect them with semaphores/muticies etc. If this isn't done, you might get several kinds of very hard-to-find runtime bugs.
0 Kudos

260 Views
Halom
Contributor I
Thank you very much Ludin!
I will try that and see If I got it right.
B
0 Kudos