1. 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.
    Dismiss Notice

Calling all programming heads

Discussion in 'Microcontrollers' started by Mr.K, Mar 7, 2009.

  1. Mr.K

    Mr.K New Member

    Joined:
    Feb 12, 2009
    Messages:
    47
    Likes:
    0
    Yes sir you recommend it so I trust your judgement

    Do I need a encoder between the keypad and the chip?
     
    Last edited: Mar 10, 2009
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    No That why I said get a bigger pic
     
  3. Mr.K

    Mr.K New Member

    Joined:
    Feb 12, 2009
    Messages:
    47
    Likes:
    0
    Ok cuz i was told i might need one, so now i will go ahead and order the chip i see ur an ebay seller u selling any?lol

    also how can the program portion be tweaked to achieve want i want to do?
    Code (text):
    LIST p=16F628  
     include "P16F628.inc"
     ERRORLEVEL 0, -302 ;suppress bank selection messages
     __config 0x3D18
     
      cblock 0x20   ;start of general purpose registers
       count   ;used in looping routines
       count1   ;used in delay routine
       counta   ;used in delay routine
       countb   ;used in delay routine
       tmp1   ;temporary storage
       tmp2
       templcd   ;temp store for 4 bit mode
       templcd2
       key   ;which key was pressed
       rows   ;counter for number of rows
       code1   ;registers for secret code
       code2
       code3
       code4
       key1   ;registers for keyed attempts
       key2
       key3
       key4
      endc
    LCD_PORT Equ PORTA
    LCD_TRIS Equ TRISA
    LCD_RS  Equ 0x04   ;LCD handshake lines
    LCD_RW  Equ 0x06
    LCD_E  Equ 0x07
    KEY_PORT Equ PORTB   ;keypad port
    KEY_TRIS Equ TRISB
    Col1  Equ 0   ;pins used for keypad inputs
    Col2  Equ 1
    Col3  Equ 2
    Col4  Equ 3
      org 0x0000
      goto Start
    Key_Table   ADDWF   PCL       , f ;translation table for keypad
                 RETLW   0x31 ;1
                 RETLW   0x34 ;4
                 RETLW   0x37 ;7
                 RETLW   0x2a ;*
                 RETLW   0x32 ;2
                 RETLW   0x35 ;5
                 RETLW   0x38 ;8
                 RETLW   0x30 ;0
                 RETLW   0x33 ;3
                 RETLW   0x36 ;6
                 RETLW   0x39 ;9
                 RETLW   0x23 ;#
                 RETLW   0x43 ;C
                 RETLW   0x44 ;D
                 RETLW   0x45 ;E
                 RETLW   0x46 ;F
     
    HEX_Table   ADDWF   PCL       , f  ;hex table for LCD routines
                 RETLW   0x30
                 RETLW   0x31
                 RETLW   0x32
                 RETLW   0x33
                 RETLW   0x34
                 RETLW   0x35
                 RETLW   0x36
                 RETLW   0x37
                 RETLW   0x38
                 RETLW   0x39
                 RETLW   0x41
                 RETLW   0x42
                 RETLW   0x43
                 RETLW   0x44
                 RETLW   0x45
                 RETLW   0x46
     
    Text  addwf PCL, f
      retlw 'S'
      retlw 'k'
      retlw 'r'
      retlw 'i'
      retlw 'v'
      retlw ' '
      retlw 'k'
      retlw 'o'
      retlw 'd'
      retlw 'e'
      retlw '!'
      retlw '.'
      retlw 0x00
    Wrong_Text addwf PCL, f
      retlw 'F'
      retlw 'o'
      retlw 'r'
      retlw 'k'
      retlw 'e'
      retlw 'r'
      retlw 't'
      retlw ' '
      retlw 'k'
      retlw 'o'
      retlw 'd'
      retlw 'e'
      retlw 0x00
    Correct_Text addwf PCL, f
      retlw 'K'
      retlw 'o'
      retlw 'r'
      retlw 'r'
      retlw 'e'
      retlw 'k'
      retlw 't'
      retlw ' '
      retlw 'k'
      retlw 'o'
      retlw 'd'
      retlw 'e'
      retlw '.'
      retlw 0x00
    Start  movlw 0x07
      movwf CMCON   ;turn comparators off (make it like a 16F84)
    Initialise movlw '0'   ;set 4 digit secret code
      movwf code1
      movlw '0'
      movwf code2
      movlw '0'
      movwf code3
      movlw '0'
      movwf code4
     
    SetPorts bsf  STATUS,  RP0 ;select bank 1
      movlw 0x00   ;make all pins outputs
      movwf LCD_TRIS
      movlw 0x0F   ;set keypad pins
      movwf KEY_TRIS  ;half in, half out
      movwf TRISB
      bcf  STATUS,  RP0 ;select bank 0
    Init  call LCD_Init  ;setup LCD
     
      clrf count   ;set counter register to zero
    Message  movf count, w  ;put counter value in W
      call Text   ;get a character from the text table
      xorlw 0x00   ;is it a zero?
      btfsc STATUS, Z
      goto Main
      call LCD_Char
      incf count, f
      goto Message
    Wrong  call LCD_Clr
      clrf count   ;set counter register to zero
    Message1 movf count, w  ;put counter value in W
      call Wrong_Text  ;get a character from the text table
      xorlw 0x00   ;is it a zero?
      btfsc STATUS, Z
      goto Wrong_Wait
      call LCD_Char
      incf count, f
      goto Message1
    Wrong_Wait call Delay255
      call Delay255
      call Delay255
      call Delay255
      goto Init
    Correct  call LCD_Clr
      clrf count   ;set counter register to zero
    Message2 movf count, w  ;put counter value in W
      call Correct_Text  ;get a character from the text table
      xorlw 0x00   ;is it a zero?
      btfsc STATUS, Z
      goto Correct_Wait
      call LCD_Char
      incf count, f
      goto Message2
    Correct_Wait call Delay255
      call Delay255
      call Delay255
      call Delay255
      goto Init
    Main  movlw d'1'
      call LCD_Line2W  ;move to 2nd row, 2nd column
      call LCD_CurOn
      call Chk_Keys  ;wait for key
      movwf key1   ;store first digit
      call LCD_Char
      call Chk_Keys  ;wait for key
      movwf key2   ;store second digit
      call LCD_Char
      call Chk_Keys  ;wait for key
      movwf key3   ;store third digit
      call LCD_Char
      call Chk_Keys  ;wait for key
      movwf key4   ;store fourth digit
      call LCD_Char
      call LCD_CurOff
     
    Chk_Code movf code1, w  ;test first digit
      subwf key1, w
      btfss STATUS, Z
      goto Wrong
      movf code2, w  ;test second digit
      subwf key2, w
      btfss STATUS, Z
      goto Wrong
      movf code3, w  ;test third digit
      subwf key3, w
      btfss STATUS, Z
      goto Wrong
      movf code4, w  ;test fourth digit
      subwf key4, w
      btfss STATUS, Z
      goto Wrong
      goto Correct
    ;Keypad subroutines
    Chk_Keys movlw 0x00   ;wait until no key pressed
      movwf KEY_PORT  ;set all output pins low
      movf KEY_PORT, W
      andlw 0x0F   ;mask off high byte
      sublw 0x0F
      btfsc STATUS, Z  ;test if any key pressed
      goto Keys   ;if none, read keys
      call Delay20
      goto Chk_Keys  ;else try again
    Keys    call    Scan_Keys
                 movlw   0x10   ;check for no key pressed
                 subwf   key, w
                 btfss   STATUS, Z
                 goto    Key_Found
      call Delay20
      goto Keys
    Key_Found       movf    key, w
      andlw 0x0f
      call Key_Table  ;lookup key in table
      movwf key   ;save back in key
      return    ;key pressed now in W
    Scan_Keys   clrf    key
      movlw 0xF0   ;set all output lines high
                 movwf   KEY_PORT
                 movlw   0x04
                 movwf   rows   ;set number of rows
                 bcf     STATUS, C  ;put a 0 into carry
    Scan    rrf     KEY_PORT, f
                 bsf     STATUS, C  ;follow the zero with ones
    ;comment out next two lines for 4x3 numeric keypad.
                 btfss   KEY_PORT, Col4
                 goto    Press
                 incf    key, f
                 btfss   KEY_PORT, Col3
                 goto    Press
                 incf    key, f
                 btfss   KEY_PORT, Col2
                 goto    Press
                 incf    key, f
                 btfss   KEY_PORT, Col1
                 goto    Press
                 incf    key, f
                 decfsz  rows, f
                 goto    Scan
    Press    return
    ;end of keypad subroutines.
    ;LCD routines
    ;Initialise LCD
    LCD_Init call  LCD_Busy  ;wait for LCD to settle
      movlw 0x20   ;Set 4 bit mode
      call LCD_Cmd
      movlw 0x28   ;Set display shift
      call LCD_Cmd
      movlw 0x06   ;Set display character mode
      call LCD_Cmd
      movlw 0x0c   ;Set display on/off and cursor command
      call LCD_Cmd   ;Set cursor off
      call LCD_Clr   ;clear display
      retlw 0x00
    ; command set routine
    LCD_Cmd  movwf templcd
      swapf templcd, w ;send upper nibble
      andlw 0x0f   ;clear upper 4 bits of W
      movwf LCD_PORT
      bcf LCD_PORT, LCD_RS ;RS line to 1
      call Pulse_e   ;Pulse the E line high
      movf templcd, w ;send lower nibble
      andlw 0x0f   ;clear upper 4 bits of W
      movwf LCD_PORT
      bcf LCD_PORT, LCD_RS ;RS line to 1
      call Pulse_e   ;Pulse the E line high
      call  LCD_Busy
      retlw 0x00
    LCD_CharD addlw 0x30   ;add 0x30 to convert to ASCII
    LCD_Char movwf templcd
      swapf templcd, w ;send upper nibble
      andlw 0x0f   ;clear upper 4 bits of W
      movwf LCD_PORT
      bsf LCD_PORT, LCD_RS ;RS line to 1
      call Pulse_e   ;Pulse the E line high
      movf templcd, w ;send lower nibble
      andlw 0x0f   ;clear upper 4 bits of W
      movwf LCD_PORT
      bsf LCD_PORT, LCD_RS ;RS line to 1
      call Pulse_e   ;Pulse the E line high
      call  LCD_Busy
      retlw 0x00
    LCD_Line1 movlw 0x80   ;move to 1st row, first column
      call LCD_Cmd
      retlw 0x00
    LCD_Line2 movlw 0xc0   ;move to 2nd row, first column
      call LCD_Cmd
      retlw 0x00
    LCD_Line1W addlw 0x80   ;move to 1st row, column W
      call LCD_Cmd
      retlw 0x00
    LCD_Line2W addlw 0xc0   ;move to 2nd row, column W
      call LCD_Cmd
      retlw 0x00
    LCD_CurOn movlw 0x0d   ;Set display on/off and cursor command
      call LCD_Cmd
      retlw 0x00
    LCD_CurOff movlw 0x0c   ;Set display on/off and cursor command
      call LCD_Cmd
      retlw 0x00
    LCD_Clr  movlw 0x01   ;Clear display
      call LCD_Cmd
      retlw 0x00
    LCD_HEX  movwf tmp1
      swapf tmp1, w
      andlw 0x0f
      call HEX_Table
      call LCD_Char
      movf tmp1, w
      andlw 0x0f
      call HEX_Table
      call LCD_Char
      retlw 0x00
    Delay255 movlw 0xff   ;delay 255 mS
      goto d0
    Delay100 movlw d'100'   ;delay 100mS
      goto d0
    Delay50  movlw d'50'   ;delay 50mS
      goto d0
    Delay20  movlw d'20'   ;delay 20mS
      goto d0
    Delay5  movlw 0x05   ;delay 5.000 ms (4 MHz clock)
    d0  movwf count1
    d1  movlw 0xC7   ;delay 1mS
      movwf counta
      movlw 0x01
      movwf countb
    Delay_0
      decfsz counta, f
      goto $+2
      decfsz countb, f
      goto Delay_0
      decfsz count1 ,f
      goto d1
      retlw 0x00
    Pulse_e  bsf LCD_PORT, LCD_E
      nop
      bcf LCD_PORT, LCD_E
      retlw 0x00
    LCD_Busy
      bsf STATUS, RP0  ;set bank 1
      movlw 0x0f   ;set Port for input
      movwf LCD_TRIS
      bcf STATUS, RP0  ;set bank 0
      bcf LCD_PORT, LCD_RS ;set LCD for command mode
      bsf LCD_PORT, LCD_RW ;setup to read busy flag
      bsf LCD_PORT, LCD_E
      swapf LCD_PORT, w  ;read upper nibble (busy flag)
      bcf LCD_PORT, LCD_E  
      movwf templcd2
      bsf LCD_PORT, LCD_E  ;dummy read of lower nibble
      bcf LCD_PORT, LCD_E
      btfsc templcd2, 7  ;check busy flag, high = busy
      goto LCD_Busy  ;if busy check again
      bcf LCD_PORT, LCD_RW
      bsf STATUS, RP0  ;set bank 1
      movlw 0x00   ;set Port for output
      movwf LCD_TRIS
      bcf STATUS, RP0  ;set bank 0
      return
    ;end of LCD routines
     
      end
     
    Last edited: Mar 10, 2009
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn

    Here a start for the key pad it not done need to add what the Keys going to do when pressed
    Code (text):

    LIST p=16F628a  
     include "P16F628a.inc"
     ERRORLEVEL 0, -302 ;suppress bank selection messages
     __config 0x3D18
     
      cblock 0x20   ;start of general purpose registers
       count   ;used in looping routines
       count1   ;used in delay routine
       counta   ;used in delay routine
       countb   ;used in delay routine
       tmp1   ;temporary storage
       tmp2
     
       key   ;which key was pressed
       rows   ;counter for number of rows
       code1   ;registers for secret code
       code2
       code3
       code4
       key1   ;registers for keyed attempts
       key2
       key3
       key4
      endc

    KEY_PORT Equ PORTB   ;keypad port
    KEY_TRIS Equ TRISB
    Col1  Equ 0   ;pins used for keypad inputs
    Col2  Equ 1
    Col3  Equ 2
    Col4  Equ 3
      org 0x0000
      goto Start
    Key_Table   ADDWF   PCL       , f ;translation table for keypad
                 RETLW   0x31 ;1
                 RETLW   0x34 ;4
                 RETLW   0x37 ;7
                 RETLW   0x2a ;*
                 RETLW   0x32 ;2
                 RETLW   0x35 ;5
                 RETLW   0x38 ;8
                 RETLW   0x30 ;0
                 RETLW   0x33 ;3
                 RETLW   0x36 ;6
                 RETLW   0x39 ;9
                 RETLW   0x23 ;#
                 RETLW   0x43 ;C
                 RETLW   0x44 ;D
                 RETLW   0x45 ;E
                 RETLW   0x46 ;F
     

     

    Start  movlw 0x07
      movwf CMCON   ;turn comparators off (make it like a 16F84)
    Initialise movlw '0'   ;set 4 digit secret code
      movwf code1
      movlw '0'
      movwf code2
      movlw '0'
      movwf code3
      movlw '0'
      movwf code4
     
    SetPorts bsf  STATUS,  RP0 ;select bank 1
     
      movlw 0x0F   ;set keypad pins
      movwf KEY_TRIS  ;half in, half out
      movwf TRISB
      bcf  STATUS,  RP0 ;select bank 0




    ;Keypad subroutines
    Chk_Keys movlw 0x00   ;wait until no key pressed
      movwf KEY_PORT  ;set all output pins low
      movf KEY_PORT, W
      andlw 0x0F   ;mask off high byte
      sublw 0x0F
      btfsc STATUS, Z  ;test if any key pressed
      goto Keys   ;if none, read keys
      call Delay20
      goto Chk_Keys  ;else try again
    Keys    call    Scan_Keys
                 movlw   0x10   ;check for no key pressed
                 subwf   key, w
                 btfss   STATUS, Z
                 goto    Key_Found
      call Delay20
      goto Keys
    Key_Found       movf    key, w
      andlw 0x0f
      call Key_Table  ;lookup key in table
      movwf key   ;save back in key
      return    ;key pressed now in W
    Scan_Keys   clrf    key
      movlw 0xF0   ;set all output lines high
                 movwf   KEY_PORT
                 movlw   0x04
                 movwf   rows   ;set number of rows
                 bcf     STATUS, C  ;put a 0 into carry
    Scan    rrf     KEY_PORT, f
                 bsf     STATUS, C  ;follow the zero with ones
                 incf    key, f
                 btfss   KEY_PORT, Col3
                 goto    Press
                 incf    key, f
                 btfss   KEY_PORT, Col2
                 goto    Press
                 incf    key, f
                 btfss   KEY_PORT, Col1
                 goto    Press
                 incf    key, f
                 decfsz  rows, f
                 goto    Scan
    ;end of keypad subroutines.
    Press        return
    Delay255    movlw 0xff   ;delay 255 mS
                goto d0
    Delay100    movlw d'100'   ;delay 100mS
                goto d0
    Delay50     movlw d'50'   ;delay 50mS
                goto d0
    Delay20     movlw d'20'   ;delay 20mS
                goto d0
    Delay5      movlw 0x05   ;delay 5.000 ms (4 MHz clock)
    d0          movwf count1
    d1          movlw 0xC7   ;delay 1mS
                movwf counta
                movlw 0x01
                movwf countb
    Delay_0
                decfsz counta, f
                goto $+2
                decfsz countb, f
                goto Delay_0
                decfsz count1 ,f
                goto d1
                retlw 0x00


      end
     
     
    Last edited: Mar 10, 2009
  6. Mr.K

    Mr.K New Member

    Joined:
    Feb 12, 2009
    Messages:
    47
    Likes:
    0
    allllright im going to try to play with it. Do I add what the Keys are going to do when pressed with the keypad subroutine?
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    That would be a start even if it don't work it will help to see what you want to do pressing “2” Increasing brightness pressing 8 decreasing brightness
     
    Last edited: Mar 10, 2009
  8. Mr.K

    Mr.K New Member

    Joined:
    Feb 12, 2009
    Messages:
    47
    Likes:
    0
    Also do I need this part of the code since the telephone keypad has no letter buttons
    Code (text):

    RETLW   0x43 ;C
    RETLW   0x44 ;D
    RETLW   0x45 ;E
    RETLW   0x46 ;F
     
    Can I remove that?
     
    Last edited: Mar 10, 2009
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    You can don't need it if your going to use a telephone keypad
     
  10. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    Firstly you have create a sub-routine called Keyscan that looks at all the keys and if it finds a ket, the key value is placed in a file such as 22h and a flag is set that says a key has been pressed. This flag is looked at the beginning of the scan routine. The next time the scan routine is run (only a few milliseconds later) it is looking for NO KEY pressed.
    This is called debounce.
    No resistors are needed on the keypad, just turn on the internal 47k pull-up resistors.

    Then create an answer for the keyboard. You can simply increment a register (50 bytes of program at least) while you are scanning or do some fancy Boolean work (about 11 bytes of program).
    I prefer to do simple things as I am a teacher and students will get completely lost unless it is absolutely understandable. Output a register that has been incremented and this will identify each key.
    That's the KeyScan finished. More needed later.
     
    Last edited: Mar 10, 2009
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Wouldn't that be this part
    Code (text):
    ;Keypad subroutines
    Chk_Keys movlw 0x00   ;wait until no key pressed
      movwf KEY_PORT  ;set all output pins low
      movf KEY_PORT, W
      andlw 0x0F   ;mask off high byte
      sublw 0x0F
      btfsc STATUS, Z  ;test if any key pressed
      goto Keys   ;if none, read keys
      call Delay20
      goto Chk_Keys  ;else try again
    Keys    call    Scan_Keys
                 movlw   0x10   ;check for no key pressed
                 subwf   key, w
                 btfss   STATUS, Z
                 goto    Key_Found
      call Delay20
      goto Keys
     
     
  12. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    Producing vague code like that above is all "very well". But you need to have the cirucit diagram with the pins identified and the registers allocated, to be able to produce real instructions. You don't really need any delays as the "flags" create the debounce.
    The sub-routine must be called on a regular basis and take up no time. You must also work out how you are going to generate a key value. Obviously input a byte and check three of four bits for a value. After each bit is checked, a register is incremented.

    I can't do any more until the circuit is generated.
     
    Last edited: Mar 11, 2009
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Last edited: Mar 11, 2009
  14. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    Identify the switches. Add a 10th sw. Remove the resistors. Allocate files.
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    colins55 You have a good website I liked all the circuit's you have on it
     
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Now I know Bill and and I know you can short the output when scanning more them 1 button at a time . It is to easy to turn off a pullups. So she will stay as is.
     
  17. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    How does a short occur when you are scanning? You must be scanning incorrectly.
    All that happens when 2 keys are pressed is this: The program picks up the first key in the sequence.
     
  18. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    Are you having a bad day or what. You just said don't use pullups. Set the pins to use
    Then you say
    now it is a lot easier to use a pullups then to turn it on internally.
     
  19. colin55

    colin55 Well-Known Member

    Joined:
    Feb 14, 2009
    Messages:
    3,534
    Likes:
    82
    Location:
    Melbourne Australia
    I meant external pull-ups.
     
  20. Mr.K

    Mr.K New Member

    Joined:
    Feb 12, 2009
    Messages:
    47
    Likes:
    0
    :eek: now im even more confused than before:confused:
     
  21. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    I posted the wrong 1 here the right 1 I didn't put pullups in it at first and when I posted it here what you need to go with but I still need to added 1 more row of keys.
     

    Attached Files:

    Last edited: Mar 11, 2009

Share This Page