Inhaltsverzeichnis
TASTO?
A key (Italian tasto, Latin clavis) or a button is a control element that is operated by pressing and automatically returns to its original position when released.
Forth has KEY?
and KEY
to query the computer's keyboard. I therefore call the query of a different individual key something else. Figure below shows such a key.
At CLAVIS? you can find out more about it.
Implementation for MSP430G2553
Here, the button is connected to pin 7 of port 1 of the MSP430G2553 microprocessor MCU. See circuit diagram in code.
I/O
This circuit provides a digital input, with the pin itself being the voltage source.
Pseudo code
Function: PININ ( mask -- ) configure pin as input Function: TASTO? ( mask -- f ) \ flag is true if the key is not pressed (high level at pin) check digital voltage level
Forth implementation (noForth)
The port pins are configured using registers. The configuration register table is specified in the code. The interrupt register indicates whether a falling edge was present.
In the direction register, P1.7 is set to digital IN. The integrated pull-up resistor is activated to support the high level. No interrupt should occur, so it is disabled. The pin is used in the I/O function.
Try more or fewer than five queries to eliminate the bouncing. (More on the topic of bouncing.)
\ test button \ TI MSP430G2553 Launchpad with noForth mv 2553 240101 (* History 20250609 P1.7 digital input addr acronym registername 020 P1IN Input 021 P1OUT Output 022 P1DIR Direction 023 P1IFG Interupt Flag 024 P1IES Interrupt Edge Select 025 P1IE Interrupt Enable 026 P1SEL Port Select 041 P1SEL2 Port Select2 027 P1REN Resistor Enable Pin1.7--<---R---Button---GND R=47K *) hex 80 constant pin7 \ mask for pin7 of port : PININ ( mask -- ) \ make pin to input ( 020 ) \ P1IN Input, read only dup 021 *bis \ P1OUT Output, pullup resistor set dup 022 *bic \ P1DIR Direction to IN dup 023 *bic \ P1IFG Interupt Flag, cleared dup 024 *bic \ P1IES Interrupt Edge Select, is irrelevant here. dup 025 *bic \ P1IE Interrupt Enable, off dup 026 *bic \ P1SEL Port Select, I/O dup 041 *bic \ P1SEL2 Port Select2, I/O 027 *bis \ P1REN Resistor Enable, set ; : TASTO? ( mask -- f ) \ Query button. debounced >r r@ 020 bit* 3 ms r@ 020 bit* 3 ms and r@ 020 bit* 3 ms and r@ 020 bit* 3 ms and r@ 020 bit* 3 ms and r> = ; \ test mk : tt ( -- ) pin7 pinin begin ( 100 ms ) \ add more ms if you like pin7 tasto? . key? until ; shield nn\ freeze ( finis)
Background information
You can find them there: CLAVIS?
Contributions
Alternative Implementations
You have another approach?
Please add it at the end of this document.
Sign in to post a comment.