Matrix Keypad routine


Closed Thread
Results 1 to 40 of 135

Hybrid View

  1. #1
    Join Date
    Oct 2008
    Posts
    47


    Did you find this post helpful? Yes | No

    Exclamation Need help desperately!!!

    I'm doing a temperature control system. Pretty new to PIC programming. I have managed to display temperature on the LCD using an LM35 sensor and a 16 x 2 lcd with a 16F873. I want to add on a 4 x4 matrix keypad so that i can set setpoints to switch a relay and a low warning buzzer I have no idea how to program for the above mentioned add-ons. Please could someone help me. below is my code which will obviously need additions and a scematic of my circuit is attached . Please could somebody help me with this!!

    ' Define LCD registers and bits

    Define LCD_DREG PORTB
    Define LCD_DBIT 0
    Define LCD_RSREG PORTB
    Define LCD_RSBIT 5
    Define LCD_EREG PORTB
    Define LCD_EBIT 4
    dEFINE LCD_BITS 4
    DEFINE LCD_LINES 2

    adval var word ' Create adval to store result
    temp var word ' Create temp to store result
    temp1 var word ' Create temp1 to store result

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %10000010 ' Set PORTA analog and RIGHT justify result
    ADCON0 = %11000001 ' Configure and turn on A/D Module
    Pause 100 ' Wait 0.1 second


    loop: ADCON0.2 = 1 ' Start Conversion

    AGAIN: Pause 1
    If ADCON0.2 = 1 Then AGAIN ' Wait for low on bit-2 of ADCON0, conversion finished

    adval.highbyte = ADRESH ' Move HIGH byte of result to adval
    adval.lowbyte = ADRESL ' Move LOW byte of result to adval

    Lcdout $fe, 1 ' Clear screen
    temp=50*adval ' Conversion to Degrees
    temp=temp/100
    Lcdout "TEMP = ",DEC temp,$DF,"C" ' Display the value of temp
    temp1 = temp*18 ' Conversion to Fahrenheit
    temp1 = temp1+320
    temp1 = temp1/10
    lcdout $FE,$C0, "TEMP = ",dec temp1,$DF,"F" ' Display the value of temp
    Pause 1000 ' Wait 1 second

    Goto loop ' Do it forever
    End
    Attached Images Attached Images  

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kalind View Post
    I'm doing a temperature control system. Pretty new to PIC programming. I have managed to display temperature on the LCD using an LM35 sensor and a 16 x 2 lcd with a 16F873. I want to add on a 4 x4 matrix keypad so that i can set setpoints to switch a relay and a low warning buzzer I have no idea how to program for the above mentioned add-ons. Please could someone help me. below is my code which will obviously need additions and a scematic of my circuit is attached . Please could somebody help me with this!!
    Schematic for keypad connection to a PIC is talked about at the beginning of this thread. If you need a schematic, draw it up... Shouldn't be that difficult at all...dontcha think?

    As far as the code goes, you've gotten this far (or have you? Ctrl-C/Ctrl-V)...
    Why not write some code, doesn't even have to be real code, just talk yourself thru it, and type it out as you talk. After you see it written down, you just might be able to write the code yourself.

  3. #3
    Join Date
    Oct 2008
    Posts
    47


    Did you find this post helpful? Yes | No

    Question

    i have written this code. its just that im really confused and lost as to how to begin with the keypad. i kinda figured out how to hook up the keypad though. i just need help as to how to go about doing the code

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kalind View Post
    i have written this code. its just that im really confused and lost as to how to begin with the keypad. i kinda figured out how to hook up the keypad though. i just need help as to how to go about doing the code
    I am HELPING. No need for whiney PM's.
    Key word here is HELPING...not DOING.
    I think you'll find a whole lot of people around here willing to HELP...some might even be willing to DO.
    Write some code, in fact, don't even write code, just write a sequence of thoughts of how you think the program should operate. The code will follow. That's how it works. You think about something and it happens eventually.
    As far as your keypad goes, what's wrong with the code that's provided earlier in the thread? I use it...works great for me.

  5. #5
    Join Date
    Oct 2008
    Posts
    47


    Did you find this post helpful? Yes | No

    Default

    well, i was thinking that when i push "Button A" of the keyapd, the upper setpoint will be displayed, "Button B" = Lower limit, "Button C" = Backspace, "Button D" = OK , "Button *" = alarm on/off, "Button #" = Main screen

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kalind View Post
    well, i was thinking that when i push "Button A" of the keyapd, the upper setpoint will be displayed, "Button B" = Lower limit, "Button C" = Backspace, "Button D" = OK , "Button *" = alarm on/off, "Button #" = Main screen
    Ok, so you've defined the FUNCTIONs of the buttons...now how to you act on those functions.
    In fact, do yourself a favor and just work on adding one button at a time...

  7. #7
    Join Date
    Oct 2008
    Posts
    47


    Did you find this post helpful? Yes | No

    Default

    thats my point. I have no idea where to begin...i was thinking maybe scan keypad first, check if A or B was pressed and if so then act upon it by prompting the user to enter a desired setpoint. once that's done, press ok and return to the main display screen. the alarm can be set on for the desired lower setpoint and once the upper setpoint has been reached, the relay should switch on. vice versa

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kalind View Post
    well, i was thinking that when i push "Button A" of the keyapd, the upper setpoint will be displayed, "Button B" = Lower limit, "Button C" = Backspace, "Button D" = OK , "Button *" = alarm on/off, "Button #" = Main screen
    Code:
    InterruptHandler:
    @ READKEYPAD _MyVar
    resume
    'now your key reading is stored in MyVar
    most 16 key keypads are laid out 123A 456B 789C *0#D, so 1=1 2=2 3=3 A=4 4=5 . . . .
    You can use this as is or use a lookup routine to alias the actual port reading to what
    you want, like so:
    Code:
    lookup MyVar,[0,"123A456B789C*0#D"],MyOtherVar
    now your key information stored in myOtherVar will mirror your keypad.
    so . . .
    Code:
    If myothervar = A then gosub Uppersetpoint
    if MyOtherVar = b then gosub . . . .easy
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  9. #9
    Join Date
    Oct 2008
    Posts
    51


    Did you find this post helpful? Yes | No

    Default

    Hay guys and girls. I've been playing around with this coding this afternoon and i keep getting errors and when i dont nothing going as i thought it should so any chance somone could point out whats wrong please or point me in the right dirrection at least!

    My main coding:

    Code:
    TRISA=%00000011
    TRISB=%00000000
    TRISC=%11111110
    ADCON0=%11000000
    ADCON1=%00000111 
    INCLUDE "KeyPad.bas"         
    
        '                      
        '    Hardware connection
        '    ===================
        DEFINE KEYPAD_ROW        8       ' 8 row 
        define KEYPAD_ROW_PORT   PORTc   '   on PORTB
        DEFINE KEYPAD_ROW_BIT    0       '      <7:0>
        DEFINE KEYPAD_COL        4       ' 4 col 
        DEFINE KEYPAD_COL_PORT   PORTc   '   on PORTA
        DEFINE KEYPAD_COL_BIT    0       '      <3:0>
        DEFINE KEYPAD_DEBOUNCEMS 200     ' debounce delay = 200 mSec
        define KEYPAD_AUTOREPEAT 1       ' use auto-repeat
    ' LCD Display
    ' -----------
    ' Adjust these to suit your chosen LCD pinout
    '
    DEFINE LCD_DREG PORTb 			'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT 4 			'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTb 			'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 3 			'Define Portb pin used for RS connection
    DEFINE LCD_EREG PORTb 			'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 0 			'Define PortB pin used for E connection
    DEFINE LCD_BITS 4 			'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2 			'Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000 		'Define delay between sending LCD commands
    DEFINE LCD_DATAUS 50 			'Define delay time between data sent.
        
        '
        '    Serial Communication definition
        '    ===============================
        DEFINE HSER_TXSTA 24h            ' enable transmit, BRGH=1
        DEFINE HSER_SPBRG 129            ' 9600 Bauds
    
        '   
        '    Variables definition 
        '    ===================
        myvar                    var byte
    
        '    ---------------------------------[Program Start]----------------------------------------------
    start:
        @ READKEYPAD _myvar
        hserout ["Key=",dec myvar,13,10]
    	gosub lcddisp
        goto start
    
    lcddisp:
    LCDOUT $FE, 1, myvar
    pause 60
    return
    end

    The modified Keypad code
    Code:
    TRISA=%00000011
    TRISB=%00000000
    TRISC=%11111110
    ADCON0=%11000000
    ADCON1=%00000111 
    INCLUDE "KeyPad.bas"         
    
        '                      
        '    Hardware connection
        '    ===================
        DEFINE KEYPAD_ROW        8       ' 8 row 
        define KEYPAD_ROW_PORT   PORTc   '   on PORTB
        DEFINE KEYPAD_ROW_BIT    0       '      <7:0>
        DEFINE KEYPAD_COL        4       ' 4 col 
        DEFINE KEYPAD_COL_PORT   PORTc   '   on PORTA
        DEFINE KEYPAD_COL_BIT    0       '      <3:0>
        DEFINE KEYPAD_DEBOUNCEMS 200     ' debounce delay = 200 mSec
        define KEYPAD_AUTOREPEAT 1       ' use auto-repeat
    ' LCD Display
    ' -----------
    ' Adjust these to suit your chosen LCD pinout
    '
    DEFINE LCD_DREG PORTb 			'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT 4 			'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTb 			'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 3 			'Define Portb pin used for RS connection
    DEFINE LCD_EREG PORTb 			'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 0 			'Define PortB pin used for E connection
    DEFINE LCD_BITS 4 			'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2 			'Define using a 2 line LCD
    DEFINE LCD_COMMANDUS 2000 		'Define delay between sending LCD commands
    DEFINE LCD_DATAUS 50 			'Define delay time between data sent.
        
        '
        '    Serial Communication definition
        '    ===============================
        DEFINE HSER_TXSTA 24h            ' enable transmit, BRGH=1
        DEFINE HSER_SPBRG 129            ' 9600 Bauds
    
        '   
        '    Variables definition 
        '    ===================
        myvar                    var byte
    
        '    ---------------------------------[Program Start]----------------------------------------------
    start:
        @ READKEYPAD _myvar
        hserout ["Key=",dec myvar,13,10]
    	gosub lcddisp
        goto start
    
    lcddisp:
    LCDOUT $FE, 1, myvar
    pause 60
    return
    end



    forgot to put the error code

    Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F876A "KEYPADTE.ASM" /l"KEYPADTE.lst" /e"KEYPADTE.err"
    Error[113] C:\PBP\PBPPIC14.LIB 2784 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2785 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2789 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2790 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2800 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2871 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2889 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2913 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2920 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2928 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\PBPPIC14.LIB 2934 : Symbol not previously defined (PORTb)
    Error[113] C:\PBP\KEYPADTE.ASM 100 : Symbol not previously defined (PORTc)
    Error[113] C:\PBP\KEYPADTE.ASM 103 : Symbol not previously defined (PORTc)
    Error[113] C:\PBP\KEYPADTE.ASM 125 : Symbol not previously defined (PORTc)
    Error[113] C:\PBP\KEYPADTE.ASM 136 : Symbol not previously defined (PORTc)
    Halting build on first failure as requested.
    BUILD FAILED: Thu Nov 20 16:00:57 2008
    Attached Images Attached Images  
    Last edited by chrisshortys; - 20th November 2008 at 17:45.

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Well the answeris in mister E's code, it works very well give it a true it is very well documented and easy to use

    Walter


    Quote Originally Posted by Kalind View Post
    I'm doing a temperature control system. Pretty new to PIC programming. I have managed to display temperature on the LCD using an LM35 sensor and a 16 x 2 lcd with a 16F873. I want to add on a 4 x4 matrix keypad so that i can set setpoints to switch a relay and a low warning buzzer I have no idea how to program for the above mentioned add-ons. Please could someone help me. below is my code which will obviously need additions and a scematic of my circuit is attached . Please could somebody help me with this!!

    ' Define LCD registers and bits

    Define LCD_DREG PORTB
    Define LCD_DBIT 0
    Define LCD_RSREG PORTB
    Define LCD_RSBIT 5
    Define LCD_EREG PORTB
    Define LCD_EBIT 4
    dEFINE LCD_BITS 4
    DEFINE LCD_LINES 2

    adval var word ' Create adval to store result
    temp var word ' Create temp to store result
    temp1 var word ' Create temp1 to store result

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %10000010 ' Set PORTA analog and RIGHT justify result
    ADCON0 = %11000001 ' Configure and turn on A/D Module
    Pause 100 ' Wait 0.1 second


    loop: ADCON0.2 = 1 ' Start Conversion

    AGAIN: Pause 1
    If ADCON0.2 = 1 Then AGAIN ' Wait for low on bit-2 of ADCON0, conversion finished

    adval.highbyte = ADRESH ' Move HIGH byte of result to adval
    adval.lowbyte = ADRESL ' Move LOW byte of result to adval

    Lcdout $fe, 1 ' Clear screen
    temp=50*adval ' Conversion to Degrees
    temp=temp/100
    Lcdout "TEMP = ",DEC temp,$DF,"C" ' Display the value of temp
    temp1 = temp*18 ' Conversion to Fahrenheit
    temp1 = temp1+320
    temp1 = temp1/10
    lcdout $FE,$C0, "TEMP = ",dec temp1,$DF,"F" ' Display the value of temp
    Pause 1000 ' Wait 1 second

    Goto loop ' Do it forever
    End

  11. #11
    Join Date
    Oct 2008
    Posts
    47


    Did you find this post helpful? Yes | No

    Question

    ok, now that i have e's keypad code, how do i implement the idea that i mentioned in #90?

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kalind View Post
    ok, now that i have e's keypad code, how do i implement the idea that i mentioned in #90?
    Do you have the code working?
    What happens when "A" is pressed now?
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Need help desperately!!!

    handy key fifo for this routine to keep 5 key strokes deep

    Code:
     
      if key >0  then 
           
       for Key_count  = 4 to 1 step - 1 
           Key_value(Key_count) = Key_value( Key_count - 1)
       next Key_count     
     
        Key_Value(0) = Key
       
       endif

  14. #14
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Need help desperately!!!

    How can I use this include for something like this ???
    Code:
    main:
    @ READKEYPAD _myvar
    gosub check
    goto main
    
    check:
    select case myvar
    
    case 1  
    porta.0 = 1 ; WHILE CASE 1 PORTA.0 = 1, THEN PORTA.0 = 0
    Thanks !

  15. #15
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Matrix Keypad routine

    Code:
    CASE 1  
      Porta.0 = 1
      WHILE myVar = 1
        @ READKEYPAD _myvar
      WEND
      PortA.0 = 0
    /Henrik.

  16. #16
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Matrix Keypad routine

    Thank You!

    LE : But don't work.
    Press button 1 of matrix ; led 1 = OFF ; end pressing button 1 ; led 1 = ON untill press ANOTHER button ...say button 2.
    But pressing button 2 don't turn ON led2 ; just another press and release of button 2 turn correspondent led (2) ON.
    Attached Images Attached Images  
    Last edited by fratello; - 12th July 2015 at 18:39.

  17. #17
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Matrix Keypad routine

    In your first post you indicated that you wanted PortA.0 high while the myVar was equal to 1 - is that not what's happening with the code?
    How do you want it to work and what does the code look like?

  18. #18
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: Matrix Keypad routine

    Thanks for support , Mr.Henrik !
    What I want :
    - porta.x = ON while correspondent button.x is pressed.
    How work :
    - press button x ; do nothing ; release button x - porta.x=ON ; remain ON while press ANOTHER button(say y) ...But this new button(y) don't turn correspondent porta.y ON ; just turn OFF previous porta.x ; only after the release of the second press turn on correspondent porta.y...
    Attached Files Attached Files
    Last edited by fratello; - 12th July 2015 at 19:20.

Similar Threads

  1. calculator-like code entry with matrix keypad and display
    By Dennis in forum mel PIC BASIC Pro
    Replies: 35
    Last Post: - 16th December 2009, 22:58
  2. Need help in matrix keypad coding
    By rano_zen06 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th May 2008, 13:16
  3. I2C PCF8574 4X4 Keypad routine
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th January 2007, 21:25
  4. Inconsistent output on a 4x4 matrix keypad
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th November 2006, 03:54
  5. very basic matrix keypad question
    By kitcat in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th July 2006, 21:33

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts