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.

Mplab 8.1

Status
Not open for further replies.

Mr.K

New Member
How do I convert this code to work for MPLAB 8.1? props to bebe

Code:
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

 

 

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
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

  end
 
Last edited:
would you mind converting the code?
There's no conversion necessary. It assembles just fine (aside from some minor formatting errors). Read MPLAB's help to learn how to create a project, add that source file to your project and assemble. No problem. :D

Here it is cleaned up so no errors:
Code:
        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,tmp2               ;temporary storage
        key                     ;which key was pressed
        rows                    ;counter for number of rows
        code1,code2,code3,code4 ;registers for secret code
        key1,key2,key3,key4     ;registers for keyed attempts
        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

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
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

        end
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top