读取端口数据前面有控制字符_在微控制器端口中读取和写入值

读取端口数据前面有控制字符

读写字节 (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

读取端口数据前面有控制字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Erlang代码示例,用于将Redis的地址和端口写入`redis.config`文件: ```erlang %% 设置Redis的地址和端口 Address = "127.0.0.1", Port = 6379, %% 将Redis的地址和端口写入redis.config文件 {ok, File} = file:open("redis.config", [write]), io:fwrite(File, "redis_host = ~s~n", [Address]), io:fwrite(File, "redis_port = ~B~n", [Port]), file:close(File). ``` 上述代码首先设置了Redis的地址和端口,然后使用`file:open/2`函数打开`redis.config`文件,使用`io:fwrite/3`函数将地址和端口写入文件,最后使用`file:close/1`函数关闭文件。 在redis.config文件,你可以使用以下格式存储Redis的地址和端口: ```erlang redis_host = 127.0.0.1 redis_port = 6379 ``` 然后,你可以使用类似下面的代码读取redis.config文件的Redis地址和端口: ```erlang {ok, ConfigBin} = file:read_file("redis.config"), Config = string:tokens(binary_to_list(ConfigBin), "\n"), {ok, RedisHost} = lists:keyfind("redis_host", 1, Config), {ok, [_, HostValue]} = string:to_integer(string:tokens(RedisHost, "= ")), {ok, RedisPort} = lists:keyfind("redis_port", 1, Config), {ok, [_, PortValue]} = string:to_integer(string:tokens(RedisPort, "= ")), {HostValue, PortValue}. ``` 上述代码首先使用`file:read_file/1`函数读取`redis.config`文件数据,然后使用`string:tokens/2`函数将其分割成字符串列表。接着使用`lists:keyfind/3`函数查找Redis的地址和端口,并使用`string:to_integer/1`函数将其转换为整数。最后将Redis的地址和端口作为元组返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值