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.

Hardware Stack Overflow + need help!

Status
Not open for further replies.

Yagami5159

New Member
Hi, good evening, well there's not much to say, I'm currently have a side project of making a combination lock using 3*4 keypad for inputs and 4 seven segment display as outputs (by using PIC 16F877A) under assembly language.

Well I looked through the keypad tutorials, I've decided to try the basics to make a keypad to accept 4 digits input and it'll show ON at the 7segment display as correct and OFF as incorrect:

Code:
; Keyscan.asm 
; Reads keypad and returns value in W reg 
  INCLUDE P16F877A.INC 
  __CONFIG _XT_OSC & _WDT_OFF & _LVP_OFF
key   EQU 0x20      ;Count for keys
rows  EQU 0x21         
CODE1 EQU 0x22            ;code1
CODE2 EQU 0x23           ;code2
CODE3 EQU 0x24            ;code3
CODE4 EQU 0x25            ;code4
KEY1  EQU 0x26            ;register for keyed attempts
KEY2  EQU 0x27
KEY3  EQU 0x28
KEY4  EQU 0X29

_ON   RETLW    B'00011101'    ;o
     MOVWF    PORTB
     RETLW    B'00010101'    ;n
     MOVWF    PORTC

_OFF  RETLW    B'00011101'    ;o
     MOVWF    PORTA
     RETLW    B'01000111'    ;f
     MOVWF    PORTB
     RETLW    B'01000111'    ;f
     MOVWF    PORTB

Initialise	movlw	D'2'  ;set 4 digit secret code
     movwf	CODE1
 	movlw	D'5'
 	movwf	CODE2
 	movlw	D'8'
 	movwf	CODE3
 	movlw	D'0'
 	movwf	CODE4

;SETPORTS 
     BANKSEL  TRISD          ; 
     MOVLW    B'00001111'    ;       Set RD0-RD3 as inputs for Rows 1,2,3,4 
     MOVWF    TRISD          ;       and RD4-RD7 as outputs for Columns 1,2,3 
     BANKSEL  PORTD 
     CLRF     PORTA          ;clear 7-segment porta
     BANKSEL  TRISA 
  CLRW        ;       Set PORTA as outputs 
  MOVWF    TRISA 
  BANKSEL  PORTA  	
  CLRF     PORTA
     CLRF     PORTB          ;clear 7-segment portb
     BANKSEL  TRISB 
  CLRW        ;       Set PORTB as outputs 
  MOVWF    TRISB 
  BANKSEL  PORTB  	
  CLRF     PORTB
     CLRF     PORTC          ;clear 7-segment portc 
  BANKSEL  TRISC 
  CLRW        ;       Set PORTC as outputs 
  MOVWF    TRISC 
  BANKSEL  PORTC  	
  CLRF     PORTC 
     GOTO     main 

Chk_Code	movf	CODE1, w	;test first digit
 	subwf	KEY1, w
 	btfss	STATUS, Z
 	goto	_OFF
 	movf	CODE2, w	;test second digit
 	subwf	KEY2, w
 	btfss	STATUS, Z
 	goto	_OFF
 	movf	CODE3, w	;test third digit
 	subwf	KEY3, w
 	btfss	STATUS, Z
 	goto	_OFF
 	movf	CODE4, w	;test fourth digit
 	subwf	KEY4, w
 	btfss	STATUS, Z
 	goto	_OFF
 	goto	_ON
          
; Read keypad 
main  CALL     scan
     MOVWF    KEY1
     CALL     scan
     MOVWF    KEY2
     CALL     scan
     MOVWF    KEY3
     CALL     scan
     MOVWF    KEY4
     CALL     Chk_Code

      ;GOTO     main; Repeat 
