8051: Problem with Port 0

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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:


Rich (BB code):
 org 0000h
Loop:
mov p1,#00h
mov a,#01h
mov p1,a
mov p3,#00h
mov p2,#00h
mov p0,#0FFh

cpl a
L7:
rl a
mov p0,a
call delay
jb p0.7,L7
clr p0.7
Thank you very much if someone could help me,
ps: i use the 8051
 

MrChips

Joined Oct 2, 2009
30,823
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?
 

Thread Starter

PY01A0080

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

Rich (BB code):
 org 0000h
Loop:
mov p1,#00h
mov a,#01h
mov p1,a
mov p3,#00h
mov p2,#00h

mov p0,#0FFh

lcall delay

;From P1.0 to P1.7
L1: 
rl a
mov p1,a
call delay
jnb p1.7, L1
clr p1.7
;From P3.0 to P3.7
mov a,#01h
mov p3,A
LCALL DELAY
L2:
rl a
mov p3,a
call delay
jnb p3.7, L2
clr p3.7
;From P2.0 to P2.7
L4:
rl a
mov p2,a
call delay
jnb p2.7,L4
clr p2.7


;From P0.0 to P0.7
cpl a
L7:
rl a
mov p0,a
call delay
jb p0.7,L7
clr p0.7


;-----------------------------------------
;Reverse From P0.7 to P0.0
cpl a
L8:
rr a
mov p0,a
call delay
jb p0.0,L8
clr p0.0


;Reverse From P2.7 to P2.0

L3:
rr a
mov p2,a
call delay
jnb p2.0,L3
clr p2.0
;Reverse from P3.7 to P3.0
L5:
rr a
mov p3,a
call delay
jnb p3.0,L5
clr p3.0
;Reverse from P1.7 to P1.0
L6:
rr a
mov p1,a
call delay
jnb p1.0,L6
clr p1.0

;--------------------
ljmp Loop
delay: mov r0,#14
m1:MOV TMOD,#01H ; initialise TMOD
MOV TL0,#76H ; initialise TL0
MOV TH0,#01H ; initialise TH0

SETB TR0 ; start timer
Wait: JNB TF0,Wait ; wait for TF0
; stop timer
CLR TF0 ; clear TF0
djnz r0,m1
ret
end
 

Ian Rogers

Joined Dec 12, 2012
1,136
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:

Papabravo

Joined Feb 24, 2006
21,227
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.
 

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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
 

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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
 

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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
 

absf

Joined Dec 29, 2010
1,968
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:


Rich (BB code):
 org 0000h
Loop:
mov p1,#00h
mov a,#01h
mov p1,a
mov p3,#00h
mov p2,#00h
mov p0,#0FFh

cpl a
L7:
rl a
mov p0,a
call delay
jb p0.7,L7
clr p0.7
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
 

Attachments

Last edited:

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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.
:)
 

Thread Starter

PY01A0080

Joined Mar 7, 2013
42
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
 
Top