Docstoc

Using the PICAXE - DOC

Document Sample
Using the PICAXE - DOC Powered By Docstoc
					Using PICAXE with Keypads
4 key input
For input to the PICAXE you can use a 4 key
matrix. It uses input pins 0,1,6,7 in the order
shown in the PCB layout. Input pin 2 is not used
but is connected to the central input of this block
so that you can use another switch or sensor
block. Pin 2 can take an analog input so you
could use a light sensor block or other analog
sensor block. There is a 1M pull-up resistor
indicated on input 2, but this does not need to be
used in many cases. Here's a program that lights
an LED corresponding to the key pressed:

symbol          key1=input1    'rename input
symbol          key2=input0
symbol          key3=input7
symbol          key4=input6
main:           if key1=0 then flash1
                if key2=0 then flash2
                if key3=0 then flash3
                if key4=0 then flash4
                goto main
flash1:         high 1
                goto endloop
flash2:         high 2
                goto endloop
flash3:         high 3
                goto endloop
flash4:         high 4
endloop:        let pins=%00000000   'reset all LEDs
                goto main

This block is exactly the same as the one above
but simply with the keys in a different
arrangement. It is useful for controlling a buggy
with left, right, forward and back commands. Both
blocks use an active low input for the PICAXE. In
other words the input pins are held high except
when you press a key. Here is a shorter program
which allows you to press more than one key at
once:

main:      let b0=pins     'get inputs
           b1=b0&%11000011 'mask unwanted bits
           let pins=b1     'set outputs
           goto main




005c3f11-c808-449a-b78e-f6ad7edf9636.docwww.school-electronics.co.uk   page 1
12 Key Matrix (commercial) - Keyboard scanner
In order to read 12 keys we use outputs 0-2 of the
PICAXE to push each column high in order. At the
same time the 4 inputs 0,1,2,7 are used to read
each row to see whether it has gone high. If it has
then the key pressed can be deduced by the
program. Pin 6 is available as an input pin for the
preceding block. The pull-down resistors hold the
inputs normally low.

‘Commercial Keyboard scanner from CJL
' Keypad Reader for PICAXE18

symbol col1=output1                 'output 1 - column 1
symbol col2=output2                 'output 2 - column 2
symbol col3=output0                 'output 0 - column 3

symbol    row1=input1               'input     1   -   row   1
symbol    row2=input7               'input     7   -   row   2
symbol    row3=input2               'input     2   -   row   3
symbol    row4=input0               'input     0   -   row   4

symbol key_value = b1               'value of key pressed
symbol display=output4              'lcd display on pin 4

' scan each column in turn by setting only 1 column high
' then check each row for switch pressed by calling key_test

init: pause 500              'for LCD to initialise
      serout display,N2400,("Ready")

scan: let key_value = 1
      high col1
      gosub key_test
      low col1
      let key_value = 2
      high col2
      gosub key_test
      low col2
      let key_value = 3
      high col3
      gosub key_test
      low col3
      goto scan                            'loop back

key_test:                                  ' return straight away if no key pressed
      if row1        =   1   then   add1
      if row2        =   1   then   add2
      if row3        =   1   then   add3
      if row4        =   1   then   add4
      return

