I am confused about how to configure ports of AT89S52. It has four I/O ports where in each port contains 8 pins that can be configured as inputs or outputs. It can be configured as input or output depends on logic state.
In order to configure a pin of AT89S52 as an output, it is necessary to send a logic zero (0) to the port pins. In this case, the voltage level at the appropriate pin will be 0.
Similarly, in order to configure a pin of AT89S52 as an input, it is necessary to send a logic one (1) to the port pin. In this case, the voltage level on the appropriate pin will be 5V.
I haven't tested this code. I have doubt on the input & output configuration. Can we set all port in same way
Configure port pin as input
Configure port pin as output
In order to configure a pin of AT89S52 as an output, it is necessary to send a logic zero (0) to the port pins. In this case, the voltage level at the appropriate pin will be 0.
Similarly, in order to configure a pin of AT89S52 as an input, it is necessary to send a logic one (1) to the port pin. In this case, the voltage level on the appropriate pin will be 5V.
C:
#include <REG51.h>
sbit Switch = P0^1; //switch connected to P0.1
sbit LED = P2^0; //LED connected to p2.0
#define ON 1
#define OFF 0
#define High 1
#define Low 0
void main (void)
{
Switch = High; //Set P0^1 as input pin by making it high
LED = Low; //Set P2^0 as output pin by making it low
while(1)
{
if(Switch == ON) //Check switch
{
LED = ON; // Turn on LED
}
else
{
LED = OFF; // Turn off LED
}
}
}
Configure port pin as input
Code:
P0^1 = 1;
P1^1 = 1;
P2^1 = 1;
P3^1 = 1;
Code:
P0^1 = 0;
P1^1 = 0;
P2^1 = 0;
P3^1 = 0;