Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a "virtual port" which consists of multiple ports on Arduino

I started using ports on Arduino instead of setting every pin low or high by hand. This is very useful and a lot faster. I am on a project, where i need at least one full port (8 bits) and at least one Serial Port.

I wanted to use the Arduino UNO but it has only one full port, port D. PD0 and PD1 are used for Serial communication. This means i can't use port D.

I was wondering if there is a possibility for me to merge multiple ports into a "virtual port". In the end i want something like this:

PORTX = 0b11111111; // the first 2 bits are PB0/PB1 and bit 3-8 are PD3-PD8

Is this possible in any way???

like image 709
felixlkunz Avatar asked Feb 18 '19 19:02

felixlkunz


People also ask

Which Arduino boards have multiple serial ports?

Arduino Mega has multiple Serial ports, in addition to the one connected to USB.

Does Arduino Uno have 2 serial ports?

The Arduino Leonardo has a spare hardware serial port, and the Arduino Mega 2560 has 3 spare hardware serial ports.

What is the use of two serial ports in Arduino?

This can be extremely helpful when the need arises to communicate with two serial enabled devices, or to talk with just one device while leaving the main serial port open for debugging purpose. In the example below, digital pins 8 and 10 on your Arduino board are used as virtual RX serial lines.

What is Compim?

COMPIM is used to model physical COM interfaces in Proteus. It works by capturing and buffering serial signals which it then presents to the electrical circuit. The computer's serial ports will be used to conduct all serial data originating from the CPU or the UART model.


1 Answers

I would say 'yes' it is possible, but maybe not in the way you want it (or maybe I just don't know how to do so^^)

First of all, the PORTS are macros from Atmel. Your Arduino-Uno is based on the AtMega328p and therefore uses the AVR-Toolchain with all those PORTS under the hood. If you were about to program your microcontroller without the arduino-bootloader and all the fancy arduino-library-stuff, you would address all your GPIOs that way.

If you have a look into the code of the Atmel-AVR Toolchain (that arduino is sitting on top of), you would see, that the PORTS are defined in iom328p.h and are only addresses of internal IO-registers within the microcontroller.

So, just declaring a virtual-Port is not that easy (maybe with a kind of memory mapping with something similar to std::mmap() but I've never tried this one).

Anyway you are a programmer, so there is a solution to almost everthing ;) I personally would suggest, to create your own Port-Class:

  • this class holds your required Pins as members and you have a setter, that overwrites your member-Pins according to the number you pass to it
  • (this code is not meant to be the 'perfect' solution, just a quick hint into the direction)

I would recommend you to stay with the arduino-library for this approach. If you do it with the plain PORTS, you might mess up something somewhere. So for example if you init your SerialPort and afterwards do something like PORTD |= (1<<PD0), you wont be able to receive any data and don't know why.

class MyPort
{
private:
  uint8_t m_pin[8];
public:
  MyPort(uint8_t pins[8])
  {
    for(int i=0; i<8; ++i)
    {
      m_pin[i] = pins[i];       //copy from constructor-argument into member-variable
      pinMode(pins[i], OUTPUT); //setting pin as OUTPUT
    }
  }

  void operator =(uint8_t val)
  {
    for(int i=0; i<8; ++i)
    {
      digitalWrite(m_pin[i], (val >> i)&1);
    }
  }
};

//            B0,B1,D2,D3,D4,D5,D6,D7
//             v  v  v  v  v  v  v  v
uint8_t pins[]{8, 9, 2, 3, 4, 5, 6, 7};
MyPort PORTX(pins);

void setup()
{
  PORTX = 0b11001100;
}

void loop()
{
  // put your main code here, to run repeatedly:
}

please note that you will have to override the other operators as well, if you want bitwise addressing on your own port too

like image 54
Tom Mekken Avatar answered Sep 27 '22 22:09

Tom Mekken