Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

4x4 keypad program and a simple game for it

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here
View previous topic :: View next topic  
Author Message
erg322

Bascom Member



Joined: 12 Dec 2013
Posts: 4
Location: South-East

australia.gif
PostPosted: Tue Dec 17, 2013 7:51 am    Post subject: 4x4 keypad program and a simple game for it Reply with quote

Couldn't resist........

Code:

'
'               _   _____
'              / | / /   |  ____ ___  ___
'             /  |/ / /| | / __ `__ \/ _ \
'            / /|  / ___ |/ / / / / /  __/
'           /_/ |_/_/  |_/_/ /_/ /_/\___/                                                         '
'

'-------------------------------------------------------------------
'
' Elementery Keypad Program with the game HiLo....use as a routine
' when needed. There is a better 3x3 alphanumeric keypad at AN #21
' in the apps page but this one is simple and hackable.
'
' Program for a 4x4 keypad Plus "*" And "#" For Use In Other Programs.
'
' Note 1. Only test for press and release to stop multiple chars.
' Note 2. Subroutines "Getnum", "FindKey", the row subroutines and
'         "Releasekey" are needed for the keypad.
'
'
'                      28th April 2013
'
' Acknowledgement to - Amol Shah for concept.
' ASCII Text - http://www.network-science.de/ascii/ (use slant)
'
'
'------------------------------------------------------------------
'           ____       ___           __
'          / __ \ __  / __(_)___ (_)/ /_(_)___  ____  _____
'         / / / / _ \/ /_/ / __ \/ / __/ / __ \/ __ \/ ___/
'        / /_/ /  __/ __/ / / / / / /_/ / /_/ / / / (__  )
'       /_____/\___/_/ /_/_/ /_/_/\__/_/\____/_/ /_/____/
'
'------------------------------------------------------------------
$regfile = 8052.dat

$crystal = 11059200                       ' Baud rate accurate Xtal
$baud = 2400
$large

Config Lcdpin = Pin , Db4 = P2.4 , Db5 = P2.5 , Db6 = P2.6 , Db7 = P2.7 , E = P1.0 , Rs = P1.2
Rem with the config lcdpin statement you can override the compiler settings


Config Lcd = 20 * 4
'
'
'
Column1 Alias P0.0                        'The columns on my Keyboard
Column2 Alias P0.1                        'have 10k ohm pullup resistors.
Column3 Alias P0.2
Column4 Alias P0.3
'
'
Row1 Alias P0.4
Row2 Alias P0.5
Row3 Alias P0.6
Row4 Alias P0.7
'
'
' Keyboard matrix goes like this..........
'
' Columns 1 2 3 4  Rows
'                   \/
'         1 2 3 A    1
'         4 5 6 B    2
'         7 8 9 C    3                                 For Reference
'         * 0 # D    4                     (ASCII 0=&H30 9=&H39 A=&H41 D=&H44 *=&H2A  #=&H23)
'
'I have decoded it for the moment 0-9 then A=10 B=11 C=12 D=13 *=14 #=15
'as an byte value.
'
'
'
'
Dim A As Byte                             'Flag indicating a key is pressed
Dim C As Byte                             'Character of key pressed
Dim D As Byte                             '0-99 from 2 "C" variables
Dim K As Byte                             'Random number
Dim X As Byte                             'Number used to shuffle Rnd() function

'
'------------------------------------------------------------------------
'               ____
'              / __ \_________  ____  _________  ____ ___
'             / /_/ / ___/ __ \/ __ `/ ___/ __ `/ __ `__ \
'            / ____/ /  / /_/ / /_/ / /  / /_/ / / / / / /
'           /_/   /_/   \____/\__  /_/   \__,_/_/ /_/ /_/
'                             ____/
'------------------------------------------------------------------------

Prelude:

  K = Rnd(100)
  Cls
  Cursor Off
  Wait 1
  Gosub Rand_seed
  Locate 1 , 1
  Lcd "The Game is HiLo"
  Locate 2 , 1
  Lcd "Enter No. 0-100"


Main:




  Gosub Get_num
  D = C * 10
  Locate 1 , 1
  Lcd "Number guessed " ; C ; " "
  Locate 2 , 1
  Lcd "                  "
  Gosub Get_num
  D = D + C
  Locate 1 , 1
  Lcd "Number guessed " ; D

  Locate 2 , 1
  If D < K Then
     Lcd "Too Low...      "
     Goto Main
  End If
  If D > K Then
     Lcd "Too High..      "
     Goto Main
  End If

  If D = K Then
     Lcd "Your Right !!"
     Wait 3
     K = Rnd(100)
     Locate 1 , 1
     Lcd "Ready Again      "
     Locate 2 , 1
     Lcd "Type 2 digit guess"
     Goto Main
  End If


