read a byte at a time

I have a set of 8 switches ( 4 handpad and 4 limit) connected to the 8 analog input pins of my nano. I want to grab all 8 bits at once as a digital byte rather than read them one bit at a time. I can easily set the pins to digital inputs with no trouble, but for simplicity and easy parsing, rather just have a byte. AFIK, the analog pins ought to be what we used to call something like port A. I've looked on the playground as well as on the forum search, but evidently I just cannot conjure up the right keywords! I'll bet it's simple, or at least it darn well ought to be, but I just cannot find it. Thanks in advance for the help! Don the noob

Would this work?

byte mySwitches = PINA; //however, this value may not be what you are expecting. It depends if the inputs are completely connected on port A or not

If you need, you can do some bit masks to get just the bits you want.

Port C has the analog input pins for a meg138/328 chip. And PINx is the command that reads a port, where x is the port name.

So:

byte mySwitches = PINC;

That assumes you have set the pins up as digital input pins via digital mode command.

Here is link where the ports are defined for a 168/328 chip. http://www.arduino.cc/playground/Learning/PortManipulation

Lefty

PORT works to read IO ports? :\

I have a set of 8 switches ( 4 handpad and 4 limit) connected to the 8 analog input pins of my nano.

Digital switches connected to analog pins. Why?

Which pins?

PaulS:

I have a set of 8 switches ( 4 handpad and 4 limit) connected to the 8 analog input pins of my nano.

Digital switches connected to analog pins. Why?

Because he doesn't need the analog pins but wants to get the inputs in a byte form?

The port assignments for the 328 and 168 are:

PORT   0   1   2   3   4   5   6   7
-------------------------------------
  B    8   9. 10. 11. 12  13
  C   A0  A1  A2  A3  A4  A5
  D   RX  TX   2   3.  4   5.  6.  7

The '.'s mark the PWM pins.

Its a little tricky to output a byte at a time in one write because the only 8 pin port includes RX and TX pins...

Where do the Nano's two extra analog pins fit on that chart? 6 & 7 on the C line?

PaulS: Where do the Nano's two extra analog pins fit on that chart? 6 & 7 on the C line?

There aren't any...

Just to be on the safe side, to read the analog inputs you can use:

byte mySwitches = PINC & 0b11111100;

There aren't any...

The Arduino team seems to be of a different opinion. http://arduino.cc/en/Main/ArduinoBoardNano

Analog Input Pins 8

bubulindo:

PaulS: Where do the Nano's two extra analog pins fit on that chart? 6 & 7 on the C line?

There aren't any...

Just to be on the safe side, to read the analog inputs you can use:

byte mySwitches = PINC & 0b11111100;

Actually the 328 in the surface mount 'AU' package does have fully 8 analog input pins on it's PORT C as shown in the schematic:

http://arduino.cc/en/uploads/Main/ArduinoNano30Schematic.pdf

Lefty

Thanks, everyone! I finally got off my lazy rear end and looked at the Nano schematic and the 328 data sheet. There are indeed 8 analog inputs, 6 on the C port and two that are not but appear to be "pure" analog. There are no ports that have 8 available bytes, because bits 1 and 2 on the only possible real 8-bit port are used for serial comms, So what a kick! If I want a "true" byte input, I'll have to use all analog reads, as was suggested. Actually, I'll use an SPI read and a serial shift register. Still faster than 8 sequential analog reads (?). I have to use the analog ports because I need 4 pins with 2 interrupts for two encoders, two PWM pins, and two direction pins to drive two motors, and I also need the serial interface to other programs running on a PC. Wow. I really had not looked at the pinouts and schematics. In the bad old days, I'd have used a 6502 or 8080 (shudder) or 6800 with a nice glue chip having three (3) real bidirectional one-byte ports. Of course, dead slow, 2 whole MHz, but you'd think with all the mod cons and teenyweeny packages . . . Guess I oughta look at the Mega! Thanks again for the advice Don

This code tests the Nano analog ports for digital use. switches and pullups were placed on all 8 analog pins. The binary print of PINC returned only six bits, and the two extra port prints returned 0 no matter what. I guess A6 and A7cannot be used as digital pins. The compiler/linker/loader did not complain. Hope this puts the topic to bed? Thanks Don

int extraSix = 20;
int extraSeven = 21;


void setup()
{
  Serial.begin (115200);
}

