caramelwilly
New Member
Hi all.
I have a work to do with 8051 microcontroller and I need help because I coded something and I don't know what is wrong. I just started to learn this language and this is very hard.
I put in green what I've done. I know my "MOVX" is wrong and I was thinking about a "MOVC". And I know that R6 should be used but I don't know how.
Thank for your help!
Task description:
; Search for the smallest element in a number sequence (array) stored in the internal memory.
; Every element is an 8 bit unsigned integer.
; Inputs: Start address of the array (pointer), number of elements
; Output: Smallest element value in a register
; -------------------------------------------------------------------
; Definitions
; -------------------------------------------------------------------
; Address symbols for creating pointers
ARRAY_LEN EQU 16
ARRAY_ADDR_IRAM EQU 0x40
; Test data for input parameters
; (Try also other values while testing your code.)
; Store numbers (bytes) in the code memory as an array
ORG 0x0070 ; Move if more code memory is required for the program code
ARRAY_ADDR_CODE:
DB 0x42, 0x1A, 0x7F, 0x80, 0x55, 0xAA, 0xA0, 0xCC, 0x12, 0x13, 0x11, 0x10, 0x05, 0xAA, 0x42, 0x34
; Interrupt jump table
ORG 0x0000;
SJMP MAIN ; Reset vector
; Beginning of the user program, move it freely if needed
ORG 0x0010
; -------------------------------------------------------------------
; MAIN program
; -------------------------------------------------------------------
; Purpose: Prepare the inputs and call the subroutines
; -------------------------------------------------------------------
MAIN:
; Prepare input parameters for the subroutine
MOV DPTR,#ARRAY_ADDR_CODE
MOV R6,#ARRAY_ADDR_IRAM
MOV R7,#ARRAY_LEN
CALL CODE2IRAM ; Copy the array from code memory to internal memory
MOV R6, #ARRAY_ADDR_IRAM
MOV R7, #ARRAY_LEN
; Infinite loop: Call the subroutine repeatedly
LOOP:
CALL FIND_MIN_NUMBER ; Call Find min number subroutine
SJMP LOOP
; ===================================================================
; SUBROUTINE(S)
; ===================================================================
; -------------------------------------------------------------------
; CODE2IRAM
; -------------------------------------------------------------------
; Purpose: Copy the number array from code memory to internal memory
; -------------------------------------------------------------------
; INPUT(S):
; DPTR - Base address of the number array in code memory
; R6 - Base address of the number array in the internal memory
; R7 - Number array size (in bytes)
; OUTPUT(S):
; -
; MODIFIES:
; [TODO]
; -------------------------------------------------------------------
CODE2IRAM:
MOV A,R6
MOVC A,@A+DPTR ;A->ext destn
INC R6 ;Source increment
INC DPTR ;destination increment
DJNZ R7
RET
; -------------------------------------------------------------------
; FIND_MIN_NUMBER
; -------------------------------------------------------------------
; Purpose: Find the minimum number (byte) in the array
; -------------------------------------------------------------------
; INPUT(S):
; R6 - Base address of the number array in the internal memory
; R7 - Array size (in bytes)
; OUTPUT(S):
; R4 - Minimum element value
; MODIFIES:
; [TODO]
; -------------------------------------------------------------------
FIND_MIN_NUMBER:
MOV DPTR, #2400 ;set DPTR as pointer array
MOVX A,@DPTR
MOV R0, A ; load the count value into a R0 register
DEC R0 ;it means that if we have 6 data, we have 5 comparisons to do (6-1) thats why we decrease by 1
INC DPTR ;we go to the first element of the array
MOVX A, @DPTR ;we load the first data into R7
MOV R4, A ;the first element is the smallest and we wave it into R4
AGAIN :
INC DPTR ;make DPTR to point next element of array
MOVX A, @DPTR ;get next element of array in A
MOV R2, A ;save in R2
CLR C ;clear carry flag
SUBB A, R4 ;Subtract current smallest from A
JNC AHEAD ;check for carry, if carry is set
MOV A, R2 ;save content of R2 as current smallest
MOV R4, A ;copy the smallest value into R4
AHEAD:
DJNZ R0, AGAIN ;Decrement count and go again if count is not zero
;otherwise go to next instruction
MOV DPTR, #2500h ;load the adress of result in DPTR
MOV A, R4 ; move the smallest data to A
MOVX @DPTR, A ;save in external memory
HALT:
SJMP HALT ;remain Idle in infinity loop
RET
; [TODO: You can also create other subroutines if needed.]
; End of the source file
END
I have a work to do with 8051 microcontroller and I need help because I coded something and I don't know what is wrong. I just started to learn this language and this is very hard.
I put in green what I've done. I know my "MOVX" is wrong and I was thinking about a "MOVC". And I know that R6 should be used but I don't know how.
Thank for your help!
Task description:
; Search for the smallest element in a number sequence (array) stored in the internal memory.
; Every element is an 8 bit unsigned integer.
; Inputs: Start address of the array (pointer), number of elements
; Output: Smallest element value in a register
; -------------------------------------------------------------------
; Definitions
; -------------------------------------------------------------------
; Address symbols for creating pointers
ARRAY_LEN EQU 16
ARRAY_ADDR_IRAM EQU 0x40
; Test data for input parameters
; (Try also other values while testing your code.)
; Store numbers (bytes) in the code memory as an array
ORG 0x0070 ; Move if more code memory is required for the program code
ARRAY_ADDR_CODE:
DB 0x42, 0x1A, 0x7F, 0x80, 0x55, 0xAA, 0xA0, 0xCC, 0x12, 0x13, 0x11, 0x10, 0x05, 0xAA, 0x42, 0x34
; Interrupt jump table
ORG 0x0000;
SJMP MAIN ; Reset vector
; Beginning of the user program, move it freely if needed
ORG 0x0010
; -------------------------------------------------------------------
; MAIN program
; -------------------------------------------------------------------
; Purpose: Prepare the inputs and call the subroutines
; -------------------------------------------------------------------
MAIN:
; Prepare input parameters for the subroutine
MOV DPTR,#ARRAY_ADDR_CODE
MOV R6,#ARRAY_ADDR_IRAM
MOV R7,#ARRAY_LEN
CALL CODE2IRAM ; Copy the array from code memory to internal memory
MOV R6, #ARRAY_ADDR_IRAM
MOV R7, #ARRAY_LEN
; Infinite loop: Call the subroutine repeatedly
LOOP:
CALL FIND_MIN_NUMBER ; Call Find min number subroutine
SJMP LOOP
; ===================================================================
; SUBROUTINE(S)
; ===================================================================
; -------------------------------------------------------------------
; CODE2IRAM
; -------------------------------------------------------------------
; Purpose: Copy the number array from code memory to internal memory
; -------------------------------------------------------------------
; INPUT(S):
; DPTR - Base address of the number array in code memory
; R6 - Base address of the number array in the internal memory
; R7 - Number array size (in bytes)
; OUTPUT(S):
; -
; MODIFIES:
; [TODO]
; -------------------------------------------------------------------
CODE2IRAM:
MOV A,R6
MOVC A,@A+DPTR ;A->ext destn
INC R6 ;Source increment
INC DPTR ;destination increment
DJNZ R7
RET
; -------------------------------------------------------------------
; FIND_MIN_NUMBER
; -------------------------------------------------------------------
; Purpose: Find the minimum number (byte) in the array
; -------------------------------------------------------------------
; INPUT(S):
; R6 - Base address of the number array in the internal memory
; R7 - Array size (in bytes)
; OUTPUT(S):
; R4 - Minimum element value
; MODIFIES:
; [TODO]
; -------------------------------------------------------------------
FIND_MIN_NUMBER:
MOV DPTR, #2400 ;set DPTR as pointer array
MOVX A,@DPTR
MOV R0, A ; load the count value into a R0 register
DEC R0 ;it means that if we have 6 data, we have 5 comparisons to do (6-1) thats why we decrease by 1
INC DPTR ;we go to the first element of the array
MOVX A, @DPTR ;we load the first data into R7
MOV R4, A ;the first element is the smallest and we wave it into R4
AGAIN :
INC DPTR ;make DPTR to point next element of array
MOVX A, @DPTR ;get next element of array in A
MOV R2, A ;save in R2
CLR C ;clear carry flag
SUBB A, R4 ;Subtract current smallest from A
JNC AHEAD ;check for carry, if carry is set
MOV A, R2 ;save content of R2 as current smallest
MOV R4, A ;copy the smallest value into R4
AHEAD:
DJNZ R0, AGAIN ;Decrement count and go again if count is not zero
;otherwise go to next instruction
MOV DPTR, #2500h ;load the adress of result in DPTR
MOV A, R4 ; move the smallest data to A
MOVX @DPTR, A ;save in external memory
HALT:
SJMP HALT ;remain Idle in infinity loop
RET
; [TODO: You can also create other subroutines if needed.]
; End of the source file
END