Embedded-C Guideline for AT89C51 (I/O Programming)

This article has complete embedded-C programming guideline for AT89C51 micro-controller. All examples are written in Keil uVision2 V2.30 and Proteus 8.0 SP 0 is used for simulation.

Input and Output Programming

The AT89C51 has four 8-bit ports and you can configure any port or individual pin as input or output by writing one (1) to make any port or pin as input and by writing zero (0) to make as output. This is required only at the time of initialization.

Port-0 Initialization as Input

The following function initialize the port-0 as input.
port-0 input
void InitPort0AsInput(void)
{
// write 1’s to initialize port-0 as input
P0 = 0xFF;
}

Port-0 Initialization as Output

The following function initialize the port-0 as output.
port-0 output
void InitPort0AsOutput(void)
{
// write 0’s to initialize port-0 as output
P0 = 0x00;
}

Port-0 Initialization as In/Out

You can also configure individual pin as input or output. The following function initialize the port-0 pin P0.7, P0.6,P0.0 as inputs and others are outputs.
port-0 inout
void InitPort0AsInOut(void)
{
// P0.7, P0.6, and P0.0 are inputs and all others are outputs.
P0 = 0xC1;
}

Scan Port-0 as Byte Level

The following function returns the status of port-0 as a single byte.
unsigned char ScanPort0(void)
{
// return port-0 data
return P0;
}

Scan Port-0 Single Bit

The following function scan a single bit of port-0 and return the status of single bit. You can use this function as “ScanPort0Bit(7)” which scan the status of port-0 bit-7.
bit ScanPort0Bit(unsigned char port0_bit)
{
unsigned char val = 1;
unsigned char bit_status = 0;
bit_status = P0 & (val << port0_bit);
if(bit_status >= 1)
return 1;
else
return 0;
}

Set Port-0 Single Bit

The following function sets a single bit of port-0. Let you want to set the port-0 bit-5, you can call function as “SetBitPort0(5);” and the valid passing parameter value range is from 0 to 7.
void SetBitPort0(unsigned char bit_num)
{
unsigned char val = 1;
val = val << bit_num;
P0 |= val;
}

Clear Port-0 Single Bit

The following function clears a single bit of port-0. Let you want to clear the port-0 bit-5, you can call function as “ClearBitPort0(5);” and the valid passing parameter value range is from 0 to 7.
void ClearBitPort0(unsigned char bit_num)
{
unsigned char val = 1;
unsigned char inv_val = 0;
val = val << bit_num;
inv_val = ~val;
val = inv_val;
P0 &= val;
}

Write Data to Port-0

The following function writes 8-bit data to port-0. The valid range to pass this function is from 0 to 255 (0x00 to 0xFF). Let you want to write 0x55 data to port-0, you can call this function as “WrDataToPort0(0x55);”.
void WrDataToPort0(unsigned char wr_data)
{
P0 = wr_data;
}

Port-1 Initialization as Input

The following function initialize the port-1 as input.
port-1 input
void InitPort1AsInput(void)
{
// write 1’s to initialize port-1 as input
P1 = 0xFF;
}

Port-1 Initialization as Output

The following function initialize the port-1 as output.
port-1 output
void InitPort1AsOutput(void)
{
// write 0’s to initialize port-1 as output
P1 = 0x00;
}

Port-1 Initialization as In/Out

You can also configure individual pin as input or output. The following function initialize the port-1 pin P1.6, P1.4,P1.1 as inputs and others are outputs.
port-1 inout
void InitPort1AsInOut(void)
{
// P1.6, P1.4, and P1.1 are inputs and all others are outputs.
P1 = 0x52;
}

Scan Port-1 as Byte Level

The following function returns the status of port-1 as a single byte.
unsigned char ScanPort1(void)
{
// return port-1 data
return P1;
}

Scan Port-1 Single Bit

The following function scan a single bit of port-1 and return the status of single bit. You can use this function as “ScanPort1Bit(5)” which scan the status of port-1 bit-5.
bit ScanPort1Bit(unsigned char port1_bit)
{
unsigned char val = 1;
unsigned char bit_status = 0;
bit_status = P1 & (val << port1_bit);
if(bit_status >= 1)
return 1;
else
return 0;
}

Set Port-1 Single Bit

The following function sets a single bit of port-1. Let you want to set the port-1 bit-5, you can call function as “SetBitPort1(5);” and the valid passing parameter value range is from 0 to 7.
void SetBitPort1(unsigned char bit_num)
{
unsigned char val = 1;
val = val << bit_num;
P1 |= val;
}

Clear Port-1 Single Bit

The following function clears a single bit of port-1. Let you want to clear the port-1 bit-5, you can call function as “ClearBitPort1(5);” and the valid passing parameter value range is from 0 to 7.
void ClearBitPort1(unsigned char bit_num)
{
unsigned char val = 1;
unsigned char inv_val = 0;
val = val << bit_num;
inv_val = ~val;
val = inv_val;
P1 &= val;
}

Write Data to Port-1

The following function writes 8-bit data to port-1. The valid range to pass this function is from 0 to 255 (0x00 to 0xFF). Let you want to write 0x55 data to port-1, you can call this function as “WrDataToPort1(0x55);”.
void WrDataToPort1(unsigned char wr_data)
{
P1 = wr_data;
}

Port-2 Initialization as Input

The following function initialize the port-2 as input.
port-2 input
void InitPort2AsInput(void)
{
// write 1’s to initialize port-2 as input
P2 = 0xFF;
}

