Advertisement  

Wednesday, 24 July 2019
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
 
     
 

 
   
     
 
AN #21 - ASCII input with 3x4 keyboard matrix Print
ASCII input with 3x4 keyboard matrix

This application note is written by John van der Putte.

The program uses a 3x4 keyboard matrix to input ASCII.
The output is placed on the LCD-display and will be saved in the variable TEXT.
If one key is keyed several times the characters of that key rotates at the cursor.
When no key is pressed the cursor shifts to the next display-position after a while.
This time can easely be changed.

Download source code in .BAS file


' *************************************************
' * MATRIXINPUT V1.0 07-12-99
' *
' * Author: John van der Putte
' * E-mail: joconiki@hotmail.com
' * icq: 47666030
' *
' * Program:
' * This program uses a 3x4 keyboard matrix to input
' * ASCII. The output is placed on the LCD-display and
' * will be saved in the variable TEXT.
' * If one key is pushed several times the characters
' * of that key rotates at the cursor. When no key is
' * pressed the cursor shifts to the next display-
' * position after a while. This time can easely be changed.
' *
' * The keys have the following layout:
' *
' * 1 2 3
' * .,?!-& ABC  DEF
' *
' * 4 5 6
' * GHI JKL MNO
' *
' * 7 8 9
' * PQRS TUV WXYZ
' *
' * * 0 #
' * <BACKSPACE> <SPACE> <ENTER>
' *
' *
' *
' * The procedure which looks to the keyboard matrix returns the
' * following values:
' * no key = 17
' * 0...9 = 0...9
' * * = 10
' * # = 11
' *

$romstart = &H4000
$ramstart = &HC000
$lcd = &H2000
$map

Config Lcd = 20 * 2
Config Debounce = 25 ' Set Key Pad Debounce to 40 Ms

Col1
Alias P4.7
Col2
Alias P4.6
Col3
Alias P4.5
Row1
Alias P4.3
Row2
Alias P4.2
Row3
Alias P4.1
Row4
Alias P4.0

Dim Keyread As Byte ' key being scanned
Dim Key As Byte ' key being pressed
Dim Keyold As Byte ' key being pressed before
Dim Number As Byte ' pointer to charactertable
Dim Index As Byte ' pointer to character for the pressed key
Dim Character As String * 1 ' character selected
Dim Count As Byte ' time for automatic cursormovement
Dim Text As String * 20 ' text inputted so far
Dim I As Byte ' used by for-next loops
Dim Begin As Byte ' pointer to character in charactertable
Dim Name As String * 20

Const Countmax = 30 ' change this value for different time of automatic cursormovement

Cls ' clear lcd-display
Cursor Off Blink ' no cursor but blinking character

' time for automatic cursormovement is controlled bij timer0.
' when timer0 is needed for other purposes you can replace it
' with a simple incrementation of a variable
' timer0 is configured as a 16-bit timer
Config Timer0 = Timer , Gate = Internal , Mode = 1
On Timer0 Timer_0_int
Enable Interrupts
Enable Timer0

Print "Give your textinput with keyboardmatrix."
Print "Input is displayed on LCD, result is displayed on screen"
Gosub Matrix_input
Print
Print "This is your input: >" ; Text ; "<"
End

Matrix_input
:

Index
= 0 ' reset index
Keyold
= 16 ' reset keyold (16 is used to prevent automatic cursormovement)
Count
= 0 ' reset count
Text
= "" ' reset text
Do
L0
:
 
Gosub Keyscan
L1
:
 
If Row1 = 0 Then Goto L1
 
If Row2 = 0 Then Goto L1
 
If Row3 = 0 Then Goto L1
 
If Row4 = 0 Then Goto L1

 
If Count = Countmax Then ' if it is time for automatic cursormovement then
 
Shiftcursor Right ' move cursor to the right on display
 Text
= Text + Character ' save character in text
 
Stop Timer0 ' stop automatic cursormovement
 Count
= 0  ' reset counter
 Index
= 0 ' reset index
 Keyold
= 16 ' pretend an other keypress to suppress automatic cursormovement
 
End If
 
If Key < 16 Then
 
If Keyold = 16 Then ' prevent automaic cursormovement
 Keyold
= Key
 
Stop Timer0
 
End If
 
If Key = 10 Then ' BACKSPACE-keywas pressed
 
Gosub Key10
 
Goto L0
 
End If
 
If Key = 11 Then Goto Key11 ' ENTER-key was pressed
 
