8051

Discussion in 'Programmer's Corner' started by Ameer ul haq, Mar 19, 2015.

  1. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    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?
     
  2. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,346
    Hello,

    Only "8051" as title is very less info.
    Please choose a better title and show us what you have done upto now.

    Bertus
     
    Ameer ul haq likes this.
  3. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    Yes. CMP is the instruction you need to do arithmetic comparisons. With a bit of effort you can work out the details for both signed and unsigned numbers.
     
    Ameer ul haq likes this.
  4. Brevor

    Active Member

    Apr 9, 2011
    279
    54
    The 8051 does not have a CMP instruction. ???
     
    Ameer ul haq likes this.
  5. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    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 (Text):
    1.  
    2.     MOV    A,#105
    3.     CJNE    A,#100,CHECK
    4. EQUAL:    MOV    R0,A    ;ACC = 100
    5.     AJMP    EXIT
    6. CHECK:    JC    SMALLER
    7. GREATER:
    8.     MOV     R2,A    ;ACC > 100
    9.     AJMP    EXIT
    10. SMALLER:
    11.     MOV    R1,A    ;ACC < 100
    12. HERE:     AJMP    HERE
    13.  
    Funny thing is the 8051 PSW doesn't show the Zero flag and Negative flag (signed number test).

    Allen
     
    Last edited: Mar 20, 2015
    Ameer ul haq likes this.
  6. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    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.
     
    Ameer ul haq likes this.
  7. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    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?
     
  8. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    3,572
    2,537
    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?
     
    Ameer ul haq likes this.
  9. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    I did it this way and tested on the "MCU 8051 IDE" simulator.

    8051 led shifting.PNG

    Allen
     
    Ameer ul haq and cmartinez like this.
  10. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    I

    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
     
  11. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    Thanks dear. it really help me to clear my concept.
     
  12. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    3,572
    2,537
    You cannot rotate P2 directly, you have to first feed that value to A, rotate it, and then place it back into P2

    MOV A, #245
    RR A
    MOV P2, A
     
    Ameer ul haq likes this.
  13. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    ##cmartinez thank you for your help. I am now clear about RR instruction.
     
    cmartinez likes this.
  14. cmartinez

    AAC Fanatic!

    Jan 17, 2007
    3,572
    2,537
    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.
     
    Ameer ul haq likes this.
  15. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    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.
     
  16. Ameer ul haq

    Thread Starter New Member

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

    Code (Text):
    1.  
    2. org 00h
    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.     SUB A,B        \\A=A-B=24-0
    14. MOV A,R0
    15. SUB A,11           \\A=A-11=24-11    
    16.     Label1: JNC, check1    \\If A>=11
    17.     Ljmp delay3
    18.     check1: MOV A,R0
    19.     SUB A,20   \\A=A-20=24-20
    20.     label2: JC, delay1       \\A<20
    21.     sjmp delay2
    22.     delay1:clr C
    23.     Here1: mov th0,#         \\To use higher nibble of starting instruction value for 200ms
    24.     mov tl0,#                        \\To use lower nibble of starting instruction value for 200ms
    25.     setb tr0                            \\run the timer
    26.     again: jnz tf0,again           \\when instruction reach to FFFFH, Flag bit=1
    27.     mov a,#240
    28.     mov p0,a                        \\blink port0
    29.     clr tr0                               {=
    30.     clr tf0
    31.     mov th0,#
    32.     mov tl0,#
    33.     setb tr0
    34.     again1: jnz tf0, again1              
    35.     mov a,#240
    36.     mov p1,a
    37.     clr tr0
    38.     clr tf0
    39.     mov th0,#                                            \\part of delay1 to blink led of port 0,1,2,3
    40.     mov tl0,#                          
    41.     setb tr0
    42.     again2: jnz tf0, again2
    43.     mov a,#240
    44.     mov p2,a
    45.     clr tr0
    46.     clr tf0
    47.     mov th0,#
    48.     mov tl0,#
    49.     setb tr0
    50.     again3: jnz tf0, again3
    51.     mov a,#240
    52.     mov p3,a
    53.     clr tr0
    54.     clr tf0
    55.     ljmp here1                                                   = }
    56.     delay2:
    57.     here2:mov th0,#
    58.     mov tl0,#
    59.     setb tr0
    60.     poll: jnz tf0, poll
    61.     mov a,#240
    62.     rotation: mov p3,a
    63.     RR a,rotation                                \\here i use this to blink led from msb to lsb as question demand
    64.     clr tr0
    65.     clr tf0
    66.     Sjmp here2
    67.     delay3:
    68.     here3:mov th0,#
    69.     mov tl0,#
    70.     setb tr0
    71.     repeat: jnz tf0, repeat
    72.     mov a,#240
    73.     rotation2: mov p1,a
    74.     mov p2,a
    75.     RL a, rotation2                             \\here i use this to blink led from lsb to msb as question demand
    76.     clr tr0
    77.     clr tf0
    78.     sjmp here3
    79. end
    80.  
    Moderators note : Please use code tags for pieces of code
     
    Last edited by a moderator: Mar 21, 2015
  17. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    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 (Text):
    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
    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              
    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
    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
    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
    63.         mov a,#240
    64. rotation: mov p3,a
    65.         RR a,rotation    ;here i use this to blink led from
    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
    75.     mov a,#240
    76. rotation2: mov p1,a
    77.         mov p2,a
    78.         RL a,rotation2    ;here i use this to blink led
    79.             ;from lsb to msb as question demand
    80.         clr tr0
    81.         clr tf0
    82.         sjmp here3
    83.     end
    84.  
    And here is the LST file that include all the mistakes

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

    Senior Member

    Dec 29, 2010
    1,492
    372
    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
     
    cmartinez likes this.
  19. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    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?
     
  20. Ameer ul haq

    Thread Starter New Member

    Mar 19, 2015
    11
    1
    thank you sir for your help to highlight my mistake.
     
Loading...