scan  	BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       RETLW       D'1'          ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       RETLW       D'4'          ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       RETLW       D'7'          ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       RETLW       D'10'         ;        Key = Clear
     	
       BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 RETLW       D'2'          ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	RETLW       D'5'          ;        Key = 5
 BTFSC    PORTD,2       ;        Test and skip if Row 3 = 0
       RETLW       D'8'          ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       RETLW       D'0'          ;        Key = 0 
     	
 BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	RETLW       D'3'          ;        Key = 3
 BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	RETLW       D'6'          ;        Key = 6
 BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	RETLW       D'9'          ;        Key = 9
 BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       RETLW       D'12'         ;        Key = #/Enter 12      
       END

But when I ran the PIC simulator IDE, I've got an error saying "Hardware Stack Overflow", What did I do wrong?
 
hi Yagi and welcome.

The program Header is incomplete, no origin statements.

If you complete the Header repost that section.

I get a Hardware Stack underflow error, which means the Call and Return do not balance, more Returns than Call's.


EDIT:
These changes get you past the STACK error, but the program terminates in scan
Code:
; Keyscan.asm  
; Reads keypad and returns value in W reg  


	list		p=16f877A	; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	errorlevel -302, -207
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF 
	

key	equ	0x20		;Count for keys
rows	equ	0x21         
CODE1	equ	0x22		;code1
CODE2	equ	0x23		;code2
CODE3	equ	0x24		;code3
CODE4	equ	0x25		;code4
KEY1	equ	0x26		;register for keyed attempts
KEY2	equ	0x27
KEY3	equ	0x28
KEY4	equ	0X29


	org 0x0000
	goto 	start
	org 0x0004
	nop
	retfie	
		 
_ON	retlw	B'00011101'	;o
	movwf	PORTB
	retlw	B'00010101'	;n
	movwf	PORTC
		 
_OFF	retlw	B'00011101'	;o
	movwf	PORTA
	retlw	B'01000111'	;f
	movwf	PORTB
	retlw	B'01000111'	;f
	movwf	PORTB

start:		 
Initialise 
	movlw D'2'		;set 4 digit secret code
	movwf	CODE1
 
Last edited:
Thank you for replying , well I followed your instruction and this is what I managed to do :
Code:
; Keyscan.asm  
; Reads keypad and returns value in W reg  


list  p=16f877A; list directive to define processor
#include	<p16f877A.inc>; processor specific variable definitions
errorlevel -302, -207
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF 


key  equ	0x20;Count for keys
rows	equ	0x21         
CODE1	equ	0x22;code1
CODE2	equ	0x23;code2
CODE3	equ	0x24;code3
CODE4	equ	0x25;code4
KEY1	equ	0x26;register for keyed attempts
KEY2	equ	0x27
KEY3	equ	0x28
KEY4	equ	0X29


org 0x0000
goto  start
org 0x0004
nop
retfie	

_ON	
retlw	B'00011101';o
movwf	PORTB
retlw	B'00010101';n
movwf	PORTC

_OFF	
retlw	B'00011101';o
movwf	PORTA
retlw	B'01000111';f
movwf	PORTB
retlw	B'01000111';f
movwf	PORTC

start:   
Initialise 
movlw  D'2';set 4 digit secret code
movwf	CODE1
movlw	D'5'
 movwf	CODE2
 movlw	D'8'
 movwf	CODE3
 movlw	D'0'
 movwf	CODE4

;SETPORTS 
    BANKSEL     TRISD        ; 
    MOVLW      B'00001111'  ;       Set RD0-RD3 as inputs for Rows 1,2,3,4 
    MOVWF       TRISD        ;       and RD4-RD7 as outputs for Columns 1,2,3 
    BANKSEL     PORTD 
    CLRF        PORTA        ;   clear 7-segment porta
    BANKSEL     TRISA 
   CLRW            ;       Set PORTA as outputs 
  MOVWF       TRISA 
  BANKSEL      PORTA  	
 CLRF        PORTA
  CLRF        PORTB      ;   clear 7-segment portb
  BANKSEL     TRISB 
  CLRW            ;       Set PORTB as outputs 
  MOVWF       TRISB 
  BANKSEL     PORTB  	
  CLRF        PORTB
    CLRF        PORTC      ;   clear 7-segment portc 
  BANKSEL     TRISC 
  CLRW            ;       Set PORTC as outputs 
  MOVWF       TRISC  
 BANKSEL     PORTC  	
  CLRF        PORTC 
    GOTO        main 

