News:

Let's find out together what makes a PIC Tick!

Main Menu

Inkey broken?

Started by See_Mos, Jul 04, 2021, 04:31 PM

Previous topic - Next topic

See_Mos

Using the various example files I have been unable to get INKEY to work as it should.

The closest that I can get is the example in the help file but the 1 key which should return a value of 0 does nothing.  all of the other keys return the correct key value key 4 = 1, key 7 = 2 etc all the way up to key D which returns 15 and no key = 16.

If I write my own code to simply monitor the action of the keys then 1 works OK so I know the keypad is OK.


top204

Using the Inkey.bas demo program and the Isis PIC16_ALCD file, Inkey seems to be working.

What devices type are you using, because this is a very important aspect of a program because each device family operate differently, and require slightly different mnemonics, but they all work on exactly the same principle for Inkey.

Make sure you have pull-up resistors on the keypad because some microcontrollers do not have the same control of the internal pull-ups.

With a code question is it is always required to have a code snippet showing a possible failure, otherwise, the answer is guesswork.

Giuseppe

where can I find the files :demo Inkey.bas and file Isis PIC16_ALCD ?

RGV250

Quotewhere can I find the files :demo Inkey.bas and file Isis PIC16_ALCD ?

In the samples folder, for the VSM you need to change the programmer with the drop down menu to "Labcenter electronics PROTEUS VSM" and when you compile and program it opens up the VSM options.

Bob

Giuseppe

Thanks for the info

RGV250

The VSM is quite useful as you can single step through your code and set breakpoints, even if the device you have is not covered with a bit of ingenuity you can test small routines etc.

Bob

See_Mos

The device is an 18F25K22 using PORTC for the keypad with a 10K SIL resistor pack for pullup.  The code is based on the help file :-

Declare Keypad_Port = PORTC
Dim bVar1 As Byte
While 1 = 1
bVar1 = InKey ' Scan the keypad
DelayMS 50 ' Debounce by waiting 50ms
Print At 1,1,Dec bVar1, " " ' Display the result on the LCD 
Wend

I will test the other samples later and give more details of the errors.

In the picture the connector is PIN 1, black, negative, pin 2 RC0 etc and pin 10 VDD.  The SIL socket allows swapping from pull up to pull down

RGV250

Hi,
It may sound obvious but have you checked with a meter that button 1 works?

Bob

top204

#8
The device you are using does not have internal pull-up resistors that can be enabled at the program's setup, so external pull-up resistors will need to be added to pins PORTC.4 to PORTC.7 for the keypad. As illustrated in the attached diagram.

The simple demo program below operates perfectly with the circuit:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate the Inkey command
' Because a device is being used that does not have internal pull-up resistors that can be enabled for the PORT used,
' pull-up resistors will need to be added to PORTC.4 to PORTC.7
'
' Written by Les Johnson for the Positron8 BASIC compiler.
' https://sites.google.com/view/rosetta-tech/home
'
    Device = 18F25K22
    Declare Xtal = 20
'
' Setup for software serial because PORTC is being used for the keypad, so the USART pins cannot be used
'
    Declare Serial_Baud = 9600
    Declare RSOut_Pin = PORTB.0
    Declare RSOut_Mode = 0
'
' Set the port to use for Inkey
'
    Declare Keypad_Port = PORTC
'
' Create a variable for the demo
'  
    Dim KeyValue As Byte

'------------------------------------------------------------------------
' The main program starts here
' Scan a keypad and transmit the values received from key presses to a serial terminal
'
    Do                          ' Create a loop
        KeyValue = InKey        ' Read the keypad
        RsOutLn Dec KeyValue    ' Transmit the value of the key pressed (if any)
        DelayMS 100             ' A small delay so we can see the changes
    Loop                        ' Do it forever

Please..... Before blaming the compiler, work out if it is a circuit or user program fault because a lot of people see the negative words and ignore the rest of a thread or post, which may impact on the, already, few sales of the compilers .

Keypad.jpg

top204

