User Tools

Site Tools


en:pfw:clavis

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:pfw:clavis [2025-06-22 00:23] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1en:pfw:clavis [2025-06-22 00:23] (current) – ↷ Seite von pfw:clavis nach en:pfw:clavis verschoben mka
Line 1: Line 1:
 +====== CLAVIS? ======
  
 +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.
 +
 +{{:pfw:weissertaster.png?100|The small white button is on the breadboard at the bottom right of the picture.}}
 +
 +
 +===== 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:  CONF ( mask -- ) 
 +              configure pin as input
 +   Function:  CLAVIS? ( mask -- f )  \ flag is true if the key was pressed
 +              check for falling edge at pin 
 +
 +
 +==== 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.
 +The integrated Schmitt-Trigger Input is set to the falling edge.
 +However, no interrupt should occur, so it is disabled.
 +The pin is used in the I/O function.
 +
 +<code>
 +\ Query single button.
 +\ TI MSP430G2553 Launchpad with noForth mv 2553 240101 
 +
 +(* 
 +History
 +20250614 P1.7 digital input; test falling edge.
 +
 +Port 1
 +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 
 +
 +circuit diagram
 +P1.7---R---C\_---GND
 +
 +R=47K
 +
 +*)
 +
 +
 +\  asm\  ( include if assembler is your latest shield )  
 +    
 +hex
 +80 constant pin7 \ mask for pin7 of port
 +    
 +: CONF ( 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, falling  \_ 
 +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
 +  ;
 +  
 +: CLAVIS? ( mask -- f )  \ Query button.
 +  dup 023 bit*      \ test for edge
 +    = ( true )      \ button has been pressed 
 +  ( pin7 023 *bic ) \ clear edge detection in your app.
 +  ;
 +
 +
 +(* Simple Test
 +Wait for a key on the keyboard to be pressed 
 +so you can press the button first.
 +
 +\ *)   \ uncomment for testing
 +: tt ( -- f )   
 +  pin7 conf   
 +  key drop  
 +  pin7 clavis? .s ;
 +
 +
 +shield nn\
 +freeze
 +
 +*)
 +
 +
 +( finis)
 +</code>
 +
 +==== Background information ====
 +
 +  BIT** ( mask addr -- x ) \ AND mask with cell in addr
 +In noForth the bit manipulaton words with one star operate on the lower half of a cell.
 + 
 +See: https://home.hccnet.nl/anij/nof/noforth%20documentation.pdf
 +
 +=== What is a Port? ===
 +
 +In short: The "port" is formed by 9 registers. The registers are 8 bits wide. Each bit operates on "its" pin. In its basic function, the pin can be used as a digital I/O. The direction register determines whether it is an input or output pin. The interrupt flag register is used here. It can detect a voltage edge and store it as an event bit. The functions of the other registers are described in more detail in the User’s Guide.
 +
 +See: Texas Instruments MSP430F2xx, MSP430G2xx Family User’s Guide - 8 Digital I/O, p.339ff
 +
 +=== How long is a key press? ===
 +
 +A manual short key press took around 120ms if I tapped the key only once.
 +
 +{{:pfw:dauer_kurzer_tastendruck_2025-06-14_18-53-38.png?400|Keystroke registered with the logic analyzer}}
 +
 +As you can see in the image, the key //bounces// briefly at the beginning until a stable LOW state is reached. 
 +
 +{{:pfw:prellen-kurzer_tastendruck_zoom_2025-06-14_18-55-44.png?400|Contact bounces after pressing the button}}
 +
 +In the second image, the bounce phase has been enlarged. After about 1ms, the bouncing was over.
 +
 +The port pin reacts to the first falling edge. Subsequent edges, caused by bouncing, do not trigger anything. The next falling edge would only be registered once the interrupt flag has been reset by the program.
 +
 +More on the [[pfw:topicofbouncing|topic of bouncing]].
 +
 +
 +
 +
 +
 +===== Contributions =====
 +
 +
 +===== Alternative Implementations =====
 +
 +You have another approach?
 +
 +Please add it at the end of this document.
 +
 +Sign in to post a comment.