8051

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
After dividing number by 10, how to check whether quotient lies between 10 and 20. May i use CMP command for comparing two numbers by carry in 8051?
 

absf

Joined Dec 29, 2010
1,968
You may use "CJNE A,#nn,Label" . This instruction would compare Acc with #nn.

If Acc <> #nn, it would make the jump to Label.
On Label,
If Acc > #nn, Carry is cleared then it can be tested with JNC to the next instruction.
If Acc < #nn, then Carry is set and it can be tested with JC to do the next job.

Code:
    MOV    A,#105
    CJNE    A,#100,CHECK
EQUAL:    MOV    R0,A    ;ACC = 100
    AJMP    EXIT
CHECK:    JC    SMALLER
GREATER:
    MOV     R2,A    ;ACC > 100
    AJMP    EXIT
SMALLER:
    MOV    R1,A    ;ACC < 100
HERE:     AJMP    HERE
Funny thing is the 8051 PSW doesn't show the Zero flag and Negative flag (signed number test).

Allen
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225
You're right. The instruction is CJNE for Compare, Jump Not Equal. After it falls through or takes the jump you can check the carry flag.
Also a compare is just a non-destructive subtract instruction. In the 8051 you can save a copy of the accumulator, do the subtract, restore the accumulator and test the flags.
 

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
Thank you for your responses!
I have another question.If i would like to blink led of port 2 from msb to lsb. Should i have to use RR(rotate right) command? if yes then how to use it? RR P2, #No. correct/false?
 

cmartinez

Joined Jan 17, 2007
8,252
Thank you for your responses!
I have another question.If i would like to blink led of port 2 from msb to lsb. Should i have to use RR(rotate right) command? if yes then how to use it? RR P2, #No. correct/false?
The instruction set of the 8051 specifies that certain instructions can only be applied to certain registers. See attached file, for a complete view of what is possible and what is not.
In this case, RR, RL, RRC and RLC can only be applied to the accumulator, see pages 58 and 59.
Also, your question is not clear enough. You want to blink the led of P2 from the msb to the lsb of what register?
 

Attachments

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
I
The instruction set of the 8051 specifies that certain instructions can only be applied to certain registers. See attached file, for a complete view of what is possible and what is not.
In this case, RR, RL, RRC and RLC can only be applied to the accumulator, see pages 58 and 59.
Also, your question is not clear enough. You want to blink the led of P2 from the msb to the lsb of what register?

I used register R0-R7 in my code. To blink the led from msb to lsb of port 2. i used RR P2, #245 after completing code for delay
 

cmartinez

Joined Jan 17, 2007
8,252
##cmartinez thank you for your help. I am now clear about RR instruction.
Glad to be of help.... read the instruction set that I posted more carefully. Each instruction explains its advantages and limitations... for instance, in the MOV instruction, indirection can only be used with registers R0 and R1.
 

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
I have made a code for this statement!
Make a code for LED blinking that
–Takes input from Port 0 (8 bit)
–Divides the input by 10
–Calculates the difference between quotient and remainder
•If the difference is greater than 10, Blink all LEDs of all Ports one after another Port with 200 ms Delay (P0 -> P1 -> P2 -> P3 -> P0)
•If the difference is greater than 20, Blink LEDs of Port 2 from MSB to LSB with 400 ms Delay
•For all other values of difference blink Port 1 and Port 2 from LSB to MSB with 100 ms Delay
for number of instruction. i calculated as 200ms=200000us, 200000/1.805=184332, as total number of bit is 16.so, 2^16=65536, and instruction start from the execution is 65536-184332=-118796 , it gives negative value. i am posting my code.
 

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
please help me in this code.it question statement is in last post

Code:
org 00h
    mov tmod,#01   \\to use mode 1 and timer 0
    mov p0, #0ffh    \\use it as input
    mov p1, #00h      \\for output
    mov p2, #00h       = =
    mov p3, #00h        = =
    Here: mov A, #240d
    mov b, #10d
    DIV AB           \\A=24, B=0
    MOV R0,A
    MOV R1,B
    SUB A,B        \\A=A-B=24-0