Restore Keylength ' get startpoint in keytable
 Begin
= 1 ' point at start of keytable
 
For I = 0 To Key '
  
Read Number ' read number of characters available on key
 
If I < Key Then ' calculate startpoint in keytable
 Begin
= Begin + Number '
 
End If '
 
Next I '
 
If Key <> Keyold Then ' if other key is pressed then
 Index
= 0 ' reset index
 
Shiftcursor Right  ' move cursor to the right on display
 Text
= Text + Character ' save character in text
 
End If '
 
If Index = Number Then ' index must not exceed the numer of available characters
 Index
= 0 '
 
End If '
 Begin
= Begin + Index ' set pointer in keytable
 
Restore Keydata ' get character from keytable
 
For I = 1 To Begin '
 
Read Character '
 
Next I '
 
Lcd Character ' show character to display
 
Shiftcursor Left ' and move cursor to the left
 Count
= 0 ' reset count
 
Start Timer0 ' start timer for automatic cursormovement
 Keyold
= Key  ' save the pressed key
 
Incr Index ' increment index
 
End If '
Loop

' BACKSPACE-key was pressed
Key10
:
I
= Len(text) ' number of characters of endresult
If I > 0 Then ' only remove character it there are characters
 
If Tcon.4 = 1 Then ' if timer0 is still runnuning then:
 
Stop Timer0 ' stop the timer
 Count
= 0 ' and reset counting
 
Lcd " " ' clear lcd at position of cursor
 
Shiftcursor Left ' and place cursor at the right position
 
Else
 
Shiftcursor Left ' move cursor to the left
 
Lcd " " ' and remove character from lcd
 
Shiftcursor Left ' place the cursor at the right position
 
Decr I  ' remove last character from result
 Text
= Left(text , I)
 
If I = 0 Then ' if no more characters in result then clear result
 Text
= ""
 
End If
 
End If
 Keyold
= 16 ' prevent automatic cursormovement
End If
Return

' ENTER-key was pressed
Key11
:
If Tcon.4 = 1 Then ' if timer0 is still running the keypress isn't added to endresult
 Text
= Text + Character ' so add it now
End If
Return

Keyscan
: ' Keypad Read Routine (Assignments can be changed for different connections)
Key
= 17
Col1
= 0 : Col2 = 1 : Col3 = 1 ' Turns Columns On (Low) 1 by 1
Keyread
= 1 : Debounce Row1 , 0 , Gotkey ' Goto if Key Pressed (Reads LOW Input)
Keyread
= 4 : Debounce Row2 , 0 , Gotkey
Keyread
= 7 : Debounce Row3 , 0 , Gotkey
Keyread
= 10 : Debounce Row4 , 0 , Gotkey ' BACKSPACE Button *
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Col1
= 1 : Col2 = 0 : Col3 = 1
Keyread
= 2 : Debounce Row1 , 0 , Gotkey
Keyread
= 5 : Debounce Row2 , 0 , Gotkey
Keyread
= 8 : Debounce Row3 , 0 , Gotkey
Keyread
= 0 : Debounce Row4 , 0 , Gotkey
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Col1
= 1 : Col2 = 1 : Col3 = 0
Keyread
= 3 : Debounce Row1 , 0 , Gotkey
Keyread
= 6 : Debounce Row2 , 0 , Gotkey
Keyread
= 9 : Debounce Row3 , 0 , Gotkey
Keyread
= 11 : Debounce Row4 , 0 , Gotkey ' ENTER Button #
Return

Gotkey
:
Key
= Keyread
Return

' everytime TIMER0 overflows it generates an interrupt and jumps to this place
Timer_0_int
:
 
Incr Count ' increment count
Return

' This table holds the number of available characters for each key (0...9)
Keylength
:
 
Data 2 , 7 , 4 , 4 , 4 , 4 , 4 , 5 , 4 , 5

' This table holds the characters available for each key (0...9)
' If you changes this table than you must change the keylength-table too.
Keydata
:
 
Data " " , "0"
 
Data "." , "," , "?" , "!" , "-" , "&" , "1"
 
Data "A" , "B" , "C" , "2"
 
Data "D" , "E" , "F" , "3"
 
Data "G" , "H" , "I" , "4"
 
Data "J" , "K" , "L" , "5"
 
Data "M" , "N" , "O" , "6"
 
Data "P" , "Q" , "R" , "S" , "7"
 
Data "T" , "U" , "V" , "8"
 
Data "W" , "X" , "Y" , "Z" , "9"