读取端口数据前面有控制字符
读写字节 (Reading and Writing Bytes)
In this example, we will see how we can read input from one port and copy it to the other port of an 8051 microcontroller.
在此示例中,我们将看到如何从一个端口读取输入并将其复制到8051微控制器的另一个端口。
A simple "Super Loop" application which copies the value from Port 1 (P1) to Port 2 (P2).
一个简单的“ 超级循环 ”应用程序,可将值从端口1(P1)复制到端口2(P2)。
#include <Reg52.H>
void main (void)
{
unsigned char Port1_value;
// Must set up P1 for reading
P1 = 0xFF;
while(1)
{
// Read the value of P1
Port1_value = P1;
// Copy the value to P2
P2 = Port1_value;
}
}
读写位 (Reading and Writing Bits)
In the previous example, we have seen how we can copy the value from one port of microcontroller to the other. In this example, we will refer how we can copy the value from one pin to another pin of the same port in an 8051 microcontroller. This is required in cases when the pins of a particular port are connected to the different input and output devices. Thus in such cases, the code to access the devices will be different as per the companies manufacturing them. So there comes the necessity to read from and write to a particular pin only, rather than the entire port of the microcontroller.
在前面的示例中,我们看到了如何将值从微控制器的一个端口复制到另一个端口。 在此示例中,我们将介绍如何在8051微控制器中将值从一个引脚复制到同一端口的另一引脚。 当特定端口的引脚连接到不同的输入和输出设备时,这是必需的。 因此,在这种情况下,根据制造设备的公司,访问设备的代码将有所不同。 因此,有必要仅对特定的引脚进行读写,而不是对微控制器的整个端口进行读写。
Let’s see how this can be achieved,
让我们看看如何做到这一点,
#include <Reg52.H>
sbit Switch_pin = P1^0;
sbit LED_pin = P1^1;
void main (void)
{
bit x;
// Set switch pin for reading
Switch_pin = 1;
while(1)
{
x = Switch_pin; // Read Pin 1.0
LED_pin = x; // Write to Pin 1.1
}
}
翻译自: https://www.includehelp.com/embedded-system/reading-and-writing-values-in-microcontroller-ports.aspx
读取端口数据前面有控制字符