Chk_Code	
 movf    CODE1, w;test first digit
  subwf    KEY1, w
  btfss    STATUS, Z
  goto    _OFF
  movf    CODE2, w;test second digit
  subwf    KEY2, w
  btfss    STATUS, Z
  goto    _OFF
  movf    CODE3, w;test third digit
  subwf    KEY3, w
  btfss    STATUS, Z
  goto    _OFF
  movf    CODE4, w;test fourth digit
  subwf    KEY4, w
  btfss    STATUS, Z
  goto    _OFF
  goto       _ON
 GOTO     main

; Read keypad 
main  
 CALL     scan
    MOVWF    KEY1
    CALL     Chk_Code
    CALL     scan
    MOVWF    KEY2
    CALL     Chk_Code
    CALL     scan
    MOVWF    KEY3
    CALL     Chk_Code
    CALL     scan
    MOVWF    KEY4
    CALL     Chk_Code
 GOTO    main

   ;GOTO     main; Repeat 
scan  	
 BSF      	PORTD,4      ;        Column 1 = 1 
     BCF      	PORTD,5      ;        Column 2 = 0 
     BCF      	PORTD,6      ;        Column 3 = 0 
     BTFSC    	PORTD,0      ;        Test and skip if Row 1 = 0
       RETLW       D'1'        ;        Key = 1
     BTFSC    	PORTD,1      ;        Test and skip if Row 2 = 0
       RETLW       D'4'        ;        Key = 4 
     BTFSC    	PORTD,2      ;        Test and skip if Row 3 = 0
       RETLW       D'7'        ;        Key = 7 
     BTFSC    	PORTD,3      ;        Test and skip if Row 4 = 0
       RETLW       D'10'        ;        Key = Clear

       BCF      	PORTD,4      ;        Column 1 = 0 
     BSF      	PORTD,5      ;        Column 2 = 1 
     BTFSC    	PORTD,0      ;        Test and skip if Row 1 = 0
 	RETLW       D'2'        ;        Key = 2 
     BTFSC    	PORTD,1      ;        Test and skip if Row 2 = 0 
     RETLW       D'5'        ;        Key = 5
 	BTFSC    	PORTD,2      ;        Test and skip if Row 3 = 0
       RETLW       D'8'        ;        Key = 8 
     BTFSC    	PORTD,3      ;        Test and skip if Row 4 = 0
       RETLW       D'0'        ;        Key = 0 

 	BCF      	PORTD,5      ;        Column 2 = 0 
     BSF      	PORTD,6      ;        Column 3 = 1 
     BTFSC    	PORTD,0      ;        Test and skip if Row 1 = 0 
     RETLW       D'3'        ;        Key = 3
 	BTFSC    	PORTD,1      ;        Test and skip if Row 2 = 0
     RETLW       D'6'        ;        Key = 6
 	BTFSC    	PORTD,2      ;        Test and skip if Row 3 = 0 
     RETLW       D'9'        ;        Key = 9
 	BTFSC    	PORTD,3      ;        Test and skip if Row 4 = 0
       RETLW       D'12'        ;        Key = #/Enter 12      
 return
      END
As you can see, the Hardware Stack Overflow are stop from poping out from my simulator. I set port D as my keyboard and Port A, B and C as my 7 segment display to display either ON or OFF, but there's a little problem...when I ran the simulator rate by "Step-by-step", it just froze there. If I tried running with rate "Extremely Fast" it will go through the steps but it doesn't recognize my inputs, either there's a mistake or the codes were completely a mess. :(
 
