Author Topic: Atmega164 spi byte string  (Read 1211 times)

0 Members and 1 Guest are viewing this topic.

Offline PferdTopic starter

  • Contributor
  • Posts: 38
  • Country: de
Atmega164 spi byte string
« on: November 13, 2021, 07:41:44 pm »
Hello Forum,

I am again in need of some programming advice... I have six shift registers type 74HC595 in series on the spi interface of an Atmega164. I wrote a send function which takes 6 bytes of data and shifts them out, after all 6 bytes are through the MCU latches the registers. This all works perfectly. The problem here is that I have 6-bit and 4-bit circuits connected to the shift registers parallel outputs, but for the sake of programming ease I want every value to be stored in a normal uint8_t variable in software. So I somehow need to cut down those variables to the information I need and then combine them again to the 8-bit b<tes I am then giving the SPI send function. Is there a clever way of bitmasking this or is there a trick in existence?
I´ll attach a sketch to hopefully give some additional help of what my circuit needs to achieve.
Thanks a lot in advance!:)
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12864
Re: Atmega164 spi byte string
« Reply #1 on: November 13, 2021, 08:41:02 pm »
No, as ATmegas don't have a barrel shifter.   Also that 4 bit chunk in the middle doesn't do you any favors if you want a neat solution like shift and mask in a loop.

If the hardware design isn't set in stone, your best option would be to rewire so each 6 bit chunk uses the same bits of each '595, preferably LSB aligned, then you only need to split and mask in the  two halves of the 4 bit chunk into the high bits of two of the bytes and mask in the five individual bits.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7770
  • Country: de
  • A qualified hobbyist ;)
Re: Atmega164 spi byte string
« Reply #2 on: November 13, 2021, 08:52:09 pm »
You could use an array of 6 bytes for the 595s and overlay a uint16_t and a uint32_t pointer based on the boundaries. If any of the bitfield bytes for driver/signal lines change then copy and shift the changed bitfield byte in a temporary variable, clear the corresponding bits in the overlay (AND ~bitmask) and copy the new bitfield by a simple OR. After that resend the array. Instead of changing single bitfield bytes in the overlay you could also build the complete 2 or 4 byte pack and copy it directly to the overlay.
« Last Edit: November 13, 2021, 09:28:39 pm by madires »
 
The following users thanked this post: Pferd


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf