I am confused with the shifting of bits, please help me clarify my thoughts. If I do this code,
My questions is, by shifting SS, MOSI, SCK to the left by 1 isn't its value will now be
For SS: From 00010000 to 00100000
For MOSI: From 00100000 to 01000000
For SCK: From 10000000 to 00000000
Is this right? If I will apply "OR" with this values isn't it the final value of DDRB is this "01100000"?
If this is right, I believe this is wrong for what is suppose to be the value of DDRB which should be "10110000"
I think what it is saying is:
DDRB=(1<SS)|(1<<MOSI)|(1<<SCK);
meaning:
DDRB becomes equal to a value of
'1' shifted 4 times = 00010000 (SS is equal to 4)
ORed with
'1' shifted 5 times = 00100000 (MOSI is equal to 5)
ORed with
'1' shifted 7 times = 10000000 (SCK is equal to 7)
so the final value is 10110000.
Basically, it is using the signal names to specify the position within DDRB that bits should be set.
I think what it is saying is:
DDRB=(1<SS)|(1<<MOSI)|(1<<SCK);
meaning:
DDRB becomes equal to a value of
'1' shifted 4 times = 00010000 (SS is equal to 4)
ORed with
'1' shifted 5 times = 00100000 (MOSI is equal to 5)
ORed with
'1' shifted 7 times = 10000000 (SCK is equal to 7)
so the final value is 10110000.
Basically, it is using the signal names to specify the position within DDRB that bits should be set.
Thank you sir, but the issue of my confusion is how I interpret the shift.
for A,,B. (I get error when typing greater and less sign, so i type comma instead)
I thought it was shifting B to left A times, but it is really shifting A to the left B times.
What is being shifted goes first then << then the number of times it is shifted.
Examples:
0x05 << 1 = 0x0A
0x05 << 2 = 0x14
In your original code the name of the bit is associated with it's position in the register using the #define lines. The value finally in DDRB is the ORed combination of a 1 shifted into each of the named bit positions. It is a very useful way to write code because it lets you use signal (or pin) names instead of just numbers. So if you want to SET (make = 1) a bit you use:
[variable or register] |= (1 << [name of the bit or pin])
and to CLEAR (make = 0) a bit you use:
[variable or register] &= ~(1 << [name of the bit or pin])
So in your original code, if you wanted to make a pulse on the SCK pin of RB, you could use:
RB |= (1 << SCK);
RB &= ~(1 << SCK);