hi yagi,
Your new program runs OK.

Have you used the Oshonsoft 'Keypad matrix' TOOL connected to your PORTD.??

E.

EDIT:

If I set up the Keypad TOOL it works for me, it stops on a key press.
 
Last edited:
hi,
You say in your program comments that.
bsf PORTD,4 ; Column 1 = 1
bcf PORTD,5 ; Column 2 = 0
bcf PORTD,6 ; Column 3 = 0

These are the Column drivers, you have the matrix the other 'way' around... look at my key matrix image
 
Hi and thanks again for your wonderful advice!
Interesting...the correct code should be 2580 instead of 147*,
and the seven segments are not showing ON and OFF as well.:confused:
I'd recheck the All Ports A, B and C and set those ports as "Always Enable", but it's still not working.:confused::confused:
 
Hi and thanks again for your wonderful advice!
Interesting...the correct code should be 2580 instead of 147*,
and the seven segments are not showing ON and OFF as well.:confused:
I'd recheck the All Ports A, B and C and set those ports as "Always Enable", but it's still not working.:confused::confused:

OK,
when you are ready for a re-run, post the program I will try it out.;)

Ensure that PORTA is set for digital use.
 
Hello and Good Evening, after some time modifying the codes I had decided, instead using Banks to store my input, why not making it a keypad per input.
Means every correct code entered, the program will transfer to the next correct keypad. As for every wrong code, the program will still transfer to next wrong keypad.
So that means it'll only show the results after 4 inputs, which is On or Off.
Beside that, every code selected must press # to confirm selection, in order to prevent next keypad to read same input while the program is running.
So here's my question, after you get your results, you can press # again to reset and enter the codes again, but how? CLRW?
here's the code:
Code:
	list		p=16f877A		; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	errorlevel -302, -207
;	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF 
	__CONFIG _XT_OSC & _WDT_OFF & _LVP_OFF
 
 
key		equ	0x20				 ;		Count for keys
rows	equ	0x21         

;SETPORTS 
     BANKSEL  	  TRISD          ; 
     MOVLW   	  B'00001111'    ;       Set RD0-RD3 as inputs for Rows 1,2,3,4 
     MOVWF    	  TRISD          ;       and RD4-RD7 as outputs for Columns 1,2,3 
     BANKSEL  	  PORTD 
	 CLRW
	 MOVWF		  PORTD

     BANKSEL  	  TRISA 
  	 CLRW       			 	 ;       Set PORTA as outputs 
 	 MOVWF    	  TRISA 
 	 BANKSEL      PORTA  	

 	 BANKSEL  	  TRISB 
 	 CLRW        			 	 ;       Set PORTB as outputs 
 	 MOVWF    	  TRISB 
 	 BANKSEL  	  PORTB  	

 	 BANKSEL  	  TRISC 
 	 CLRW        			 	 ;       Set PORTC as outputs 
 	 MOVWF    	  TRISC 	
	 BANKSEL  	  PORTC  	

     GOTO     	  main 
 
;Read keypad 
main  
	 	CALL     	scan		  ;		   GOTO scan
	 	GOTO	  	main
 
 
scan  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO  	    waitingoff    ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO  	    waitingoff    ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff    ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff    ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO       	waiting       ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO  	    waitingoff    ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff    ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff    ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO  	    waitingoff    ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO  	    waitingoff    ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO  	    waitingoff    ;        Key = 9
 		BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff    ;        Key = #/Enter 12      
		GOTO		scan

scan2  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO  	    waitingoff2   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO  	    waitingoff2   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff2   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff2   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO  	    waitingoff2   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO       	waiting2      ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff2   ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff2   ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO  	    waitingoff2   ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO  	    waitingoff2   ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO  	    waitingoff2   ;        Key = 9
		GOTO		scan2     