Port-2 Initialization as Output

The following function initialize the port-2 as output.
port-2 output
void InitPort2AsOutput(void)
{
// write 0’s to initialize port-2 as output
P2 = 0x00;
}

Port-2 Initialization as In/Out

You can also configure individual pin as input or output. The following function initialize the port-2 pin P2.5, P2.4,P2.3 as inputs and others are outputs.
port-2 inout
void InitPort2AsInOut(void)
{
// P2.5, P2.4, and P2.3 are inputs and all others are outputs.
P2 = 0x38;
}

Scan Port-2 as Byte Level

The following function returns the status of port-2 as a single byte.
unsigned char ScanPort2(void)
{
// return port-2 data
return P2;
}

Scan Port-2 Single Bit

The following function scan a single bit of port-2 and return the status of single bit. You can use this function as “ScanPort2Bit(3)” which scan the status of port-2 bit-3.
bit ScanPort2Bit(unsigned char port2_bit)
{
unsigned char val = 1;
unsigned char bit_status = 0;
bit_status = P2 & (val << port2_bit);
if(bit_status >= 1)
return 1;
else
return 0;
}

Set Port-2 Single Bit

The following function sets a single bit of port-2. Let you want to set the port-2 bit-5, you can call function as “SetBitPort2(5);” and the valid passing parameter value range is from 0 to 7.
void SetBitPort2(unsigned char bit_num)
{
unsigned char val = 1;
val = val << bit_num;
P2 |= val;
}

Clear Port-2 Single Bit

The following function clears a single bit of port-2. Let you want to clear the port-2 bit-5, you can call function as “ClearBitPort2(5);” and the valid passing parameter value range is from 0 to 7.
void ClearBitPort2(unsigned char bit_num)
{
unsigned char val = 1;
unsigned char inv_val = 0;
val = val << bit_num;
inv_val = ~val;
val = inv_val;
P2 &= val;
}

Write Data to Port-2

The following function writes 8-bit data to port-2. The valid range to pass this function is from 0 to 255 (0x00 to 0xFF). Let you want to write 0x55 data to port-2, you can call this function as “WrDataToPort2(0x55);”.
void WrDataToPort2(unsigned char wr_data)
{
P2 = wr_data;
}

Port-3 Initialization as Input

The following function initialize the port-3 as input.
port-3 input
void InitPort3AsInput(void)
{
// write 1’s to initialize port-3 as input
P3 = 0xFF;
}

Port-3 Initialization as Output

The following function initialize the port-3 as output.
port-3 output
void InitPort3AsOutput(void)
{
// write 0’s to initialize port-3 as output
P3 = 0x00;
}

Port-3 Initialization as In/Out

You can also configure individual pin as input or output. The following function initialize the port-3 pin P3.5, P3.3,P3.2 as inputs and others are outputs.
port-3 inout
void InitPort3AsInOut(void)
{
// P3.5, P3.3, and P3.2 are inputs and all others are outputs.
P3 = 0x2C;
}

Scan Port-3 as Byte Level

The following function returns the status of port-3 as a single byte.
unsigned char ScanPort3(void)
{
// return port-3 data
return P3;
}

Scan Port-3 Single Bit

The following function scan a single bit of port-3 and return the status of single bit. You can use this function as “ScanPort3Bit(0)” which scan the status of port-3 bit-0.
bit ScanPort3Bit(unsigned char port3_bit)
{
unsigned char val = 1;
unsigned char bit_status = 0;
bit_status = P3 & (val << port3_bit);
if(bit_status >= 1)
return 1;
else
return 0;
}

Set Port-3 Single Bit

The following function sets a single bit of port-3. Let you want to set the port-3 bit-5, you can call function as “SetBitPort3(5);” and the valid passing parameter value range is from 0 to 7.
void SetBitPort3(unsigned char bit_num)
{
unsigned char val = 1;
val = val << bit_num;
P3 |= val;
}

Clear Port-3 Single Bit

The following function clears a single bit of port-3. Let you want to clear the port-3 bit-5, you can call function as “ClearBitPort3(5);” and the valid passing parameter value range is from 0 to 7.
void ClearBitPort3(unsigned char bit_num)
{
unsigned char val = 1;
unsigned char inv_val = 0;
val = val << bit_num;
inv_val = ~val;
val = inv_val;
P3 &= val;
}

Write Data to Port-3

The following function writes 8-bit data to port-3. The valid range to pass this function is from 0 to 255 (0x00 to 0xFF). Let you want to write 0x55 data to port-3, you can call this function as “WrDataToPort3(0x55);”.
void WrDataToPort3(unsigned char wr_data)
{
P3 = wr_data;
}

Download APIs

You are free to use APIs anywhere. You will find two files one is “io_api.h” and “io_api.c”. can download complete APIs from the following link, download file here.

Example of I/O Programming

Problem statement is the following.
  1. Read port-0 status and write to port-2.
  2. Read port-3 pin-7 status, if pin-7 is high write 0x0F to port-1 other wise write 0xF0.
  3. Required hardware is the following. Note that only ports connections are shown here. You can also download PDF format of image download here.
port inout example at89c51
You can find complete project here which includes two folders one is “project” having complete software (Keil uVision V2.30)and the other is “sim” folder having Proteus (V8.0 SP0) simulation file. You can download here.

Comments

Popular posts from this blog

How to Program Interrupts in PIC16F877A

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)