Port0 needs pull-up resistors to get a statical non-zero output voltage.
Datasheet explains:
Port 0 has no internal pullups. The FET pullup in the P0 output driver (see Figure 4) is used only when the Port emits 1s during external memory accesses. Otherwise, the FET pullup is off. Consequently, P0 lines that are used as output port lines are open drain.
As you can read from the datasheet, your circuit doesn't work. The transistor will be never turned on.
I guess you want to assure that the relay isn't turned on unintentionally, e.g during µC reset. The only save way to achieve this with a 8051 processor is to an active low controlled relay driver, e.g. a PNP transistor or a NPN transistor with additional inverter.
As the PORT0 of 8051 microcontroller is open drain. So externally you need to connect a pull-up to make it work. Jay when you try to make that particular pin high from port 0 it won't work. So make a pull-up resistor and use a PNP transistor to drive a relay or still if you want to use NPN add a inverter after the pull-up resistor.
Actually i thought to use PNP transistor but as somewhere i read that it is not recommended to use PNP transistor for switching ground of circuit. So i didnt use PNP.
But according to you i will try practically by using PNP transistor.
Thank you for your advice.
Dear FvM,
As you said "The only save way to achieve this with a 8051 processor is to an active low controlled relay driver, e.g. a PNP transistor or a NPN transistor with additional inverter"
can you please suggest me any example of this type of circuit or any practical circuit ?