MOV A,R0 
SUB A,11           \\A=A-11=24-11     
    Label1: JNC, check1    \\If A>=11
    Ljmp delay3
    check1: MOV A,R0
    SUB A,20   \\A=A-20=24-20
    label2: JC, delay1       \\A<20
    sjmp delay2
    delay1:clr C
    Here1: mov th0,#         \\To use higher nibble of starting instruction value for 200ms
    mov tl0,#                        \\To use lower nibble of starting instruction value for 200ms
    setb tr0                            \\run the timer
    again: jnz tf0,again           \\when instruction reach to FFFFH, Flag bit=1
    mov a,#240
    mov p0,a                        \\blink port0
    clr tr0                               {=
    clr tf0
    mov th0,#
    mov tl0,#
    setb tr0
    again1: jnz tf0, again1               
    mov a,#240
    mov p1,a
    clr tr0
    clr tf0
    mov th0,#                                            \\part of delay1 to blink led of port 0,1,2,3
    mov tl0,#                           
    setb tr0
    again2: jnz tf0, again2
    mov a,#240
    mov p2,a
    clr tr0
    clr tf0
    mov th0,#
    mov tl0,#
    setb tr0
    again3: jnz tf0, again3
    mov a,#240
    mov p3,a
    clr tr0
    clr tf0
    ljmp here1                                                   = }
    delay2:
    here2:mov th0,#
    mov tl0,#
    setb tr0
    poll: jnz tf0, poll
    mov a,#240
    rotation: mov p3,a
    RR a,rotation                                \\here i use this to blink led from msb to lsb as question demand
    clr tr0
    clr tf0
    Sjmp here2
    delay3:
    here3:mov th0,#
    mov tl0,#
    setb tr0
    repeat: jnz tf0, repeat
    mov a,#240
    rotation2: mov p1,a
    mov p2,a
    RL a, rotation2                             \\here i use this to blink led from lsb to msb as question demand
    clr tr0
    clr tf0
    sjmp here3
end
Moderators note : Please use code tags for pieces of code
 
Last edited by a moderator:

absf

Joined Dec 29, 2010
1,968
I assembled your program and found quite a lot of syntax mistakes.

1. There is no "SUB" instruction. Use "SUBB" instead.
2. For all the instructions with empty #... , I have inserted the default value 123.
3 always use first column for labels, then TAB or 10 spaces for instructions.
4. I know you like to use "//" for comments but my IDE only accepts ";"
5. Always use "code" tags for your code.... Dont let the Moderator do this for you all the time...

So here's your code after I corrected all the obvious mistakes

Code:
    org 00h
   
        mov tmod,#01       ;to use mode 1 and timer 0
        mov p0, #0ffh    ;use it as input
        mov p1, #00h    ;for output
        mov p2, #00h    ;= =
        mov p3, #00h    ;= =
Here:     mov A, #240d
        mov b, #10d
        DIV AB          ;A=24, B=0
        MOV R0,A
        MOV R1,B
        SUBB A,B        ;A=A-B=24-0
    MOV A,R0
    SUBB A,11    ;A=A-11=24-11    
Label1: JNC check1    ;If A>=11
        Ljmp delay3
check1: MOV A,R0
        SUBB A,20           ;A=A-20=24-20
label2: JC delay1     ;A<20
        sjmp delay2
delay1:    clr C
Here1:     mov th0,#123        ;To use higher nibble of starting
            ;instruction value for 200ms
        mov tl0,#123           ;To use lower nibble of starting
                ;instruction value for 200ms
        setb tr0        ;run the timer
again:     jnz tf0,again     ;when instruction reach to FFFFH, Flag bit=1
        mov a,#240
        mov p0,a                  ;blink port0
        clr tr0     ;   {=
    clr tf0
        mov th0,#123
        mov tl0,#123
        setb tr0
again1: jnz tf0, again1              
        mov a,#240
        mov p1,a
        clr tr0
        clr tf0
        mov th0,#123    ;part of delay1 to blink led of port 0,1,2,3
        mov tl0,#123                          
        setb tr0
again2: jnz tf0, again2
        mov a,#240
        mov p2,a
        clr tr0
        clr tf0
        mov th0,#123
        mov tl0,#123
        setb tr0
again3: jnz tf0, again3
        mov a,#240
        mov p3,a
        clr tr0
        clr tf0
        ljmp here1    ; = }
delay2:
here2:    mov th0,#123
        mov tl0,#123
        setb tr0
poll:     jnz tf0, poll
        mov a,#240
rotation: mov p3,a
        RR a,rotation    ;here i use this to blink led from
                ;msb to lsb as question demand
        clr tr0
        clr tf0
        Sjmp here2
delay3:
here3:    mov th0,#123
        mov tl0,#123
        setb tr0
repeat: jnz tf0,repeat
    mov a,#240
rotation2: mov p1,a
        mov p2,a
        RL a,rotation2    ;here i use this to blink led
            ;from lsb to msb as question demand
        clr tr0
        clr tf0
        sjmp here3
    end
And here is the LST file that include all the mistakes

Code:
Ameer project                                                                                                           PAGE 1
                         1             org 00h
                         2
                         3             mov tmod,#01    ;to use mode 1 and timer 0
                         4             mov p0, #0ffh   ;use it as input
                         5             mov p1, #00h    ;for output
                         6             mov p2, #00h    ;= =
                         7             mov p3, #00h    ;= =
                         8     Here:   mov A, #240d
                         9             mov b, #10d
                        10             DIV AB          ;A=24, B=0
                        11             MOV R0,A
                        12             MOV R1,B
                        13             SUBB A,B        ;A=A-B=24-0
                        14             MOV A,R0
                        15             SUBB A,11       ;A=A-11=24-11
                        16     Label1: JNC check1      ;If A>=11
                        17             Ljmp delay3
                        18     check1: MOV A,R0
                        19             SUBB A,20               ;A=A-20=24-20
                        20     label2: JC delay1       ;A<20
                        21             sjmp delay2
                        22     delay1: clr C
                        23     Here1:  mov th0,#123        ;To use higher nibble of starting
                        24                             ;instruction value for 200ms
                        25             mov tl0,#123           ;To use lower nibble of starting
                        26                             ;instruction value for 200ms
                        27             setb tr0        ;run the timer
                        28     again:  jnz tf0,again   ;when instruction reach to FFFFH, Flag bit=1
