Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

reverse engineering the keypad module

Status
Not open for further replies.

MrDEB

Well-Known Member
been hacking away at a keypad with led/ switch combos but getting really lost.
Figure try and use the keypad module in Swordfish but what does >> stand for?
have a 4 x 3 keypad that my code turns on an led then I press the lighted switch to turn off the led then progress to another led/switch and press again.
Would post the code but needs cleaning up etc.
 
Click on....

Help
... help topics
...... language reference
......... operators
............ bitwise

You will see that >> means shift right
 
well here is my tangled code that has a mind of its own. this version just jumps from sequence 1 to sequence 2 automatically this is a 4 x 3 matrix. the sw_col are tied high w/ pull-up resistor Have verified all the connections etc. I am wondering if I screwed up with OUTPUTS AND INPUTS?
Code:
Device = 18F43k22
Clock = 8

Include "intosc.bas"   
#option SWORDFISH_SE = true
// some LCD options...
'#option KEYPAD_PORT = PORTc
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...

Include "setdigitalio.bas"//Include "SetDigitalIO.bas"
Include "LCD.bas"
Include "utils.bas"
'INCLUDE "Keypad16pullup.bas"
Include "Convert.bas"
 //Include ISR timer Module
Include "ISRTimer.bas"
Dim mS As Word


Dim s As String
//SWITCH MATRIX

Dim SW_COL1 As PORTC.0
Dim SW_COL2 As PORTC.1
Dim SW_COL3 As PORTC.2
Dim SW_COL4 As PORTC.3
Dim SW_ROW1 As PORTC.4
Dim SW_ROW2 As PORTC.5
Dim SW_ROW3 As PORTC.6

//LED MATRIX
Dim LED_COL1 As PORTB.0
Dim LED_COL2 As PORTB.1
Dim LED_COL3 As PORTB.2
Dim LED_COL4 As PORTB.3
Dim LED_ROW1 As PORTB.4
Dim LED_ROW2 As PORTB.5
Dim LED_ROW3 As PORTB.6
// OnTimer event, flash LED...
{
Sub OnTimer()
Inc(mS)
End Sub
// initialise the timer module...
Timer.Initialize(1)
initialise Timer.Items(0)
Timer.Items(0).Interval = 1 // 1mS
Timer.Items(0).OnTimer = @OnTimer // timer event handler
// enable the timers...
Timer.Items(0).Enabled = true
Input (button)
}
'TRISB=%00000000     //SET AS INPUTS
'TRISC=%11111111     //SET AS OUTPUTS
SetAllDigital

LED_COL1=0
LED_COL2=0
LED_COL3=0
LED_COL4=0
LED_ROW1=1
LED_ROW2=1
LED_ROW3=1
SW_ROW1=0   //go high with button press
SW_ROW2=0
SW_ROW3=0
SW_COL1=1    //already high w/ pullup resistor
SW_COL2=1
SW_COL3=1
SW_COL4=1

Output(LED_COL1)
Output(LED_COL2)
Output(LED_COL3)
Output(LED_COL4)
Output(LED_ROW1)
Output(LED_ROW2)
Output(LED_ROW3)
Input(SW_ROW1)
Input(SW_ROW2)
Input(SW_ROW3)
Output(SW_COL1)
Output(SW_COL2)
Output(SW_COL3)
Output(SW_COL4)

//switch colums tied high w/ pullup resistor
//led_col"x" are the anodes
//led_row"x" are the cathodes
 
While True
//#1 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


    //leds stay on until button press
    //led on

LED_COL1=1  LED_COL2=0  LED_COL3=0 LED_COL4=0
LED_ROW1=0  //leds 1-4  led1 is on             
LED_ROW2=1  //leds 5-8     
LED_ROW3=1  //leds 9-12               
    
SW_COL1=0   SW_COL2=1   SW_COL3=1  SW_COL4=1  //TIED HIGH W/ PULLUP RESISTOR
SW_ROW1=1       
SW_ROW2=1
SW_ROW3=1
 

   Repeat
   Until
    SW_ROW1=0 'And SW_COL1=1 //On button press SW_COLX goes low
 
 DelayMS(1000)
    
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //#2
 
