reverse engineering the keypad module

Status
Not open for further replies.
I agree with you, thats why I am redesigning a pcb to conform to the keypad12.bas.
My present set up is different on how the colums and rows are connected then add in that a keypab bas is waiting for an input.
I want the keypad to tell me what key to press.. Simple enough.
And the train has left the station a long time ago.
I do have a bit of code that is not working as it is supposed to
Code:
Repeat   
          
until
 led_row2=0 and sw_col1=0  //led and sw are both =0
issue is regardless the state of the led, the code jumps out to next sequence
 
having trouble finding correct statement.
I want to build an array for each switch and led but building the DIM statements has me lost. Then I can address each element of the switch array and include the led as well,
Code:
Dim index As Byte
//blue leds

Dim led(index) =0 To 12 As Byte
{
Dim led(0) As PORTC.0  won't compile
Dim led1 As PORTC.1
Dim led2 As PORTC.2
Dim led3 As PORTC.3
Dim led4 As PORTC.4
 
getting no where fast. I want to designate each led to a port but be able to use a FOR NEXT loop to access each element of the array (all 12 elements)
Code:
Dim x As Byte


Dim index(12) As Byte
 
'Dim led(0) As PORTC.0
Dim led1 As PORTC.1

led(3)= portc.1
while true
 
A matrix keypad is a scanning device. It's an active process where one column [or row] is made active and the rows [or columns] are checked for a response. Then the next column is made active and the rows checked again. And the process continues. The keypad modules take care of all the heavy lifting for you.

Sparkfun has a good tutorial – skip down to An Introduction to Matrix Scanning. Read this. Understand this.

A word about Swordfish Modules

Modules are used to add extended commands to Swordfish. To use a module, it is included in your code:

Include "LCD.bas"
Include "utils.bas"
'INCLUDE "Keypad16pullup.bas"
Include "Convert.bas"

You don't copy anything from the module into your code. You call the functions of the module. For example,

DecToStr(MyVariable)

is a command made available in the Convert.bas module. The module is included in your code, but you don't copy and paste from the module into your code. For the most part (always, in your case), you don't change anything in the module itself.

Modules are not used like example code. They are finished, perfected code that doesn't need any "help."


You can't define an array of LEDs (or anything) to port pins. Swordfish does not support it.
 
well that;s what I was hoping to do, assign an array to port pins.
I understand the scanning etc.
My reason for editing the keypad12.bas module was due to the fact that the module has pullups on different pins, thus the editing.
I ran the code using keypad12.bas and get ONLY key01
Will try something different to scan the keypad using a for next loop.
 
I'm looking forward to see how quickly this goes downhill? Visitor, your perseverance is admirable.

Mike.
 
I do have a bit of code that is not working as it is supposed to
Code:
Repeat  
until
led_row2=0 and sw_col1=0 //led and sw are both =0
issue is regardless the state of the led, the code jumps out to next sequence

I have no idea how you have things defined anymore or how your hardware is connected, but the following:
Code:
Device = 18F43k22
Clock = 8

Include "intosc.bas"
Include "setdigitalio.bas"

Dim SW_COL1 As PORTC.0
Dim LED_ROW2 As PORTB.5

SetAllDigital()

Output(LED_ROW2)
Input(SW_COL1)

Repeat   
Until
 LED_ROW2=0 and SW_COL1=0 

// get here only when LED_ROW2 is 0 and SW_COL1 is 0
LED_ROW2 = 1
works exactly as one would expect.
 
another issue has come up
lets say led#9 is on waiting for a button press
Well pressing button #1 will force code to jump out of the sequence of REPEAT UNTIL
This happens with other buttons as well. It appears on same colums or rows only.
Thinking of redesigning but connecting the leds in series with corresponding button. and maybe no 10k pull up resistors but??
 
Could you describe exactly what the end result of you mishmash of code is supposed to do? What is the end goal you are trying to accomplish?
 
My goal and issues of using a KEYPAD.BAS MODULE





have a keypad with 12 keys that have built in leds so the senerio is


Led/switch lights up, user presses lit switch


led shuts off and another led/switch combo is enabled waiting for a key-press


using the KEYPAD12,BAS module on my present pcb, it has four columns w/pull-up resistors and three rows w/o pull-ups, all on portc.


The KEYPAD12.bas module uses a 3x4 matrix but it uses pull-ups on three columns. My pcb uses four columns with pull-ups.


The KEYPAD12,bas uses bits 4-7 for the rows. My present pcb uses bits 0-3 for the colums and 4-6 for the rows, thus editing the keypad12.bas.


Another issue, the keypad modules scan the keypad waiting for an input


My setup outputs a desired key/led combo waiting for a key-press of lit led/switch combo.


Solution = design a new pcb that corrects the missing 2 traces and not have any pull-up resistors. Use the pull ups on portb. And use the keypad12.bas module and write code to compare the input to the programmed led/key combo.
 
been thinking of this project pretty much all day and came up with a solution.
look at using ADC to read all tweleve switches (two resistor ladders so the voltage between resistors is wider)
using this method would eliminate hopefully any false switch readings, no scanning of switches thus eliminating more than one switch being sensed (thats an issue I have at the present using posted code snippet
Code:
While True
SW_COL1=1   SW_COL2=1   SW_COL3=1  SW_COL4=1  //TIED HIGH W/ PULLUP RESISTOR INPUTS
//#1 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//led# 1  1-1
LED_COL1=1  LED_COL2=0  LED_COL3=0 LED_COL4=0
LED_ROW1=1  //leds 1-4  led1 is on             
LED_ROW2=1  //leds 5-8     
LED_ROW3=0  //leds 9-12               
Repeat   
          
Until
 LED_ROW3=0 And SW_COL1=0  //led and sw are both =0

DelayMS(500)
this code works good but get false switch readings (more than one switch will make code jump out of the REPEAT UNTIL loop.
going with ADC on two separate port pins seems like a good solution but?
 
Sorry, I am lacking the fortitude to decipher what you're trying to do.

I suggest you go through the tutorial I linked above carefully, and lay out your keypad to the arrangement expected in the keypad module. If pressing more than one key simultaneously is a problem, add the diodes as discussed in the tutorial.

If I had to guess, I'd say you're trying to make a reaction timer. Illuminate an LED, and time how long it takes to press the button. You can use the keypad module as intended for that:

1. Illuminate the LED

2. Start the timer

3. Query the keypad for a reading

4a. If the correct key is returned, stop the timer

4b. If an incorrect key is pressed, query the keypad again until the correct key is pressed.

Good luck.
 
thats exactly what my brother said "wack a mole"
I attempted to use the keypad12.bas and found it is returning for key pressed (connect power) it shows key 4
changing te 4 into a 11 in the keypad module and it shows key 11.
Have changed the row and colums around but no success. Just deconstructing the code to figure out what is actually going on.
here is my present edited keypad12
Code:
Module Keypad12rev
{
Column1 = PORTX.0
Column2 = PORTX.1
Column3 = PORTX.2
PORTX.3 NC
Row1 = PORTX.4
Row2 = PORTX.5
Row3 = PORTX.6
Row4 = PORTX.7
}

// validate data port...
#option KEYPAD_PORT = PORTC  //port B for pullups
#if IsOption(KEYPAD_PORT)
   #if Not IsValidPort(KEYPAD_PORT)
      #error KEYPAD_PORT, "Invalid option. Keypad must be connected to a valid port name."
   #endif
   #option _KEYPAD_PORT_TRIS = GetTRIS(KEYPAD_PORT)   
#endif

// bring PORT and TRIS options into the module
Dim
   FKeyPort As KEYPAD_PORT,
   FKeyPortTris As _KEYPAD_PORT_TRIS


Public Function Value() As Byte
    Dim Counter As Byte
    Dim RowData As Byte
    Dim ColData As Byte
    Result = 0                           
    Counter = 0
    rowdata=0
    For Counter = 0 To 3                //
        FKeyPortTris = $FF              // Make all pins inputs
        FKeyPortTris.Bits(Counter) = 1  // Make a single Column an output
        FKeyPort.Bits(Counter) = 0      //  and set it high
        
        If (FKeyPort >> 4) <> 0 Then    // Check if any Rows are "High"
            coldata = Counter
            If FKeyPort.0 = 0 Then
                rowData = 1
            ElseIf FKeyPort.1 = 1 Then
                rowData = 11           //4 here is what it shows
            ElseIf FKeyPort.2 = 1 Then
                rowData = 7
            ElseIf FKeyPort.3 = 1 Then
                rowData = 10               
            EndIf           
            Result = Coldata + rowData    //colum and row data
            Break         
        EndIf 
    Next
End Function
Public Sub Debounce()

    While Keypad12rev.Value <> 0           // Check if key is pressed
        DelayMS(10)                     // Wait for 10mS
    Wend 
End Sub

FKeyPortTris = $FF              // Make all pins inputs
End
 
the timer will measure how long it takes to pres all 12 buttons as the leds com on/off
going to still experiment with my setup to maybe discover why it displays the wrong stuff.
 
Just letting everyone know, I'm not commenting on this because I tried about 5 years ago. In the last 5 years the same questions have been asked and answered multiple times. It's like groundhog day over and over again. Just saying.

Mike.
 
Speaking of Ground Hog day, have you seen the Hog Day Jeep commercial from the Superbowl? At least something should be useful in this thread.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…