\ Read port \ TI MSP430G2553 Launchpad with noForth mv 2553 240101 (* To do: n 0 do ... loop in t? --> n variabel machen. History 20250609 P1.7 digital input, some bounce tests 20250606 P1.0,6 on/off (red and green LED of Launchpad) 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 *) asm\ hex \ tools code cls ( -- ) s0 # sp mov next end-code \ clear stack \ --- output --- \ Switch P1.0,6 to H or L code p1H #1 021 & .b bis next end-code code p1L #1 021 & .b bic next end-code code p6H 40 # 021 & .b bis next end-code code p6L 40 # 021 & .b bic next end-code : outP16 ( -- ) \ Switch pin P1.0,6 as output 41 027 *bic \ p1ren off 41 025 *bic \ p1ie disable interrupt 41 026 *bic \ P1SEL I/0 41 041 *bic \ P1SEL2 I/0 41 022 *bis \ set out for P1.0, P1.6 ; : pulse ( n -- ) 0 do p1H p1L loop ; \ --- input --- (* Switch (T) an P1.7 (P) VCC---R---+---o_o---GND P T ( T for German "Taste" ) *) hex 80 constant mm \ mask for pin7 of Port1 : inP1 ( -- ) \ masked pin of P1 is input mm 027 *bic \ ren off mm 025 *bic \ ie disable interrupt mm 026 *bic \ SEL I/0 mm 041 *bic \ SEL2 I/0 mm 022 *bic \ set in for P1 masked pin ; : t? ( -- f ) \ query switch T position: 0=L, $400=H 0 \ 0 < f < $400 == bouncing 8 0 DO mm 020 bit* + LOOP ; : tt ( --- ) \ bouncetest - continuously query switch. cr inP1 begin t? dup 400 = if drop else dup 0 = if drop else . \ did bounce ! then then key? until ; (* The observation showed that EVERY key press bounces. There was always a value <400 when the key was pressed, and usually also when released. The query of the port pin in high-level code is fast enough to detect bounce of the connected switch. *) \ **** : t?? ( --- f ) \ debounced querry. inP1 begin t? dup 400 = if exit else dup 0 = if exit else drop \ did bounce ! then then again ; \ print next voltage level only once usin a semaphore value Hsem true to Hsem value Lsem true to Lsem : .1L ( -- ) \ print L once Lsem if [char] L emit then false to Lsem true to Hsem ; : .1H ( -- ) \ print L once Hsem if [char] H emit then false to Hsem true to Lsem ; : dtt ( --- ) \ de-bouncetest - continuously query switch. cr inP1 begin t?? dup 400 = if .1H drop else dup 0 = if .1L drop else ." failed" \ never reached then then key? \ **footnote - just press <ret> if key [char] q = if exit then [char] . emit then again ; (* ** The e4thcom terminal does not send individual keys, but only lines. Therefore, the key is only transmitted after <ret>. Just press <ret> to generate the dots. So, press the botton, press <ret>, release the botton, <ret>, pressing the botton again... etc., produced the following: @)dtt LHL.H.LHL.H.LHL.H.LHL.H.LHL.H.LHL.H.LHL.H.L.H.LHL.H.LHL.H.LHL.H.L.H.LHL.H OK.0 @) You can see that the levels always change from L to H and back, but not without bounce. Especially from H to L, an additional change is always detected. From L to H, it seems to work without bounce. A keystroke counter H->L would therefore count twice, only sometimes correctly. *) \ **** counter for switch presses. variable cnt : .1LC ( -- ) \ print L once, count +1 Lsem if [char] L emit 1 cnt +! then false to Lsem true to Hsem ; : dttc ( --- ) \ counter test - continuously query switch. cr inP1 0 cnt ! begin t?? dup 400 = if .1H drop else dup 0 = if .1Lc drop else ." failed" \ never reached then then key? \ **footnote - just press <ret> if key [char] q = if exit then [char] . emit cnt @ . then \ zähler mitdrucken again ; (* Here you can see that bouncing causes the counter to count incorrectly: @)decimal OK.0 :)dttc LHLHLHLHLH.5 L.6 H.6 L.7 H.7 L.8 H.8 LHL.10 H.10 L.11 H.11 LHL.13 H OK.0 After pressing a few buttons, it claims it was 5. Then I pressed each button slowly, one at a time, and it counted correctly to 8. But the next time I pressed the button, it counted twice again, from 8 to 10 even though it was only pressed ONCE. At 11 to 13, another counting error. Another example: @)dttc LHL.2 H.2 L.3 H.3 LHL.5 H.5 LHL.7 H.7 LHL.9 H.9 L.A H.A LHL.C H.C LHL.E H OK.0 @) *) shield nn\ freeze ( finis)