LED_COL1=0  LED_COL2=0  LED_COL3=0 LED_COL4=1
LED_ROW1=1  //leds 1-4             
LED_ROW2=1  //leds 5-8     
LED_ROW3=0  //leds 9-12  LED #9 is on             
    
SW_COL1=1   SW_COL2=1   SW_COL3=1  SW_COL4=0  //TIED HIGH W/ PULLUP RESISTOR
SW_ROW1=1       
SW_ROW2=1
SW_ROW3=1
 

   Repeat
   Until
    SW_ROW3=0 'And SW_COL4=1  //On button press SW_COLX goes low
 
 DelayMS(1000)
    
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     
    
    
//#3     
 
LED_COL1=0  LED_COL2=1  LED_COL3=0 LED_COL4=0
LED_ROW1=1  //leds 1-4             
LED_ROW2=0  //leds 5-8   led #5 is on 
LED_ROW3=1  //leds 9-12               
    
SW_COL1=1   SW_COL2=0   SW_COL3=1  SW_COL4=1  //TIED HIGH W/ PULLUP RESISTOR
SW_ROW1=1       
SW_ROW2=1
SW_ROW3=1
 
//On button press SW_COLX goes low
   Repeat
   Until
    sw_row2=0 'and SW_COL2=1
 
 DelayMS(1000)
    
 Wend
 
one problem, I am programming the key that lights being the same key button.
The keypad.bas is searching which key is pressed, not the pre-selected/programmed key. I was/am thinking about reverse engineering the keypad..bas to compare the key programmed(led on) to key that is pressed.
took a break and now back at it. trying just one key sequence.
 
the sw_col are tied high w/ pull-up resistor
In your code, SW_COLx are the outputs and SW_ROWx are the inputs.
It's the INPUTS that need to have a pullup resistor, not the outputs.

Also, for the umpteenth time QUIT TRYING TO WRITE TO PINS THAT YOU HAVE SET AS INPUTS.
 
was worrying about what pins to write to.
Will try using the rows as outputs.
Going code hacking and attempting to use the pullup 16.bas but in reverse since my colums have pullups.
First will try your suggestion on inputs and outputs.
change the col to inputs and rows as outputs.
 
I would just use the keypad.bas

Code:
Device = 18F452     // make this the chip your using
Clock = 20

Include "Keypad.bas"      // keypad on portb
Include "Convert.bas"

 

Dim Value As Byte
Dim led0 As PORTC.0


 

// loop forever...
low (led0)
While true

   Keypad.WaitFor

   Value = Keypad.Value

   If Value > 0 Then
     HIGH (led0)
      

   EndIf   

Wend
 
will try your suggestion Burt. Tried editing the keypad16pullup.bas and got nowhere fast.
Maybe the keypad.bas is different?
tried using Tumbl;eweeds suggestion about the INPUTS AND OUTPUTS.
Made some progress but can press unlit key in same colum and has same result as pressing correct key.
I experimented changing the LED INPUT/OUTPUT with weird results but the switches seem to work but with the wrong key press issue.
Code:
output(LED_COL1)
output(LED_COL2)
output(LED_COL3)
output(LED_COL4)
output(LED_ROW1)
output(LED_ROW2)
output(LED_ROW3)
Output(SW_ROW1)
Output(SW_ROW2)
Output(SW_ROW3)
Input(SW_COL1)
Input(SW_COL2)
Input(SW_COL3)
Input(SW_COL4)

//switch colums tied high w/ pullup resistor
//led_col"x" are the anodes
//led_row"x" are the cathodes
 
While True
//#1 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
LED_COL1=1  LED_COL2=0  LED_COL3=0 LED_COL4=0
LED_ROW1=0  //leds 1-4  led1 is on             
LED_ROW2=1  //leds 5-8     
LED_ROW3=1  //leds 9-12               
Repeat   
    SW_COL1=1   SW_COL2=1   SW_COL3=1  SW_COL4=1  //TIED HIGH W/ PULLUP RESISTOR INPUTS
    SW_ROW1=0  //outputs     
    SW_ROW2=1
    SW_ROW3=1