End

'-----------------------------------------------------------------------
'   _____       __                     __  _
'  / ___/__  __/ /_  _________  __  __/ /_(_)___  ___  _____
'  \__ \/ / / / __ \/ ___/ __ \/ / / / __/ / __ \/ _ \/ ___/
' ___/ / /_/ / /_/ / /  / /_/ / /_/ / /_/ / / / /  __(__  )
'/____/\__,_/_.___/_/   \____/\__,_/\__/_/_/ /_/\___/____/
'
'-----------------------------------------------------------------------
Rand_seed:
'Since the Rnd function is a mathematic function then we have to
'seed it so the function doesn't start from the first result everytime.

  Locate 1 , 1
  Lcd "Need 2 digit seed"


  Gosub Get_num
  D = C * 10
  Locate 1 , 1
  Lcd "First Digit " ; C ; "    "
  Gosub Get_num
  D = D + C
  Locate 1 , 1
  Lcd "Seed        " ; D
  Waitms 250                              'Reasure user it was entered

  For X = 1 To D                          'Like shuffling a deck of cards
     K = Rnd(100)
  Next X

Return

'----------------------------------- Keypad Stuff ---------------------------------

Get_num:

  A = 0                                   'Initialise "Key Pressed" flag
  Gosub Find_key
  If A = 0 Then Goto Get_num              'No key pressed so loop and try again
  Gosub Key_released                      'Now we wait till user lets the key go

Return




Find_key:

'This is an simple 4x4 keyboard decoder with A to D and the * and # commented out

'----------- 1st Row ----------
  Row1 = 0                                'These feed the keyboard with a logic zero
  Row2 = 1                                'then a set of tests to see which
  Row3 = 1                                'column follows the row, this computes to
  Row4 = 1                                'the intersection which is the key linking
                                          'row to column.
  Debounce Column1 , 0 , 1_but , Sub
  Debounce Column2 , 0 , 2_but , Sub
  Debounce Column3 , 0 , 3_but , Sub
'  Debounce Column4 , 0 , A_but , Sub     'Only Need 0 to 9

'----------- 2nd Row ----------
  Row1 = 1
  Row2 = 0
  Row3 = 1
  Row4 = 1

  Debounce Column1 , 0 , 4_but , Sub
  Debounce Column2 , 0 , 5_but , Sub
  Debounce Column3 , 0 , 6_but , Sub
'  Debounce Column4 , 0 , B_but , Sub        '

'----------- 3rd Row ----------
  Row1 = 1
  Row2 = 1
  Row3 = 0
  Row4 = 1

  Debounce Column1 , 0 , 7_but , Sub
  Debounce Column2 , 0 , 8_but , Sub
  Debounce Column3 , 0 , 9_but , Sub
'  Debounce Column4 , 0 , C_but , Sub

'----------- 4th Row ----------
  Row1 = 1
  Row2 = 1
  Row3 = 1
  Row4 = 0

'  Debounce Column1 , 0 , Star_but , Sub
  Debounce Column2 , 0 , 0_but , Sub
'  Debounce Column3 , 0 , Hash_but , Sub
'  Debounce Column4 , 0 , D_but , Sub

'------------------------------

Return

'These subroutines provide the vars C and A
'with values from the rows debounce commands.

'---------- 1st Row -----------
1_but:
   C = 1
   A = 1
Return

2_but:
   C = 2
   A = 1
Return


3_but:
   C = 3
   A = 1
Return


A_but:
   C = 10
   A = 1
Return

'-------- 2nd Row -----------

4_but:
   C = 4
   A = 1
Return

5_but:
   C = 5
   A = 1
Return

6_but:
   C = 6
   A = 1
Return

B_but:
   C = 11
   A = 1
Return

'-------- 3rd Row -----------

7_but:
   C = 7
   A = 1
Return

8_but:
   C = 8
   A = 1
Return

9_but:
   C = 9
   A = 1
Return

C_but:
   C = 12
   A = 1
Return

'-------- 4th Row -----------

Star_but:
  C = 14
  A = 1
Return

0_but:
  C = 0
  A = 1
Return

Hash_but:
  C = 15
  A = 1
Return

D_but:
  C = 13
  A = 1
Return

'----------------------------
Key_released:

  Do
    A = 0                                 'Start with key released
    Gosub Find_key                        'Test key
                                          'If A=1 the button is still down so loop
  Loop Until A = 0                        'If A=0 then key has been released then
                                          'return.


Return

_________________
Smile
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Dec 17, 2013 10:07 am    Post subject: Reply with quote

Thanks for sharing. Well done. Seems you got the hang of it Very Happy
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum