Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/29/03 09:35
Read: times


 
#57434 - RE: Need Help reading DIP switch
Responding to: ???'s previous message
Hi,
it depends on what do you need - more speed, reduce code size, safe execution, etc...
Could I add some more comments now:

1) a result of #00001011 would indicate that the first, third, and fourth switches are on No, it is wrong, because:
- If unused pins of P2 are not connected to GND (no reason I see for) then you will read 1s at the upper nibble of P2. So, it should be "a result of #11111011 would..."
- At ON state, DIP switch connects corresponded two pins together. Usualy, all the pins from one DIP switch side are common connected to the ground and pins from the other side are controled with port. So, when position is ON then it means that you will read the zero.
Okay, now it should be: a result of #11110100 would indicate that the first, third, and fourth switches are on.

2) There are a lot of ways to make jumps from selected conditions. Here are some ones I use time to time:
; based on DJNZ - it is simplest
SUBROUTINE_DIP:
	MOV	A,P2
	ANL	A,#00001111b
	JNZ	LABEL_1
; if we are here then value 0 has been read
	RET
LABEL_1: DJNZ	ACC,LABEL_2
; if we are here then value 1 has been read
	RET
LABEL_2: DJNZ	ACC,LABEL_3
; if we are here then value 2 has been read
	RET
; LABEL3...LABEL13 are by same way as above
LABEL_14: DJNZ	ACC,LABEL_15
; if we are here then value 14 has been read
	RET
LABEL_15:
; if we are here then value 15 has been read
	RET
Classic scheme:
; based on JMP @A+DPTR
SUBROUTINE_DIP:
	MOV	A,P2
	ANL	A,#00001111b
	MOV	DPL,A
	ADD	A,ACC
	ADD	A,DPL
	MOV	DPTR,#LABELS_VECTORS
	JMP	@A+DPTR
LABELS_VECTORS:
	LJMP	LABEL_0
	LJMP	LABEL_1
;	...
	LJMP	LABEL_15
You may use SJMP instead LJMP if your LABEL_ subroutines are short. In this case, replace LJMP with SJMP and eject MOV DPL,A and ADD A,DPL as well. Btw, DO NOT USE mnemonic "JMP" because compiler may replace it with SJMP/AJMP/LJMP which have different code length (2/3 bytes).

Now, if for some reason you do not wish to use DPTR then there is yet another way, named "call via stack":
; based on stack 
SUBROUTINE_DIP:
	MOV	A,P2
	ANL	A,#00001111b
	ADD	A,ACC
	ADD	A,#(MARK3-MARK2)
	MOV	R0,A
	ADD	A,#(MARK2-MARK1+1)
	MOVC	A,@A+PC		; low byte of address
MARK1:
	PUSH	ACC
	MOV	A,R0
	MOVC	A,@A+PC		; high byte of address
MARK2:
	PUSH	ACC
	RET
MARK3:
	DW	LABEL_0
	DW	LABEL_1
; ...
	DW	LABEL_15
I can suggest such way inside ISR when you need to keep DPTR value untouched.

3) To make your code more safe - read DIP switch twice at least:
SUBROUTINE_DIP:
	MOV	A,P2
	CJNE	A,P2,SUBROUTINE_DIP	; repeat if not the same
Good days!


List of 13 messages in thread
TopicAuthorDate
Need Help reading DIP switch            01/01/70 00:00      
   JMP @A+DPTR instruction could help            01/01/70 00:00      
      RE: JMP @A+DPTR instruction could help            01/01/70 00:00      
         one more way of doing it            01/01/70 00:00      
      RE: JMP @A+DPTR instruction could help            01/01/70 00:00      
   RE: Need Help reading DIP switch            01/01/70 00:00      
      RE: Need Help reading DIP switch            01/01/70 00:00      
      RE: Need Help reading DIP switch            01/01/70 00:00      
         RE: Need Help reading DIP switch            01/01/70 00:00      
         RE: Need Help reading DIP switch            01/01/70 00:00      
            RE: Need Help reading DIP switch            01/01/70 00:00      
            RE: Need Help reading DIP switch            01/01/70 00:00      
   RE: Need Help reading DIP switch            01/01/70 00:00      

Back to Subject List