Problem with interfacing optical encoder with micro controller

.SYMBOLS

.CHIP 8051

.RSECT


;*******************************************************

; HI-LOW STATUS FLAG

;*******************************************************

LOW_FLAG .REG 21H.0

HI_FLAG .REG 21H.1


;*******************************************************

; 7-SEGMENT LED DISPLAY REGISTERS

;*******************************************************

.ORG 30H

DIGIT1 .DS 1

DIGIT2 .DS 1

DIGIT3 .DS 1

DIGIT4 .DS 1


;*******************************************************

; MAIN PROGRAM

;*******************************************************

.CODE

.ORG 0H

JMP MAIN


.ORG 100H

MAIN: MOV SP, #50H

CALL CONFIG_PORT_OUT

RESET: MOV DIGIT1, #0H

MOV DIGIT2, #0H

CALL DISPLAY_DIGIT2

CALL DISPLAY_DIGIT1


CHK_START: JNB P1.0, CHK_START


AGAIN: CLR LOW_FLAG

CLR HI_FLAG

CALL CONFIG_PORT_IN


READ_LOW: MOV DPTR, #INPORT

MOVX A, @DPTR

ANL A, #40H

JNZ READ_LOW


CALL DEBOUNCE

SETB LOW_FLAG


READ_HI: MOV DPTR, #INPORT

MOVX A, @DPTR

ANL A, #40H

JZ READ_HI


CALL DEBOUNCE

SETB HI_FLAG

JNB LOW_FLAG, READ_LOW

JNB HI_FLAG, READ_HI


CALL CONFIG_PORT_OUT

INC DIGIT1

MOV A, DIGIT1

CJNE A, #0AH, NEXT1

MOV DIGIT1, #0H

MOV A, DIGIT1

NEXT1: CALL DISPLAY_DIGIT1

CJNE A, #0H, AGAIN



INC DIGIT2

MOV A, DIGIT2

CJNE A, #0AH, NEXT2

JMP RESET

NEXT2: CALL DISPLAY_DIGIT2

JMP AGAIN


DISPLAY_DIGIT1: ANL DIGIT1, #0FH

MOV A, DIGIT1

MOV DPTR, #OUTPORT

MOVX @DPTR, A

CALL DELAY

RET


DISPLAY_DIGIT2: MOV A, DIGIT2

ORL A, #10H

MOV DPTR, #OUTPORT

MOVX @DPTR, A

CALL DELAY

RET


DEBOUNCE: MOV R7, #66 ; WAIT FOR 0.1 SEC

DEL_ST1: MOV R6, #255

DEL_ST2: NOP

NOP

NOP

NOP

DJNZ R6, DEL_ST2

DJNZ R7, DEL_ST1

RET


DELAY: MOV R5, #35H

F: MOV R4, #07H

E: MOV R3, #0FFH

D: DJNZ R3, D

DJNZ R4, E

DJNZ R5, F

RET


;************************************************************************

; PORT_CONFIG AND ADDRESS DECLARATION

;************************************************************************

CTRL_REG_PORT0 .EQU 8000H ; CONTROL REG ADDRESS FOR PORT0

INPORT .EQU 0E000H ; INPUT PORT ADDRESS

OUTPORT .EQU 0C010H ; OUTPUT PORT ADDRESS


;************************************************************************************************

; CONFIG INPUT PORTS

;************************************************************************************************

CONFIG_PORT_IN: MOV DPTR, #CTRL_REG_PORT0 ;DPTR POINTING ON THE ADDRESS OF CONTROL PORT0

MOV A, #00H ;LOAD ACC WITH 00H

MOVX @DPTR, A ;MAKE THE PORT0 INTO A INPUT PORT

RET


;************************************************************************************************

; CONFIG OUTPUT PORTS

;************************************************************************************************

CONFIG_PORT_OUT: MOV DPTR, #CTRL_REG_PORT0 ;DPTR POINTING ON THE ADDRESS OF CONTROL PORT0

MOV A, #FFH ;LOAD ACC WITH FFH

MOVX @DPTR, A ;MAKE THE PORT0 INTO A OUTPUT PORT

RET


END




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories