Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

Using a string array in a C++ loop to switch between PIC I/O pins

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

My array is ( which continue of PIC output ports )

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

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
}

How to do that in C++ ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Port Pins are not strings

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).

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

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.

Passing Port Pins to a function

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.

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.

More efficient hardware design

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.

Unifying Port Bits

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.

Summary

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.

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

Example:

  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;

Another example:

  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;
     }
  };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...