hint for a code organization

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

hint for a code organization

1,547件の閲覧回数
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
 
ラベル(1)
0 件の賞賛
2 返答(返信)

266件の閲覧回数
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 件の賞賛

266件の閲覧回数
Halom
Contributor I
Thank you very much Ludin!
I will try that and see If I got it right.
B
0 件の賞賛