void loop()
{
  byte mySwitches = PINC;
  pinMode(extraSix,INPUT);
  pinMode(extraSeven,INPUT);
  Serial.println (mySwitches ,BIN);
  int Six = digitalRead(extraSix);
  int Seven = digitalRead(extraSeven);
  Serial.println(Six);
  Serial.println(Seven);

  delay(1000);
}

Hope this puts the topic to bed?

I'm not convinced yet, however I don't have a 328-AU based board to test it out. I think you have to do all the pin mode and pull-up commands, etc using direct port access rather then using the arduino abstracted pin commands.

Something like:

void setup()
{
  DDRC = 0x00;   // sets Arduino analog pins 0-7 as input pins
  PORTC = 0xFF; // enable internal pull-ups on analog pins 0-7. 
  Serial.begin (115200);
}

void loop()
{
  byte mySwitches = PINC;
  Serial.println (mySwitches ,BIN);
  delay(1000);
}

That should printout all 8 bits as high and if you jumper analog pins 0-7 one at a time to ground, you should see each bit printed as a 0. Again I don't have a 328 AU chip to test it out with, but perhaps someone can try it out and give us the results?

Lefty

I still have the test setup. I'll give it a try when i get back to the bench. Don

Hi Lefty: No luck. Still only 6 bits. The datasheet for the 328 etc. says that port C only has 7 bits for openers, and c6 is used for !reset. It's very hard to see where the extra two a/d pins are, but they ain't on portC. The 32 pin (nano) chip pinout has no port numbers, just adc6 and adc7, so apparently these two are internally programmed as single function pins. The data sheet is truly amazing, I don't know how the folks who set up the IDE ever made it through even the first iteration! I've chased this long enough, and considering the speed I need for the application, can live with reading as 4 digital ports for one peripheral and 4 analog ports for limits. The program you suggested only returned 6 bits and I can now see why; I think the IDE saved us. Hope the reset bit was not goofed in the upload. Thanks again to all of you. Don

Don't get me wrong here...

But what is this insistence about analog ports? You will be using it as a digital port. Just because you have the possibility to use that pin as an AD, doesn't mean you have to. In this case you can use it as a normal digital port and read data from there as we showed in our code examples.

There is two more AD channels in the AU package, but they are not connected to any ports. So my guess is that those pins don't have more than one function.

The reset bit can only be disabled through a fuse, so that should be ok.

Google some of bit operations in C and see how to combine all the inputs in one byte, or if you want to move faster while reading the digital ports, check how to mask and test bits in C. There are some Macros that are very used for that purpose.

Hi Bubulindo: I've tried digital functions on those 2 pins. Somehow they seem to be internally connected as analog ports, no matter what. I'll have to use D11 andD12, not really a problem. The PORTC analogs can be used for digital as you suggest. Thanks, all Don

buffler: Hi Lefty: No luck. Still only 6 bits. The datasheet for the 328 etc. says that port C only has 7 bits for openers, and c6 is used for !reset. It's very hard to see where the extra two a/d pins are, but they ain't on portC. The 32 pin (nano) chip pinout has no port numbers, just adc6 and adc7, so apparently these two are internally programmed as single function pins. The data sheet is truly amazing, I don't know how the folks who set up the IDE ever made it through even the first iteration! I've chased this long enough, and considering the speed I need for the application, can live with reading as 4 digital ports for one peripheral and 4 analog ports for limits. The program you suggested only returned 6 bits and I can now see why; I think the IDE saved us. Hope the reset bit was not goofed in the upload. Thanks again to all of you. Don

The ADC6 and ADC7 pins are connected to the ADC input multiplexer only. The other 6 inputs to the multiplexer are shared with port C bits 0..5. Only the surface mount version of the ATmega328 has the ADC6 and ADC7 pins, so the Arduino doesn't assume them, but some clone boards bring them out.

For completeness port B bits 6 and 7 are shared with the xtal oscillator pins, so aren't available in the Arduino (some configurations could use them if programmed to use the internal RC oscillator only, but the Arduino bootloader relies a clock accurate enough for serial access, which the internal RC clock isn't.)

MarkT:
For completeness port B bits 6 and 7 are shared with the xtal oscillator pins, so aren’t available in the Arduino (some configurations could use them if programmed to use the internal RC oscillator only, but the Arduino bootloader relies a clock accurate enough for serial access, which the internal RC clock isn’t.)

It’s not the best. But it is possible to use it without problems.