Quicker way to calculate 8 bits to a byte

I have 8 different variables, they can only be 0 or 1.
And I convert them on this way to a byte.

CFR1_data_row1 = (LSB_first1)+
(SDIO_input_only
2)+
(CFR1_bit24)+
(EXTERNAL_powerdown_control
8)+
(AUXDAC_powerdown16)+
(REFCLK_input_powerdown
32)+
(DAC_powerdown64)+
(DIGITAL_powerdown
128);

Can this be done in a quicker way?

Yeay, by not storing them in a bool first :wink:

But if you insist, I would make it more readable by doing

CFR1_data_row1  = LSB_first;
CFR1_data_row1 |= SDIO_input_only << 2;
CFR1_data_row1 |= CFR1_bit2 << 3;
CFR1_data_row1 |= EXTERNAL_powerdown_control << 4;
CFR1_data_row1 |= AUXDAC_powerdown << 5;
CFR1_data_row1 |= REFCLK_input_powerdown << 6;
CFR1_data_row1 |= DAC_powerdown << 7;
CFR1_data_row1 |= DIGITAL_powerdown << 8;

Thank you, this is also what I was looking for.
For when interested, I have aprox. 10*32 bit to setup
a DDS. So this is a small part of it. Later in the programm
I go toggle one or more bits. To have the right function.

You could also use a struct and a union and let the code do all the work for you.

typedef struct regs
{
  byte A : 1; //LSB
  byte B : 1;
  byte C : 1;
  byte D : 1;
  byte E : 1;
  byte F : 1;
  byte G : 1;
  byte H : 1; //MSB
};

typedef union combine
{
  regs data;
  byte CFR1_data_row1;
};

void setup() {
  union combine myData;
  
  Serial.begin(115200);
  myData.data.A = 1;
  myData.data.G = 1;
  Serial.println(char(myData.CFR1_data_row1)); // shows 'A'
}

void loop() {
  // put your main code here, to run repeatedly:
}

http://cpp.sh/73zq

Well, seems nice to have a struct.
But sometimes I need to modify 1 bit
of a 32bit structure. And those 32bits
can only send in one instruction. When
interested, I programming an AD9910

pe1mxp:
|=

This is fine.

pe1mxp:
=|

This does not do what you want it to do.

Does the compiler know what to do with:

CFR1_data_row2 =| OSK_enable                          << 2;

when probably

CFR1_data_row2 |= OSK_enable                          << 2;

makes more sense.

econjack:
Does the compiler know what to do with:

CFR1_data_row2 =| OSK_enable                          << 2;

when probably

CFR1_data_row2 |= OSK_enable                          << 2;

makes more sense.

Yes, my fat fingers :wink: but I did it already for 4*32bits

But I still don't get it why you can't just store it in one byte in the first place... Making it a struc union is the most elegant way to have it stored as a byte but extract 1 bit out as if calling just a bool.

Well, in first instance I need it logical readable for less educated
people. So with less experience AVR and no experience with DDS.
To programm all registers in the DDS, I have a total of:
1x 16b register
10x 32b registers
16x 64b registers

Luckily I use a fraction of that, because the ram of 1Kb I don't use.
Until I go bitbang in parallel mode and doing RAMP modulation.

When the DDS is reset it has some "standard" bits set. So for my usage
I need to set some bits. It are only 3x 32b registers. But I can only
write a full fixed 32b register. In front of it, there comes a 8bit adress.

When the programm is what I want to have I go delete the things
I don't need. But I'm the only and first one who programming this
DDS in AVR. Therefor I need to play with all kind of settings.

So in all cases, when I change 1 bit in a register, I need to send
40 bits in one time.

For easy use I doing it now first on an Atmel. hope I can push those
bits a few microseconds. In first instance I should use an STM32, but
too less experience with this.

Normal, for speeding, they use an FPGA. Since the SCLK can handle
a maximum of 70Mbps. In this case I can send 40 bits in minimal 320ns.

