How to put individual pins of a port in an array for reading?(Atmega 328)

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My pseudo-code looks like this inspired from this thread https://www.avrfreaks.net/forum/how-can-i-make-array-ports , obviously I need help to correct.

 

volatile uint8_t *a[] = { &PINB0, &PINB1 };

I intent to read the pins one by one using a loop and assign its value to a variable.

This topic has a solution.
Last Edited: Sun. Aug 9, 2020 - 12:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There was a long thread about exactly this in the last week. You basically want a struct to combine PIN register and pin-number pairs then iterate through it.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You either have a array of structs that hold address and pin-mask.   look up the struct, read the address and AND with the mask.

 

Or you have an array of numbers.  You lookup the number and then calculate address and mask from the number.

 

Both methods should work fairly well.   Especially if you make the numbers map nicely e.g. 0x12 where the 0x10 signifies PORTB and 0x02 signifies bit 2.    Look up the bit-mask from an array of masks.

 

But quite honestly,   the Arduino digitalRead() will work plenty good enough.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

There was a long thread about exactly this in the last week.


would you be so kind as to provide a link to that discussion?
Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

quote=david.prentice]

But quite honestly,   the Arduino digitalRead() will work plenty good enough.


Actually I'am trying to scan keyboard matrix, with minimum clock cycles. But I don't know if its redundant or I'm just increasing the cycles by using another method.

Last Edited: Sun. Aug 9, 2020 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

which kind of keyboard ? (4x4 , does it have diodes , should multi key press read correct, are there noise on the switches)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

achu wrote:
clawson wrote:

There was a long thread about exactly this in the last week.

would you be so kind as to provide a link to that discussion? Thanks

 

https://www.avrfreaks.net/forum/...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

achu wrote:

Actually I'am trying to scan keyboard matrix, with minimum clock cycles. But I don't know if its redundant or I'm just increasing the cycles by using another method.

SNAP: That was going to be my example - I've been digging out some example code.

 

With a matrix you have to scan the matrix driving each row (or column) low in turn. Your pin array probably just gets in the way rather than helping any.

 

Just go with the code driven approach. E.g.

KeyScan = 0;
if (IS_LEFTKEYDOWN())
    KeyScan |= (1<<LEFTKEY);
if (IS_RIGHTKEYDOWN())
    KeyScan |= (1<<RIGHTKEY);
if (IS_UPKEYDOWN())
    KeyScan |= (1<<UPKEY);
if (IS_DOWNKEYDOWN())
    KeyScan |= (1<<DOWNKEY);

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:

which kind of keyboard ? (4x4 , does it have diodes , should multi key press read correct, are there noise on the switches)

 

Yes, currently, a 4*4, keyboard with diodes for reading multiple key press. About the noise, I intent to use simple capacitor de-bouncing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Regarding pin reads.   A keypad has pins known at compile-time.   And almost certainly uses pins from only one or two PORTs.   e.g. rows on PORTB,  columns on PORTD

 

So you will probably just read PINB.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, thanks I will use that method. 

But can I ask another question?, instead of this code,

bool b = PINB & 0b00000100;

Can I use this or is it utterly meaningless?

bool b = PINB2;

On arudino IDE it seems to compile but does not work.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

No you can't say b = PINB2.

 

You must say:

bool b = PINB & (1<<PINB2);

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

achu wrote:

Can I use this or is it utterly meaningless?

bool b = PINB2;

It's not meaningless, it is legal code, but almost certainly fairly useless.

It creates a bool b with value 0 (false) if PINB2 is 0, or 1 (true) if PINB2 is any non zero value.

Assuming PINB2 comes from

#define PINB2 2

then you are saying

bool b= 2

and therfore creating a bool b with value 1 (true).

 

(If you are compiling as C code, this assumes your bool is a proper bool, aka _Bool, introduced with C99, where typically bool is used after including <stdbool.h> which has #define bool _Bool.

If compiling as C++, I think bool has always been a proper bool? but i don't know C++much).

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

achu wrote:
Can I use this or is it utterly meaningless?
you probably want to read...

 

https://www.avrfreaks.net/forum/...