Keil Logo

C51: Writing to the Output Ports


Information in this article applies to:

  • C51 Version 5.50a
  • C51 Version 6.01

QUESTION

How do I write to the output ports of the 8051 in C?

ANSWER

Before you may write to an output port, you must define the SFR(s) for port you want to write to. You may write your own SFR definitions or you may include the register definition files (header files) for your particular microcontroller.

Define your own SFRs as follows:

sfr P1 = 0x90;     // PORT 1 SFR
sbit P1_1 = 0x91;  // Port 1.1 SFR bit

Include the register definition file as follows:

#include  // Include register definitions for the Intel 8051

Writing to the SFR

To write a byte value to Port 1 (all bits of port 1 will be set to the corresponding bits of the value written):

P1 = 0xAA;

This sets P1.0, P1.2, P1.4, and P1.6 low (0) and P1.1, P1.3, P1.5, and P1.7 high (1).

To write to a single port line:

P1_1 = 0;

This sets P1.1 low (0).

MORE INFORMATION

  • Refer to the Device Database for header files for different microcontrollers.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.