Most efficient way of analysing 64 bits

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

Hi Guys,

 

Looking for some inspiration.

 

Atmega328P connected to 4 tlc5940's providing 16 x 4 outputs.

 

The problem:

I want to add individual outputs to a group which may have 1-64 ports assigned by the user. User then just needs to say "group on" and the appropriate outputs are enabled 

 

Question: What's the most efficient way of manipulating/analysing the members?

I had thought of assigning 4 bytes and using the bits to determine if a port is a member which would work, but it means I have to loop though all 64 bits.  Any more efficient options especially as they're might be only a few set?

(I still need to send the data via SPI individually)

 

regards

 

 

 

 

 

Last Edited: Tue. May 11, 2021 - 03:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nikm wrote:
What's the most efficient (sic) way

"efficient" in terms of what, exactly?

  • execution time?
  • code size?
  • RAM usage?
  • programmer effort?
  • other ??

 

As always, it's a tradeoff - some of those things will be directly contrary to others.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good point.

 

Existing code is pretty tight, so a reasonable amount of RAM available so I guess execution time with minimum programmer effort :)

Is there a better way than looping 64 times?

 

I should add that I plan to have 16 groups in an array

 

regards

 

 

 

Last Edited: Tue. May 11, 2021 - 03:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm still not entirely clear what you're trying to do.

 

Maybe a diagram (or diagrams) to illustrate would help ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

easiest is to explain my current thoughts.  Hope this helps

 

regards

 

 

sudo code

 

// Group storage

volatile  uint8_t groups[16][4];

 

// Examine group 1

 

for(y = 0 to 3)

{

  if groups[1][y] > 0 then

       for(x =0 to 7)

           if(bit set) then ..

  }

 

 

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

but it means I have to loop though all 64 bits.

You can just OR the bits

 

You must already have 8 bytes that you send out for the current 64 channels on/off settings.

Now you add 8 bytes that have ones in the channels that define the group  (ex  000101101 01011110 000101101 11001010 000101101 000101101 111101011 000101101 ) ...these ones mark which of the 64 are the user group channels

You just OR the original 8 bytes with these...send out the 64 bits and those channels will now be ON.   So you have 8 OR operations to do (if using a higher language, it will do so for you)

To turn off those channels invert the 8 bytes, then do an AND operation...send the 64 bit result out & those channel will go OFF

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

u8[16][4] is 64 BYTES. Why are you using one whole byte for a 0/1 state?

 

My first thought when the thread title said "64 bits" was simply ONE uint64_t.

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

The first element of the array is the group number then the 4 bytes containing 64 bits.  I know it's a waste of 16 bytes, is there a better option?

 

regards

 

 

 

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

Hi,

 

Thanks for that, much appreciated 

 

regards

 

 

 

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

nikm wrote:
4 bytes containing 64 bits.
It's a very good trick if you are able to get 64 bits in 4 bytes. Usually 4 bytes will contain just 32 bits ?!?

 

Anyway if you have a small group number then a large bit string a struct would seem to be the obvious. Something like:

typdef struct {
    uint8_t groupNum;
    uint32_t the32bits;
} bitInfo_t;

bitInfo_t allData[4];

But if you want to split 64 bits into 4 lots of 16 bits (say) why do you need a "group number" anyway? Surely you just have an array of bit groups and the index is the "group number".

 

I'd take a step back and very clearly state exactly what info and how much of it you want to represent then it should be easy to specify a good way to store it and how to manipulate it.

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

clawson wrote:
Why are you using one whole byte for a 0/1 state?

If he uses bool type from <stdbool.h> then that is what will happen. Sometimes though what seems an obvious waste of memory can result in cleaner or faster code.

 

There is a saying: "Unused memory is wasted memory". I concur.

 

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

N.Winterbottom wrote:
what seems an obvious waste of memory can result in cleaner or faster code.

Indeed.

 

Hence #2.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
take a step back and very clearly state exactly what info and how much of it you want to represent

+1

 

See #4.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

little side step...

why are you using a PWM controller to do digital outputs?

the chip is a 16 channel 12bit PWm controller so for 1 chip you could do with a 16 location uint16 array. for each channel you then have 4 bits left to act as a status indicator.