Assign individual pins to individual bits of a variable

Hello all,

First let me say, if this has been covered in another post, please be kind enough to point me in the correct direction. I am using a Mega2560, at the moment I have no code snippets due to the fact I have only been using Arduinos for a short time, and know not where to start with this. Plus, there is over 24000 topics on this forum and finding my answer is a daunting task.

The problem:

I have and FPGA board that is reading 2 quadrature encoders from 2 motors on a robot project. The FPGA simply counts the pulses, increments or decrements a registry and outputs the data along a parallel data bus which consist of 32 bits. 16 for the LEFT and 16 for the RIGHT. The purpose of the Arduino is to do floating point math; I am using Verilog in the FPGA, and to keep things simple, I want to stick with integers within the FPGA.

What I want to do:

In another language that I used to use a person could assign a specific pin to a specific bit of a variable;

Example:

int var;

var.0 = PORTA.0  //which assigns PORT A pin 0 to bit 0 of the variable

The ports being used on my Arduino are not in "order" so to speak. As far as the IDE is concerned it's pins 22 thru 53.

Any help would be greatly appreciated.

First, you may want to use unsigned data types when you are trying to manipulate bits. Second, you can assign binary values to data using the B designator:

unsigned int b = B0000101;

This turns bits 0 and 2 on and those can have whatever interpretation you wish. Then I would search the web for bitwise operators in C. You will find that it's pretty easy to AND and OR data to get what you need.

Have you perused our Reference section at http://arduino.cc/en/Reference/HomePage?

Look at the functions under the heading "Bits and Bytes", as well as unde "Bitwise Operators".

Look at:
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-3/recipe-3-12

IMO it's a good book to have.

Ok,

Thanks everyone for your replies, you all have referenced some good material that I will refer to later. I understand how to use the Bit manipulate functions and understand bit-wise operators. I may have not been totally clear on what I was looking for.

For instance, pins 22 - 38, which are 16 input pins that I want to assign to a variable. When I want to read the state of all the pins at once I want to read the value from a variable and not have to check the state of all the pins individually. Pin 22 is the LSB of an INT variable, and PIN 38 is the MSB.

Does this make any sense to anyone?

Thank you.

Have a look at Arduino Reference - Arduino Reference for a general explanation of direct port manipulation but note that only 8 bit ports are available on most (? all) Arduinos.

Does this make any sense to anyone?

Yes, but it is not the sort of thing you can do with a micro controller.
It is the sort of thing you do every day with an FPGA but not with a micro unless the core is wraped in an FPGA. The only processor is know that does that is a Pcos from Cypress.

Hi Grumpy_Mike,
Actually, I'm not trying to flame you, but I use to this all the time PIC micro's. Of course, I had worked with them for years and basically knew their nomenclature in and out. I moved away from them due the limited resources on their 14 bit micros, plus the limited operations per cycle. For those interested, this is what I have figured out here in the past couple of days.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup(){
  
lcd.begin(16, 2);

PORTA = B11111111;    //sets all pins on PORT A to inputs
DDRA = B00000000;  //Pull ups and Pulls downs are disabled

}

void loop(){

uint8_t LEFT;

LEFT = PINA;

  lcd.begin(16, 2);
  lcd.print("LEFT");
  lcd.setCursor(5, 0);
  lcd.print(LEFT);
 
delay(300);  

}

This code allows me to assign the 8 bits of PORTA (Inputs) to a variable, and display the result to an LCD. I deciphered this from the datasheet of Atmega2560 and reading over other articles and post that refereed by members of the forum. As time goes along and as my time allows I will expand on this and submit my findings and code because it appears to be somewhat cryptic and hard to find examples on the net. Another note to keep in mind, this can be done in reverse also, a variable -> port.

It's kind of surprising to me that you're using an AVR chip to do floating-point math. Does the Mega even have an FPU?

This code allows me to assign the 8 bits of PORTA (Inputs) to a variable, and display the result to an LCD.

Yes but that is not what you asked.

What I want to do:
........ assign a specific pin to a specific bit of a variable;

Sure you can assign a single port to a variable but you can not assemble an arbitrarily collection of bits into one variable without what I said.

16 input pins that I want to assign to a variable. When I want to read the state of all the pins at once I want to read the value from a variable and not have to check the state of all the pins individually.

AFAIK no AVR will do this as they are 8-bit devices and therefore only have 8-bit ports. If you organise the hardware appropriately you can read 8 bits at a time so you can do the job with two reads and a shift, but not one read.

Or use a Due and read up to 32 bits at a time.

Does the Mega even have an FPU?

No Arduinos have FPU, unless maybe the new Tre/Gallileo does.


Rob

Could you do a pin-change interrupt on the 8-bit port, then in the ISR, determine which specific pin has changed and update the desired bit in a 16-bit global variable?

This is assuming that the AVR in the Mega is like the Uno (AVR328) and can have a pin-change interrupt applied to each port.

var.0 = PORTA.0

C officially does not support such things, although several microcontroller C compilers have added it as an extension.

It's probably important to note that even if this were implemented in the compiler, it would probably not produce any shorter or faster code the more traditional "bit manipulation" forms like "if (PORTB & 1<<PB4)" The AVR only has so much in the way of bit-manipulation instructions, and the compiler does know how to generate them.