scan3  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO  	    waitingoff3   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO  	    waitingoff3   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff3   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff3   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO  	    waitingoff3   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO  	    waitingoff3   ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO       	waiting3      ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
        GOTO  	    waitingoff3   ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO  	    waitingoff3   ; 	   Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO  	    waitingoff3   ; 	   Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO  	    waitingoff3   ; 	   Key = 9
		GOTO		scan3	  
	

scan4  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO		waitingoff4   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO		waitingoff4   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO		waitingoff4   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO		waitingoff4   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO		waitingoff4   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO		waitingoff4   ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO		waitingoff4   ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO		waiting4      ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO		waitingoff4   ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO		waitingoff4   ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO		waitingoff4   ;        Key = 9
		GOTO		scan4

offscan2  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO  	    waitingoff2   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO  	    waitingoff2   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff2   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff2   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO  	    waitingoff2   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO       	waitingoff2   ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff2   ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff2   ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO  	    waitingoff2   ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO  	    waitingoff2   ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO  	    waitingoff2   ;        Key = 9
		GOTO		offscan2     

offscan3  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO  	    waitingoff3   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO  	    waitingoff3   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO  	    waitingoff3   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO  	    waitingoff3   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO  	    waitingoff3   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO  	    waitingoff3   ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO       	waitingoff3   ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
        GOTO  	    waitingoff3   ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO  	    waitingoff3   ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO  	    waitingoff3   ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO  	    waitingoff3   ;        Key = 9
		GOTO		offscan3	  
	

offscan4  	
		BSF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BCF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
       	GOTO		waitingoff4   ;        Key = 1
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
       	GOTO		waitingoff4   ;        Key = 4 
     	BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO		waitingoff4   ;        Key = 7 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO		waitingoff4   ;        Key = Clear
 
       	BCF      	PORTD,4       ;        Column 1 = 0 
     	BSF      	PORTD,5       ;        Column 2 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0
 		GOTO		waitingoff4   ;        Key = 2 
     	BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0 
     	GOTO		waitingoff4   ;        Key = 5
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0
       	GOTO		waitingoff4   ;        Key = 8 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 4 = 0
       	GOTO		waitingoff4   ;        Key = 0 
 
 		BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 1 
     	BTFSC    	PORTD,0       ;        Test and skip if Row 1 = 0 
     	GOTO		waitingoff4   ;        Key = 3
 		BTFSC    	PORTD,1       ;        Test and skip if Row 2 = 0
     	GOTO		waitingoff4   ;        Key = 6
 		BTFSC    	PORTD,2       ;        Test and skip if Row 3 = 0 
     	GOTO		waitingoff4   ;        Key = 9
		GOTO		offscan4

waiting  	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    scan2         ;        Key = 1
		GOTO 		waiting


waitingoff 	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    offscan2      ;        Key = 1
		GOTO		waitingoff

waiting2  	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    scan3         ;        Key = 1
		GOTO 		waiting2


waitingoff2 	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    offscan3      ;        Key = 1
		GOTO		waitingoff2

waiting3  	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    scan4         ;        Key = 1
		GOTO 		waiting3


waitingoff3 	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    offscan4      ;        Key = 1
		GOTO		waitingoff3

waiting4  	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    _ON           ;        Key = 1
		GOTO 		waiting4


waitingoff4 	
		BCF      	PORTD,4       ;        Column 1 = 1 
     	BCF      	PORTD,5       ;        Column 2 = 0 
     	BSF      	PORTD,6       ;        Column 3 = 0 
     	BTFSC    	PORTD,3       ;        Test and skip if Row 1 = 0
       	GOTO  	    _OFF      	  ;        Key = 1
		GOTO		waitingoff4

_ON	
	movlw	B'10111000'	;o
	movwf	PORTB
	movlw	B'10101000'	;n
	movwf	PORTC
	goto	_ON
 
_OFF	
	movlw	B'01111010'	;o
	movwf	PORTA
	movlw	B'11100010'	;f
	movwf	PORTB
	movlw	B'11100010'	;f
	movwf	PORTC
	goto	_OFF

       END
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top