使用在C ++循环一个字符串数组来PIC I / O引脚之间切换 [英] Using a string array in a C++ loop to switch between PIC I/O pins

查看:321
本文介绍了使用在C ++循环一个字符串数组来PIC I / O引脚之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关如何使用字符串数组元素作为函数的一部分(参数)任何提示?

Any tips about how using a string array elements as a part ( argument ) of function ?

我的数组(其中继续PIC输出端口)

My array is ( which continue of PIC output ports )

string Port[5] = {PORTA.0, PORTA.1, PORTA.2, PORTA.3};

我想在发送逻辑0港口顺序它们说成阵列端口

I want to use it in a loop that sends logic 0 to the ports sequentially which are stated into array Port

void loop() {
Port[2] = 0x00;   // sends 0 to PORTA.1
}

如何做,在C ++ ??

How to do that in C++ ??

推荐答案

您根本的问题是,你要一组端口引脚发送到功能。

Your root issue is that you want to send a set of port pins to a function.

标识符 PORTA 之类的不是字符串。他们是PIC的编译器的保留字。他们重新present的addressible位置(任一个端口或内存映射)。

The identifiers PORTA and the like are not strings. They are reserved words for the PIC compilers. They represent an addressible location (either port or memory mapped).

的点号, PORTA.3 ,指的是端口的比特数。

The dot notation, PORTA.3, refers to a bit number of the port.

的反例将复制值`PORT_FROG.8。
你是否尝试过使用下列内容:

The counter example would be copying a value to `"PORT_FROG.8". Have you tried using the following:

  const char port_a_3[] = "PORTA.3";
  *port_a_3 = 5;

问题是,最后的语句修改文本文字和简化版,写入端口。

The problem is that the last statement modifies a text literal and does't write to the port.

您将需要查找语法传递一个端口位的地址,以及如何通过一个端口的地址。如果没有文件或例子,事情会变得复杂。

You will need to look up the syntax for passing the address of a port bit, and how to pass the address of a port. If there is no documentation or examples, things will get complex.

最轻便的方法是创建一个端口类,并通过周围的情况下,或者你可以使用某种的std ::对在这里你提供一个端口ID和一个位编号。

The most portable method would be to create a Port class and pass around instances of that or you could use some kind of std::pair where you supply a port ID and a bit number.

一个更有趣的问题:是你的平台高效设计的?
通常情况下,你能说服硬件民间放置的类似的功能线相邻。这样,您就可以通过读整个端口,而不是读不同的端口不同的位利用自己状态的快照。

A more intriguing question: Is your platform designed efficiently? Usually, you can convince hardware folk to place lines of similar functionality next to each other. This way, you can take a snapshot of their status by reading the entire port instead of reading different bits from different ports.

我强烈建议你写一个函数读取指定的脱节位转换成一个数字,并且可以写他们另一个功能。在这个概念,你将有一个单位的所有状态,并且可以四处传递状态的功能。如果你需要写不相交引脚为一组,你可以打电话给你的功能。

I highly suggest that you write a function that reads the specified disjoint bits into one number and another function that can write them. In this concept, you will have all the status in one unit and can pass that status around to functions. If you need to write to the disjoint pins as a group, you can call your write function.

根的要求是要一组端口引脚地址传递给函数,使他们能够读取和写入端口引脚。这不是标准的C ++,所以你必须看看你的PIC编译器文档,看它是否是可能的。还有其他的替代品,如绕过端口的型号;端口和放大器;位对;编码读取和写入多个引脚到一个状态机功能;或改变硬件使软件更容易开发。抱歉,端口标识符不是文本文字或字符串,而不能被操纵的方式。

The root requirement is that you want to pass a set of port pin addresses to functions so that they can read and write to port pins. This is not available in standard C++, so you will have to look at your PIC compiler documentation to see if it is possible. There are other alternatives, such as passing around models of ports; port & bit pairs; coding functions that read and write multiple pins into one status unit; or changing the hardware to make the software easier to develop. Sorry, but the port identifiers are not text literals or strings and cannot be manipulated that way.

编辑1:结果
你可能能够通过映射到一个端口的结构。同样,看一下编译器文档。

Edit 1:
You may be able to pass a structure that is mapped to a port. Again, look at the compiler documentation.

例如:

  struct MyPort_Model
  {
     unsigned int bit_0 : 1;
     unsigned int bit_1 : 1;
     unsigned int bit_2 : 1;
     unsigned int bit_3 : 1;
  } my_port_variable @ PORTA;

另外一个例子:

  struct Model_4_bit_HW_Item
  {
     unsigned int MOSI : 1;
     unsigned int MISO : 1;
     unsigned int clock : 1;
     unsigned int chip_select : 1;
     unsigned int reserved : 4;
     void read_from_port(void)
     {
       MOSI = PORTA.0;
       MISO = PORTA.1;
       clock = PORTA.2;
       reserved = PORTA.3;
     }
  };

这篇关于使用在C ++循环一个字符串数组来PIC I / O引脚之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