****ERROR: Too many operands, jnz can take only 1 operand
                        29             mov a,#240
                        30             mov p0,a                  ;blink port0
                        31             clr tr0         ;   {=
                        32             clr tf0
                        33             mov th0,#123
                        34             mov tl0,#123
                        35             setb tr0
                        36     again1: jnz tf0, again1
****ERROR: Too many operands, jnz can take only 1 operand
                        37             mov a,#240
                        38             mov p1,a
                        39             clr tr0
                        40             clr tf0
                        41             mov th0,#123    ;part of delay1 to blink led of port 0,1,2,3
                        42             mov tl0,#123
                        43             setb tr0
                        44     again2: jnz tf0, again2
****ERROR: Too many operands, jnz can take only 1 operand
                        45             mov a,#240
                        46             mov p2,a
                        47             clr tr0
                        48             clr tf0
                        49             mov th0,#123
                        50             mov tl0,#123
                        51             setb tr0
                        52     again3: jnz tf0, again3
****ERROR: Too many operands, jnz can take only 1 operand
                        53             mov a,#240
                        54             mov p3,a
                        55             clr tr0
                        56             clr tf0
                        57             ljmp here1      ; = }
                        58     delay2:
                        59     here2:  mov th0,#123
                        60             mov tl0,#123
                        61             setb tr0
                        62     poll:   jnz tf0, poll
****ERROR: Too many operands, jnz can take only 1 operand
                        63             mov a,#240
                        64     rotation: mov p3,a
                        65             RR a,rotation   ;here i use this to blink led from
****ERROR: Too many operands, rr can take only 1 operand
                        66                             ;msb to lsb as question demand
                        67             clr tr0
                        68             clr tf0
                        69             Sjmp here2
                        70     delay3:
                        71     here3:  mov th0,#123
                        72             mov tl0,#123
                        73             setb tr0
                        74     repeat: jnz tf0,repeat
****ERROR: Too many operands, jnz can take only 1 operand
                        75             mov a,#240
                        76     rotation2: mov p1,a
                        77             mov p2,a
                        78             RL a,rotation2  ;here i use this to blink led
****ERROR: Too many operands, rl can take only 1 operand
                        79                             ;from lsb to msb as question demand
                        80             clr tr0
                        81             clr tf0
                        82             sjmp here3
                        83             end
ASSEMBLY COMPLETE, 8 ERRORS FOUND, NO WARNINGS


ERROR SUMMARY:
Line 28, ERROR: Too many operands, jnz can take only 1 operand
Line 37, ERROR: Too many operands, jnz can take only 1 operand
Line 46, ERROR: Too many operands, jnz can take only 1 operand
Line 55, ERROR: Too many operands, jnz can take only 1 operand
Line 66, ERROR: Too many operands, jnz can take only 1 operand
Line 70, ERROR: Too many operands, rr can take only 1 operand
Line 80, ERROR: Too many operands, jnz can take only 1 operand
Line 85, ERROR: Too many operands, rl can take only 1 operand
Allen
 

absf

Joined Dec 29, 2010
1,968
For "jnz" istruction eg
repeat: jnz tf0,repeat
you can try repeat: cjne tf0,#00,repeat and see if it works.

For RR and RL instructions, you can refer to post #12. I thought @cmartines has explained it very well.

Allen
 

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
I can use Subb command only to determine result is positive or negative? for positive, carry=0, for negative, carry=1
or it also tell us whether number is greater or less than other.
Subb A,11 ; carry=1 if A<11
If it only tell us about positive and negative then i have to refer post#5, helped by #absf.
Second problem i am facing is:
for 5ms delay using timer and mode 1, i use the procedure as explained in last two lines of post#15, but for 200ms or 400ms, the value is much greater than that for 16 bit as mode 1 is for 16 bit.Now, i am confused how to calculate the delay? whether i should have to use nested loop without timer or there is anyother technique to resolve this using timer and mode?
 

Thread Starter

Ameer ul haq

Joined Mar 19, 2015
11
For "jnz" istruction eg
repeat: jnz tf0,repeat
you can try repeat: cjne tf0,#00,repeat and see if it works.

For RR and RL instructions, you can refer to post #12. I thought @cmartines has explained it very well.

Allen
thank you sir for your help to highlight my mistake.
 
Top