'key value will already be 1, 2, or 3 so add 0 or 3 to this value
add4: let key_value = key_value + 3
add3: let key_value = key_value + 3
add2: let key_value = key_value + 3
add1: ‘the following lines are only used for testing
      serout display,N2400,("Key is ",#b1," ") 'show number on LCD
      debug key_value        ‘show number on computer



005c3f11-c808-449a-b78e-f6ad7edf9636.docwww.school-electronics.co.uk          page 2
         high 7                            ‘flash an       LED on output 7
         pause 100                         ‘you will       need to do something
         low 7                             ‘with the       key press
         return                            ‘you must       have this line!



There is a different keypad scanner used in the PICAXE lock kit from
www.picaxe.co.uk, which takes rows high and checks columns. See
chi008.pdf at the documentation page of www.picaxe.co.uk.

' Keypad Lock for PICAXE-18 (slightly altered)

'output     7 -   FET to drive solenoid bolt
'output     6 -   piezo sounder
'output     4,5   - bicolour LED
'output     3 -   row 4
'output     2 -   row 3
'output     1 -   row 2
'output     0 -   row 1

'input 0 - column 1
'input 1 - column 2
'input 2 - column 3

symbol key_pos = b0                 ' number of keys pressed
symbol key_value = b1               ' value of key pressed

init: let key_pos = 0               ' reset position to zero

' scan each row in turn by setting only 1 row (and LED) high
' if a switch is hit call key_test subroutine below

scan: let key_value = 0
      let pins = %00010001
      gosub key_test

         let key_value = 3
         let pins = %00010010
         gosub key_test

         let key_value = 6
         let pins = %00010100
         gosub key_test

         let key_value = 9
         let pins = %00011000
         gosub key_test

         goto scan


' key_test sub procedure return straight away if no key pressed
key_test:
      if pin0 = 1 then add1
      if pin1 = 1 then add2
      if pin2 = 1 then add3
      return

' key value will already be 0, 3, 6, or 9 so add 1, 2 or 3 to this
add3: let key_value = key_value + 1



005c3f11-c808-449a-b78e-f6ad7edf9636.docwww.school-electronics.co.uk              page 3
add2: let key_value = key_value + 1
add1: let key_value = key_value + 1

          sound 6,(60,50)           ' Make a beep

' increase position counter by 1 and test for 1st,2nd,3rd or 4th push
      let key_pos = key_pos + 1
      if key_pos = 1 then test1
      if key_pos = 2 then test2
      if key_pos = 3 then test3
      if key_pos = 4 then test4

'   ***   Now test the value for each position individually ***
'   ***   If value is wrong, restart, if correct loop until ***
'   ***   fourth go. If fourth is correct open lock! ***
'   ***   Key code is set to 9-3-5-1 ***

test4:           if key_value         = 1 then open
      goto      reset
test3:           if key_value         = 5 then continue
      goto      reset
test2:           if key_value         = 3 then continue
      goto      reset
test1:           if key_value         = 9 then continue
      goto      reset

' *** Got here so open lock and set LED green for 5 sec ***
open: let pins = %10100000
      wait 5

' *** Not correct value so reset position counter then return ***
reset:
      let key_pos = 0

' *** Okay so continue by returning back to main loop ***
continue:
      return

Keyboard scanner for homemade keypad
In order to read 12 keys we use outputs 0-2
of the PICAXE to push each column low in
order. At the same time the 4 inputs 0,1,2,6
are used to read each row to see whether it
has gone low. If it has then the key pressed
can be deduced by the program. Pin 7 is
available as an input pin for the preceding
block. The pull-up resistors hold the inputs
normally high.
' Keypad Reader for PICAXE18

symbol col1=output0                 'output 0 - column 1
symbol col2=output1                 'output 1 - column 2
symbol col3=output2                 'output 2 - column 3

symbol     row1=input6              'input     6   -   row   1
symbol     row2=input0              'input     0   -   row   2
symbol     row3=input1              'input     1   -   row   3
symbol     row4=input2              'input     2   -   row   4


005c3f11-c808-449a-b78e-f6ad7edf9636.docwww.school-electronics.co.uk   page 4
symbol key_value = b1               ' value of key pressed
symbol display = 4                  ‘ used for the LCD display

' scan each column in turn by setting only 1 column low
' if a switch is pressed call key_test below
init: pause 500   'for LCD

scan: let key_value = 1
      low col1
      gosub key_test
      high col1
      let key_value = 2
      low col2
      gosub key_test
      high col2
      let key_value = 3
      low col3
      gosub key_test
      high col3
      goto scan         'loop back

' return straight          away if no key pressed
key_test:
      if row1 = 0          then    add1
      if row2 = 0          then    add2
      if row3 = 0          then    add3
      if row4 = 0          then    add4
      return

' key    value will already be 1, 2, or 3 so add 0 or 3 to this value
add4:    let key_value = key_value + 3
add3:    let key_value = key_value + 3
add2:    let key_value = key_value + 3
add1:    ‘the following lines are only used for testing
         serout display,N2400,("Key is ",#b1," ") 'show number on LCD
         debug key_value        ‘show number on computer
         high 7                 ‘flash an LED on output 7
         pause 100              ‘you will need to do something
         low 7                  ‘with the key press

         return




005c3f11-c808-449a-b78e-f6ad7edf9636.docwww.school-electronics.co.uk   page 5

				
DOCUMENT INFO