Interfacing with numeric keypad and seven segment display

Discussion in 'Programmer's Corner' started by 74266, Oct 1, 2016.

  1. 74266

    Thread Starter Member

    Mar 20, 2016
    41
    0
    I am planning to make a code where after you push two numbers in numeric keypad and press the # button it will start to decrements until it will
    stop to 0

    I will assign:
    RD as the output of 2 seven segment(is will 2 7447 IC decoder)
    RA[0-3] as the row input of keypad
    RB[0-2] as the column input of keypad

    upload_2016-10-2_19-5-36.png
    ---------------------- -------------------------
    What I dont know is what will be the benefit on using RB change interrupt
     
    Last edited: Oct 2, 2016
  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,809
    834
    Decrement from where?

    For example, if you need to press the keypad while decrementing or doing something else, the interrupt method will allow you to continue processing while not missing a key press.

    What is this code going to be implemented on? PIC? Pi? Arduino? AVR?
     
    74266 likes this.
  3. 74266

    Thread Starter Member

    Mar 20, 2016
    41
    0
    In Pic and Assembly code
    Also what I am saying in decrement is that if I press [9] then I press [1] what it will display is 91 then if you press # button it will decrement from
    91 until it reaches 00
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,346
    1,029
    This thread has an example of how to do a keypad/display with a PIC. There are others here that you can find by searching the forum.
    http://forum.allaboutcircuits.com/t...nt-display-using-8051-microcontroller.100549/

    I would not use interrupt on change for basic scanning. Instead, create a timer interrupt that scans the keypad and emits valid key codes. The basic process in the timer interrupt service routine is:
    For N rows
    Turn on a row and see if any keys aim the column are pressed.
    If not,do the next row.
    When you find a key pressed, scan a few more times to validate the key (debounce).
    When satisfied, combine the row and column numbers to get a unique key code - it doesn't have to match the legend on the key yet. For example, key 3 (11 in binary) on row 1 (01) could combine to code 1101 ( key code 13)
    Store the key code end set a flag to indicate a new key.
    Continue scanning the matrix until no keys are pressed, indicating the key is released.
    Resume scanning for the next key.

    In your main code, poll the flag to look for new keys emitted by the keypad scanner. When the flag is set, read the key code and clear the flag for the next key. Decode the key code directly or use a lookup table to translate key the key code to a suitable character. Process the keys as desired. For example 0-9 go into the preset, #starts the countdown etc.

    The main points are to scan the keypad with its own little engine and make it emit valid key codes separately from the processing. You can add a translation table between the scanner and the processor to make things easier to manage. Many keypads use some variation on this technique.

    Display multiplexing is frequently timed by the same interrupt and sometimes shares hardware segments driven by the row lines, etc. That's more complex but doable. There are lots of display multiplexing examples here - search away. I mux LEDs using a timer interrupt as well; The main routine stores the digits to display. The digit values are used as an index into a table of segment patterns. The pattern for the current digit gets fetched up, written to the port and the digit gets turned on. Next interrupt, next digit and so on.

    This sort of rat of thing isn't trivial for the beginner but by breaking it into small functional blocks becomes manageable and learnable.

    Good luck!
     
    Last edited: Oct 1, 2016
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    google 1628 or 1629 - they are designed specifically for applications like that. the 38/39 chips do as well.
     
  6. 74266

    Thread Starter Member

    Mar 20, 2016
    41
    0
    THIS is the code I made but when use it in my dev board it is not showing any output
    Code (Text):
    1.  
    2.  
    3.     #include "P18F4550.inc"
    4.    
    5.     config  FOSC = HS      
    6.     config  CPUDIV = OSC1_PLL2
    7.     config  PLLDIV = 1
    8.     config  PWRT = OFF
    9.     config  BOR = OFF
    10.     config  WDT = OFF
    11.     config  MCLRE = ON
    12.     config  STVREN = ON
    13.     config  LVP = OFF
    14.     config  ICPRT = OFF
    15.     config  XINST = OFF
    16.     config  DEBUG = OFF
    17.     config  FCMEN = OFF
    18.     config  IESO = OFF
    19.     config  LPT1OSC = OFF
    20.     config  CCP2MX = ON
    21.     config  PBADEN = OFF
    22.     config  USBDIV = 2
    23.     config  VREGEN = OFF
    24.    
    25.     WHAT_BUTTON equ 0x000
    26.     OUTPUT    equ 0x001  
    27.     COLUMN    equ 0x002
    28.     OPERATOR    equ 0x003
    29.     SEGMENT     equ 0x004
    30.     TEMP        equ 0x005
    31.     ZERO        equ 0x006
    32.     W_TEMP      equ 0x007
    33.     S_TEMP      equ 0x008
    34.    
    35.     org 0x0000
    36.     BRA START
    37.    
    38.     org 0x0008
    39.     BRA HISR
    40.  
    41.    
    42.     org 0x0018
    43.     BRA LISR
    44.  
    45.    
    46. START
    47.     ; Disable Analog Comparators multiplexed with RA & RB
    48.     MOVLW 0x0F
    49.     MOVWF ADCON1, ACCESS
    50.     MOVLW 0x07
    51.     MOVWF CMCON, ACCESS
    52.  
    53.  
    54. SETUP  
    55.     CLRF ZERO,ACCESS
    56.     CLRF WHAT_BUTTON, ACCESS
    57.     CLRF OUTPUT, ACCESS
    58.     ; Set the direction of RA[0-3] to INPUT;
    59.     MOVLW 0x0F
    60.     MOVWF TRISA, ACCESS
    61.      ; Set the direction of RB[0-2] to INPUT;
    62.     MOVLW 0x07
    63.     MOVWF TRISB, ACCESS
    64.    
    65.     ; Set the direction of RD[0-7] to OUTPUT
    66.     CLRF TRISD, ACCESS
    67.  
    68.    
    69.    
    70. ;INT0 & INT1 & INT2 :LOW PRIORITY
    71. ;TIMER0:HIGH PRIORITY
    72.    
    73.    
    74. ;FOR INT0-COLUMN3
    75.     BCF INTCON, INT0IF, ACCESS
    76.     BSF INTCON, INT0IE, ACCESS
    77.     BCF INTCON2, INTEDG0, ACCESS
    78. ;FOR INT1-COLUMN2    
    79.     BCF INTCON3, INT1IF, ACCESS
    80.     BSF INTCON3, INT1IE, ACCESS
    81.     BCF INTCON3, INT1IP, ACCESS
    82.     BCF INTCON2, INTEDG1, ACCESS
    83. ;FOR INT2-COLUMN1
    84.     BCF INTCON3, INT2IF, ACCESS
    85.     BSF INTCON3, INT2IE, ACCESS
    86.     BCF INTCON3, INT2IP, ACCESS
    87.     BCF INTCON2, INTEDG2, ACCESS
    88. ;FOR INT  
    89.     BSF RCON, IPEN, ACCESS
    90.     BSF INTCON, GIEH, ACCESS
    91.     BSF INTCON, GIEL, ACCESS
    92.     BSF INTCON, RBIE, ACCESS
    93.     BCF INTCON, RBIF, ACCESS
    94.    
    95. ; INTERRUPT - TMR0
    96.     BCF INTCON, TMR0IF, ACCESS
    97.     BSF INTCON2, TMR0IP, ACCESS
    98.     BSF INTCON, TMR0IE, ACCESS
    99.  
    100.  
    101. ; TMR0
    102.     MOVLW 0xFA
    103.     MOVWF TMR0H, ACCESS
    104.     MOVLW 0xE9
    105.     MOVWF TMR0L, ACCESS
    106.     MOVLW 0x87            ; 16BIT: 1:256 PRESCALER; INTERNAL CLOCK
    107.     MOVWF T0CON, ACCESS
    108.    
    109. MAIN
    110.     BTFSC COLUMN,0x2,ACCESS
    111.     BRA COLUMN_2
    112.     BTFSC COLUMN,0x1,ACCESS
    113.     BRA COLUMN_1
    114.     BTFSC COLUMN,0x0,ACCESS
    115.     BRA COLUMN_0
    116.    
    117.    
    118. COLUMN_2
    119.     BTFSC PORTA, 0x0, ACCESS      
    120.     MOVLW 0x03                ;'00000001' - NUM3  
    121.     BTFSC PORTA, 0x1, ACCESS      
    122.     MOVLW 0x06                ;'00000100' - NUM6
    123.     BTFSC PORTA, 0x2, ACCESS          
    124.     MOVLW 0x09                ;'00000111' - NUM9  
    125.     BTFSC PORTA, 0x3, ACCESS          
    126.     BTG OPERATOR, 0x0, ACCESS        ;'00000111' - NUM#  
    127.     BRA DISPLAY
    128.  
    129. COLUMN_1
    130.     BTFSC PORTA, 0x0, ACCESS      
    131.     MOVLW 0x02                ;'00000010' - NUM2  
    132.     BTFSC PORTA, 0x1, ACCESS      
    133.     MOVLW 0x05                ;'00000101' - NUM5
    134.     BTFSC PORTA, 0x2, ACCESS          
    135.     MOVLW 0x08                ;'00001000' - NUM8  
    136.     BTFSC PORTA, 0x3, ACCESS          
    137.     MOVLW 0x00                ;'00000000' - NUM0  
    138.     BRA DISPLAY
    139. COLUMN_0  
    140.     BTFSC PORTA, 0x0, ACCESS      
    141.     MOVLW 0x01                ;'00000001' -NUM1  
    142.     BTFSC PORTA, 0x1, ACCESS      
    143.     MOVLW 0x04                ;'00000100' -NUM4
    144.     BTFSC PORTA, 0x2, ACCESS          
    145.     MOVLW 0x07                ;'00000111' -NUM7  
    146.     BRA DISPLAY
    147.    
    148. DISPLAY
    149.     ;INT2 - COLUMN_0
    150.     ;INT1 - COLUMN_1
    151.     ;INT0 _ COLUMN_2
    152.     MOVWF WHAT_BUTTON,ACCESS
    153.     MOVF OUTPUT,W,ACCESS
    154.     MOVWF TEMP,ACCESS
    155.     BTFSC SEGMENT,0x0,ACCESS
    156.     SWAPF TEMP,W,ACCESS
    157.     BTFSC SEGMENT,0x1,ACCESS
    158.     IORWF OUTPUT,W,ACCESS
    159.     BTFSC OPERATOR,0x0,ACCESS
    160.     BRA START_DEC
    161.     MOVWF LATD, ACCESS
    162.    
    163. START_DEC
    164.     MOVF ZERO,W,ACCESS
    165.     CPFSGT OUTPUT,ACCESS
    166.     BRA MAIN
    167.     MOVWF LATD,ACCESS    
    168.     BRA MAIN  
    169.    
    170. INT0_ISR
    171.     BTG SEGMENT,0x0,ACCESS
    172.     BTG COLUMN, 0x0, ACCESS
    173.     BCF INTCON, RBIF, ACCESS
    174.     BCF INTCON, INT0IF, ACCESS
    175.     RETURN
    176.  
    177. INT1_ISR
    178.     BTG SEGMENT,0x0,ACCESS
    179.     BTG COLUMN,0x1,ACCESS
    180.     BCF INTCON, RBIF, ACCESS
    181.     BCF INTCON3, INT1IF, ACCESS
    182.     RETURN
    183.  
    184. INT2_ISR
    185.     BTG SEGMENT,0x0,ACCESS
    186.     BTG COLUMN,0x2,ACCESS
    187.     BCF INTCON, RBIF, ACCESS
    188.     BCF INTCON3, INT2IF, ACCESS
    189.     RETURN
    190.  
    191.    
    192. LISR
    193.     BTFSC INTCON, INT0IF, ACCESS
    194.     RCALL INT0_ISR
    195.     BTFSC INTCON3, INT1IF, ACCESS
    196.     RCALL INT1_ISR
    197.     BTFSC INTCON3, INT2IF, ACCESS
    198.     RCALL INT2_ISR
    199.     RETFIE FAST  
    200.    
    201. HISR  
    202.     BTFSC INTCON, TMR0IF, ACCESS
    203.     RCALL TMR0_ISR
    204.  
    205.  
    206.     RETFIE      
    207.    
    208. TMR0_ISR
    209.     BTFSC OPERATOR, 0x0, ACCESS
    210.     DECF OUTPUT,F,ACCESS
    211.     BCF INTCON, TMR0IF, ACCESS
    212.     RETURN
    213.  
    214.    
    215. end                            ;    We always need to have end at the end, even if we don't want the program
    216.  
    217.  
    218.  
    219.  
    220.  
    221.  
    222.  
    223.  
    224.  
    this is the schematic I use in my dev board
    upload_2016-10-5_14-11-19.png
     
Loading...