Hey guys!
I've been playing around with the arduino and its coding and in the process I came across a code with the following line.
DDRD = DDRD | B00111100;
The note beside it says -set direction bits for pins 2 to 5-
I understand the DDRD = DDRD part from a bit of googling, but I'm extremely confused at
B00111000 part. I'm guessing its binary but confused at the working out.
If I wanted to set something for pins like 13 to 10, how would I go about working out the binary for it?
Also, in a tutorial exercise i've been following, it says to show "the number of clock pulses" on the Arduino serial monitor. Anyone have a tutorial on how to actually go about this?
Yes. The | is the logical or operator. The statement is logically ORing B00111100 (which has bits 2, 3, 4, and 5 set) with DDRD, and storing the result back in DDRD. The result is that the bits 2 to 5 in DDRD are set (not necessarily changed) to 1 while the other bits are unchanged (not cleared and not set).
If I wanted to set something for pins like 13 to 10, how would I go about working out the binary for it?
Find the correct register name (it's not DDRD) and set the pins relative to the first pin on that port.
Also, in a tutorial exercise i've been following, it says to show "the number of clock pulses" on the Arduino serial monitor.
A link would be good.
Anyone have a tutorial on how to actually go about this?
Read the data sheet for the chip you have. All will be revealed.
But, really, why do you want to do this? There is an abstraction layer provided for a reason. Well, several reasons, really. Portability is just one of the reasons.
You will get information on constants including binary contants here;
As baegmon said you are performing a logical OR.
This has the advantage that bits already set will remain set.
You should also look at logical AND as a meant of unsetting bits,
A link to your tutorial would be a good idea.
An approximate way of getting the clock cycles would be to use the millis() function and multiply by the clock speed in cycles/millisecond.
Sorry, the tutorial is hosted at my university website and I think i'm not allowed to take a photo etc for copy right reasons. Its basically to simulate a binary counter
Find the correct register name (it's not DDRD) and set the pins relative to the first pin on that port.
Read the data sheet for the chip you have. All will be revealed.
But, really, why do you want to do this? There is an abstraction layer provided for a reason. Well, several reasons, really. Portability is just one of the reasons.
I read through the data chip and I'm kinda understanding it... but what do you mean by an abstraction layer?
Also, I checked some more tutorials regarding the binary numbers.
If i wanted to ie. set direction bits for pins 10 to 13 would this be the correct code?
DDRB = DDRB | B0011110000000000 ie. using 16 bit integer?
I'm sorry for being a complete noob, still trying to make sense of everything
Things like digitalRead(), digitalWrite(), pinMode(), etc. that manage all the pin to port stuff for you, in a portable way.
Also, I checked some more tutorials regarding the binary numbers.
If i wanted to ie. set direction bits for pins 10 to 13 would this be the correct code?
DDRB = DDRB | B0011110000000000 ie. using 16 bit integer?
No. DDRB is only 8 bits wide. Lop off the last 8 0s.
No. DDRB is only 8 bits wide. Lop off the last 8 0s.
Also if you follow the earlier link about constants you will see the 'B' format only works upto 8 bits. If you were going to use more you could use hexadecimal.
To understand what the DDRD register is and what it does, you need to refer to the datasheet for the ATMEL chip. It'll explain what the DDRD register is. What that function is basically saying in terms of the OR operator is...."If I want it on and it's not on, turn it on. If it's already on, stay on." That's a pretty weak explanation I know. But really, the best advice anyone can give you is...refer to the ATMEL data sheet. Sure you can google DDRD and you'll get, "DDRD - The Port D Data Direction Register - read/write" But that doesn't help you understand anymore what's going on. The datasheet will explain what 1011 and 1101 will do and so forth. Here's the link.