8051: Problem with Port 0

Discussion in 'Embedded Systems and Microcontrollers' started by PY01A0080, Mar 7, 2013.

  1. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Hi everyone,

    my project is to switch on/off led one by one from P1,P3,P2,P0 with 1 seconde delay.

    When i try my code, it's working very well on P1,P2 and P3 but for P0, the leds are blinking one by one and it never stops.
    The portion of the code for Port 0 never exits.

    here is the code:


    Code ( (Unknown Language)):
    1.  org 0000h
    2. Loop:
    3. mov p1,#00h
    4. mov a,#01h
    5. mov p1,a
    6. mov p3,#00h
    7. mov p2,#00h
    8. mov p0,#0FFh
    9.  
    10. cpl a
    11. L7:
    12. rl a
    13. mov p0,a
    14. call delay
    15. jb p0.7,L7
    16. clr p0.7
    Thank you very much if someone could help me,
    ps: i use the 8051
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    You have not shown the full listing of your code.
    Your code is incomplete.
    Where does your code initialize the port direction register?
    Where is delay?
    How does the code loop back?
     
  3. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    OK, here's the full code:

    Code ( (Unknown Language)):
    1.  org 0000h
    2. Loop:
    3. mov p1,#00h
    4. mov a,#01h
    5. mov p1,a
    6. mov p3,#00h
    7. mov p2,#00h
    8.  
    9. mov p0,#0FFh
    10.  
    11. lcall delay
    12.  
    13. ;From P1.0 to P1.7
    14. L1:
    15. rl a
    16. mov p1,a
    17. call delay
    18. jnb p1.7, L1
    19. clr p1.7
    20. ;From P3.0 to P3.7
    21. mov a,#01h
    22. mov p3,A
    23. LCALL DELAY
    24. L2:
    25. rl a
    26. mov p3,a
    27. call delay
    28. jnb p3.7, L2
    29. clr p3.7
    30. ;From P2.0 to P2.7
    31. L4:
    32. rl a
    33. mov p2,a
    34. call delay
    35. jnb p2.7,L4
    36. clr p2.7
    37.  
    38.  
    39. ;From P0.0 to P0.7
    40. cpl a
    41. L7:
    42. rl a
    43. mov p0,a
    44. call delay
    45. jb p0.7,L7
    46. clr p0.7
    47.  
    48.  
    49. ;-----------------------------------------
    50. ;Reverse From P0.7 to P0.0
    51. cpl a
    52. L8:
    53. rr a
    54. mov p0,a
    55. call delay
    56. jb p0.0,L8
    57. clr p0.0
    58.  
    59.  
    60. ;Reverse From P2.7 to P2.0
    61.  
    62. L3:
    63. rr a
    64. mov p2,a
    65. call delay
    66. jnb p2.0,L3
    67. clr p2.0
    68. ;Reverse from P3.7 to P3.0
    69. L5:
    70. rr a
    71. mov p3,a
    72. call delay
    73. jnb p3.0,L5
    74. clr p3.0
    75. ;Reverse from P1.7 to P1.0
    76. L6:
    77. rr a
    78. mov p1,a
    79. call delay
    80. jnb p1.0,L6
    81. clr p1.0
    82.  
    83. ;--------------------
    84. ljmp Loop
    85. delay: mov r0,#14
    86. m1:MOV TMOD,#01H ; initialise TMOD
    87. MOV TL0,#76H ; initialise TL0
    88. MOV TH0,#01H ; initialise TH0
    89.  
    90. SETB TR0 ; start timer
    91. Wait: JNB TF0,Wait ; wait for TF0
    92. ; stop timer
    93. CLR TF0 ; clear TF0
    94. djnz r0,m1
    95. ret
    96. end
     
  4. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    First you have jnb as your exit clause... But with P0 you are using jb

    There is no need for LCALL as your code is small ( you have mixed "call delay" and "lcall delay") It isn't a problem... I just wondered
     
    Last edited: Mar 8, 2013
  5. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    An 8051 does not have any "Port Direction Registers". When you write a 1 to the data latch it enables a weak pullup that makes it a quasi bi-directional pin.

    Writing a 0 to the data latch makes it an open-drain output and disables the weak pullup.
     
  6. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Thank you
    i replaced lcall by call, it was just a small mistake.
    ,tried to 0FFh or 00h to P0.
    ,replaced JB by JNB now the loop L7 exits but all leds are lit
    i put VCC on the anode of P0 leds and the cathode directly to Port0

    so the same problem exists
    thank you
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    You can't hook LEDs directly to port pins. You need a current limiting resistor.
     
  8. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Yes i know but it's in the simulator (Proteus)
    and i tried with resistor.

    I think the problem is in the JB or JNB.


    Thank you
     
  9. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    i think we can't fix this problem with port0, so i will just avoid to use this port.
    i asked to an expert around me, they can't recolve.

    but i'm ready to try any other solutions

    Thank you
     
  10. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    Can you post me your schematic.... I'll have a go..
     
  11. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Here is the image of the schematics, i try to attach the .dsn file for proteus, it failed
    Thank you
     
  12. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    The reason the program keeps looping on Port 0 is because port 0 has "open drain" outputs. You knew it and that's why you connected the LEDs to Vcc using Lo to light them. But the output pin is not low enough to be detected
    as Logic Low without 10K pull-up resistors.

    I simulated your circuit on proteus and though the LED did get lighted up but the logic on the pins are still at "Hi". Try pull up resistors parallel with your LED and the program should exit the loop.

    Allen
     
    Last edited: Mar 9, 2013
  13. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Thank you so much absf, now the program exits from the loop.
    i have to look for why leds do not light enough.
    Your answer was usefull for me.
    :)
     
    absf likes this.
  14. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    If the LED are too dim. Right-click on the resistor pack and change the property to "digital".

    Allen
     
  15. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    I tried to change the resistor's property from analog to digital but in digital, the program stay in the loop.
    PY01A0080
     
  16. PY01A0080

    Thread Starter Member

    Mar 7, 2013
    42
    3
    Sorry absf, now it's working well.
    i think i forgot to refresh windows or something like that.
    It's perfect now, i know how to use the port0.
    Thank you very much
    PY01A0080
     
    absf likes this.
  17. @android

    Member

    Dec 15, 2011
    178
    9
    Always use pull up resistors with PORT0 of 8051. It doesn't have internal pull up circuit like other ports on it.
     
  18. shankar387

    New Member

    Apr 18, 2013
    1
    0
Loading...