Until
    SW_COL1=0 //and led_row1=0 DelayMS(30)//On button press SW_COLX goes LOW because sw_row1 is LOW
 
 DelayMS(500)
       LED_COL1=0  LED_COL2=0  LED_COL3=1 LED_COL4=0
        LED_ROW1=1  //leds 1-4  led1 is on             
        LED_ROW2=1  //leds 5-8     
        LED_ROW3=0  //leds 9-12     
 DelayMS(500)
    //Wend
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 //#2
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LED_COL1=0  LED_COL2=0  LED_COL3=1 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   
SW_COL1=1   SW_COL2=1   SW_COL3=1  SW_COL4=1  //TIED HIGH W/ PULLUP RESISTOR INPUTS
SW_ROW1=1  //outputs     
SW_ROW2=1
SW_ROW3=0
 
 
   Until
    SW_COL3=0 DelayMS(30)' And SW_COL1=0 //On button press SW_COLX goes high
 
 DelayMS(500)
   LED_COL1=0  LED_COL2=1  LED_COL3=0 LED_COL4=0
    LED_ROW1=1  //leds 1-4  led1 is on             
    LED_ROW2=0  //leds 5-8     
    LED_ROW3=1  //leds 9-12     
        
    DelayMS(1000)
    wend
going to give Burts suggestion a try???
 
I tried compiling etc but it needs a module I don't or can't find so back to my code that works except I can press a different key and it jumps to the next led/switch.
using 4 colums and three rows,
keys numbered 1 - 4 first row
5 - 8 second row
9 - 12 third row
led ON key 1, pressing key one led turns off and a different led/switch lights up
BUT led 1 ON and pressing led/switch # 9 ends the REPEAT UNTIL LOOP. not supposed to.
 
Apparently, you are illuminating an LED, and then waiting for the corresponding switch to be pressed....

Instead of trying to "reinvent" something you don't understand, why don't you try the following:

¤ Use the keypad module (not the keypad16pullup module if you have pullups installed). This will handle all the details for you, including those you don't understand, like why you can't set an INPUT PIN to a value.

¤ Turn on whichever LED you want.

¤ Wait for the keypad module to return a key and check if it's the desired one.

¤ Use BREAK to exit the above loop when you're tired of waiting. Read the help file.

What happened to the analog keypad you spent so much time doing?

[If anybody wants to know why I haven't been around for a week, ask the moderators. Some people's experiences leave no room for facts that other people have seen.]
 
Am looking at the keypad12 modul but need to figure out IF I can edit it for 4 output colums and three input rows instead of three colums and four rows..
Back at it early today for a fresh outlook on this project.
Am going to have to redesign a new board anyway but maybe learn something from this disaster. I missed 2 traces that I hardwired to be able to test with.. If I can't get it working as planned will have to order new redesigned boards now instead of several weeks?
Welcome back "visitor"
 
tried the BREAK no work
revised the KEYPAD12.BAS for 4 colums and three rows
LCD just sits there showing key 03 is pressed without pressing any keys. will study my results of why? One thing when compiling I get a caution (yellow) about KEYPRESSED. COULD BE THE ISSUE???
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
    For Counter = 0 To 2                //
        FKeyPortTris = $FF              // Make all pins inputs
        FKeyPortTris.Bits(Counter) = 0  // Make a single Column an output
        FKeyPort.Bits(Counter) = 1      //  and set it high
        
        If (FKeyPort >> 4) <> 0 Then    // Check if any Rows are "High"
            rowdata = Counter
            If FKeyPort.4 = 1 Then
                colData = 1
            ElseIf FKeyPort.5 = 1 Then
                colData = 4
            ElseIf FKeyPort.6 = 1 Then
                colData = 7
            ElseIf FKeyPort.7 = 1 Then
                colData = 10               
            EndIf           
            Result = Coldata + rowData
            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
 
typo error I failed to DIM value as BYTE
now all I get is key 39
more digging but making some progress
 
IF I can edit it for 4 output colums and three input rows instead of three colums and four rows..

You got be kidding there hook it up 4 and 3 the rest is software names
 
I hooked it up and all I get is o4 on the lcd.
trying to edit the keypad12 to work but getting nowhere fast
 
May I make a suggestion? Assume that the people who wrote the Swordfish modules know far more about how they work than you do, and use the modules as designed. If the modules don't work without making changes, stop and figure out how they are supposed to work.

I don't think anybody will be willing to go through your "fixed" code to try to comprehend what you're doing.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top