If you read the datasheet of the AD9910 then will be a lot clear.

By the way, have already a programm written for an AD8950,
doing beaconing in several digital HAM-radio modes.

pe1mxp:
Well, in first instance I need it logical readable for less educated
people.

Why should they be modifying the code then? You could create a header file with the values in it, and have them edit that instead of mucking around in your code.

aarg:
Why should they be modifying the code then? You could create a header file with the values in it, and have them edit that instead of mucking around in your code.

First of all, it is open source. So other HAM-radio amateurs can copy the hardware
and do softwarechanges for their own purpose. Like callsign, locator, digital mode.
The idea from my project is coming from here OZ2M - Next Generation Beacons - AD9912 DDS
A lot of HAM's are interested, but don't have the knowledge for doing this. I have build
this before but then with a simple cheap AD9850 DDS. I have also 2 different programms.
One is for others, this is clean, neat and full with instructions with a header file where they
are allowed to change variables. Example is this http://1drv.ms/1CZbFAo
The other one is my experimenting programm. Developing and testing new
digital modes on the ionosphere, meteorite scatter, airplane scatter. and is a mess.
But, I am the 1st one developing this on this type of DDS. So to learn all the things
what I can do with that DDS I need to have fully access. Well, I can buy an evaluation
board from analog.com for US$500,- then are a lot of problems solved. But for less
then US$100, I build a complete GPS-controlled transmitter doing all waveforms.

pe1mxp:
First of all, it is open source. So other HAM-radio amateurs can copy the hardware
and do softwarechanges for their own purpose. Like callsign, locator, digital mode.

Yes, well, putting that stuff in a config file won't stop anyone from using the code any way they like, if they want to. My point was, if you don't, you're forcing them to do it. I don't think every ham would be really interested in that. If they are, they still can. Separating the config info actually opens it to a wider audience. It also allows you to code it without worrying about it without having to dumb it down.

That is in no way, inconsistent with the spirit of either open source or ham radio.

If you are doing it for yourself, it will simply save yourself a lot of aggravation later on when you need to change configurations as your experiment matures.

OP--I went to your web site and was stunned that I was able to download the entire 600+ pages of the copyrighted Arduino Cookbook. Unless you can show that you have rights to distribute that book, it would be a really good idea to remove it from your web site immediately.

septillion:
Yeay, by not storing them in a bool first :wink:

But if you insist, I would make it more readable by doing

CFR1_data_row1  = LSB_first;

CFR1_data_row1 |= SDIO_input_only << 2;
CFR1_data_row1 |= CFR1_bit2 << 3;
CFR1_data_row1 |= EXTERNAL_powerdown_control << 4;
CFR1_data_row1 |= AUXDAC_powerdown << 5;
CFR1_data_row1 |= REFCLK_input_powerdown << 6;
CFR1_data_row1 |= DAC_powerdown << 7;
CFR1_data_row1 |= DIGITAL_powerdown << 8;

The right way for 8-bit shifting:

CFR1_data_row4 = LSB_first ;
CFR1_data_row1 |= SDIO_input_only << 1;
CFR1_data_row1 |= CFR1_bit2 << 2;
CFR1_data_row1 |= EXTERNAL_powerdown_control << 3;
CFR1_data_row1 |= AUXDAC_powerdown << 4;
CFR1_data_row1 |= REFCLK_input_powerdown << 5;
CFR1_data_row1 |= DAC_powerdown << 6;
CFR1_data_row1 |= DIGITAL_powerdown << 7;

And this is for 32-bit SPI:

SPI.transfer(CFR1_addr);
SPI.transfer(CFR1_data_row1);
SPI.transfer(CFR1_data_row2);
SPI.transfer(CFR1_data_row3);
SPI.transfer(CFR1_data_row4);

OP: What? No explanation about the download at your web site?