#9
To see the equivalent mechanism used by the compiler's Inkey command, the program below has a procedure that implements reading a keypad the same way as Inkey does. It even uses the Inkey declare to set the Port that the keypad is connected too. The program below operates with the circuit above:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a procedure to read a KeyPad connected to PORTC
' Because a device is being used that does not have internal pull-up resistors that can be enabled for the PORT used,
' pull-up resistors will need to be added to PORTC.4 to PORTC.7
'
' Written by Les Johnson for the Positron8 BASIC compiler.
' https://sites.google.com/view/rosetta-tech/home
'
    Device = 18F25K22
    Declare Xtal = 20
'
' Setup for software serial because PORTC is being used so the USART cannot be implemented for serial coms
'
    Declare Serial_Baud = 9600
    Declare RSOut_Pin = PORTB.0
    Declare RSOut_Mode = 0
'
' Setup the port to use for the Keypad reading procedure
'   
    Declare Keypad_Port = PORTC
'
' Create a variable for the demo
'   
    Dim KeyValue As Byte

'------------------------------------------------------------------------
' The main program starts here
' Scan a KeyPad and transmit the values received from key presses to a serial terminal
'
    Do                          ' Create a loop
        KeyValue = GetKey()     ' Read the KeyPad attached to PORTC
        RsOutLn Dec KeyValue    ' Transmit the value of the key pressed (if any)
        DelayMS 100             ' A small delay so we can see the changes
    Loop                        ' Do it forever

'------------------------------------------------------------------------
' Scan a 16-button KeyPad
' Input     : None
' Output    : Returns with the value of the keypress. If no key pressed, a value of 16 is returned
' Notes     : Uses the PORT set by the Declare KeyPad_Port directive
'           : Requires pull-up resistors on pins PORTx.4 to PORTx.7
'
Proc GetKey(), Byte
    Input __KEYPAD_PORT                 ' \
    PinOutput __KEYPAD_PORT.0           ' |
    PinOutput __KEYPAD_PORT.1           ' | Make the appropriate pins Inputs & Outputs
    PinOutput __KEYPAD_PORT.2           ' |
    PinOutput __KEYPAD_PORT.3           ' /
'
' Scan the columns of the KeyPad
'
    Result = 0                          ' Reset the result before looking for a key press
    __KEYPAD_PORT = %00001110           ' Pull the First column low
    GoSub Scan_Row                      ' Check the Row
    If STATUSbits_C = 1 Then            ' Key pressed?
        ExitProc                        ' Yes. So exit the procedure
    EndIf
    __KEYPAD_PORT = %00001101           ' Pull the Second column low
    GoSub Scan_Row                      ' Check the Row
    If STATUSbits_C = 1 Then            ' Key pressed?
        ExitProc                        ' Yes. So exit the procedure
    EndIf
    __KEYPAD_PORT = %00001011           ' Pull the Third column low
    GoSub Scan_Row                      ' Check the Row
    If STATUSbits_C = 1 Then            ' Key pressed?
        ExitProc                        ' Yes. So exit the procedure
    EndIf
    __KEYPAD_PORT = %00000111           ' Pull the Fourth column low
    GoSub Scan_Row                      ' Check the Row
    ExitProc                            ' Exit the procedure
'
' Scan one row of the KeyPad.
' The subroutine returns with the Carry flag set if a key is pressed and clear if no key pressed.
'
Scan_Row:
    If __KEYPAD_PORT.4 = 0 Then         ' Test the first row
        GoTo SetCarry_Exit              ' Exit Carry flag set if a key is pressed
    EndIf
    Inc Result
    If __KEYPAD_PORT.5 = 0 Then         ' Try the second row
        GoTo SetCarry_Exit              ' Exit Carry flag set if a key is pressed
    EndIf
    Inc Result                           
    If __KEYPAD_PORT.6 = 0 Then         ' Try the third row
        GoTo SetCarry_Exit              ' Exit Carry flag set if a key is pressed
    EndIf
    Inc Result                         
    If __KEYPAD_PORT.7 = 0 Then         ' Try the fourth row
        GoTo SetCarry_Exit              ' Exit Carry flag set if a key is pressed
    EndIf
    Inc Result
    Clear STATUSbits_C                  ' Clear the carry flag, to indicate no key pressed
    Return
SetCarry_Exit:
    Set STATUSbits_C
EndProc

See_Mos

My apologies Les, turns out the new keypad has high resistance contacts.  While most of the buttons are around 1K to 1K5 the top row is around 3K and the #1 button 3.5K

I only need it for a temporary test so I will create some simple code as a work around.