Once more unto the breach dear friends, and this time with a short native language code routine to read the state of the Blue user button
on the STM32F4DISCOVERY board. The routine waits for the button to be pressed and duly toggles the Blue led either on or off.
To avoid any mechanical contact ringing, it is then debounced by a software 50mS Delay. The routine then waits further for the button to be released
and reacts with a second debounce delay to filter out any contact after-ringing. All in all, a classic software debounce routine :-)
We start, as usual, by firstly enabling the clocks for both GPIOA and GPIOD in the clock enable register RCC_AHB1ENR.
We set GPIOD_Pin15 to be a 2Mhz General Purpose Push-Pull Output and GPIOA_Pin0 to be a default floating Input, which is held low by resistor R39 (220k).
In the Main routine, the value of the input register GPIOA_IDR is constantly polled and is compared with the UseBut constant.
When the Blue button is momentarily depressed, the input value will go from low to high and the Blue led will be toggled between On and Off
respectively in accordance with the previous state of the Blue led. This is acheived by exclusive-oring the former state of the led with the BluLed constant.
If it was previously high it now becomes low and vice-versa. In this way the exclusive-or instruction functions as a simple toggle command.
The state of the pins on the output register GPIOD_ODR can be read from as well as being normally written to, and the code can be represented as a
stereotypical risc architecture load-modify-store exercise.
Thus far it's been almost the same as the original M3 powered GetKey routine, but at lable ma we now introduce a second key test followed by another debounce.
The state of the Blue button is polled once more by constantly scanning its logic level on the GPIOA_IDR input register. When the routine detects a transition
from high back to low, then we know, that the Blue button has been released. Just like a key-depress, we have to debounce the mechanical contact noise-ringing
of the key-release using the 50mS delay subroutine DebOz. This subroutine is implemented by decrementing the value in register R0 in the routine mb and
when it reaches zero it returns to the calling code in the Main routine. The program loops back to Main and repeats itself ad nauseum.
" We may say that Mr. Babbage's Analytical Engine weaves algebraic patterns just as the Jacquard-loom weaves flowers and leaves." Ada Augusta, countess of Lovelace.