I'm super new to this, and I've just been following tutorials. And checking the reference for whatever I don't understand. I've doing mostly alright, except when I see these codes that I can't seem to find in the reference section. From what I gathered online they are bitwise operators, and they are old school C stuffs? I really don't know what I'm talking about, lol, so someone please help!
The following a bits and pieces I've encountered so far:
Registers defined for the particular Arduino the code is for. You could look at the Atmel data sheet for your particular Arduino to learn what the registers control, and what the values mean.
Those particular statements are setting up timer events.
the defines are helper macros to set, clear, check and toggle a specific bit in a number
TCCR1A, et al are register names for the special registers in the AVR microprocessor. They are defined deep in the IDE header in the headers that AVR supplies. A decent IDE could show you where in the bowels of the includes these definitions show up.
Those macros are written like functions. You can use them like normal C functions. If you want to set the 5th bit in integer MyNum to a 1, call SET(MyNum, 4); Remember programmers count from zero.
CLR "clears" a bit by setting it to 0.
CHK checks if a bit is set. It will result in true if that bit is 1.
TOG toggles a bit. If it was 1 then now it is 0 and if it was 0 then it is set to 1. This may be the least useful one of the four.
#define macros are still textual substitutions. For example, suppose you have the following fragment:
#define ELEMENTCOUNT(x) (sizeof(x) / sizeof(x[0])
int myArray[20];
// bunch of code...
int i;
for (i = 0; i < ELEMENTCOUNT(myArray); i++) {
// more code...
When the compiler first sees the source code of the for loop, it makes a textual substitution so it effectively becomes;
for (i = 0; i < (sizeof(myArray) / sizeof(myArray[0]); i++) {
The expression sizeof(myArray) resolves to 40 (i.e., 20 ints at 2 bytes each) while the expression sizeof(myArray[0]) resolves to 2 (i.e., each int is two bytes of memory). Therefore, we get:
for (i = 0; i < (40/ 2); i++) {
which becomes:
for (i = 0; i < (20); i++) {
Therefore, this "parametized" macro takes a single argument for the name of the array for which you want to know the number of elements in the array. The advantage with the macro is that, if you change the size of the array, the parametized macro makes the proper adjustments in the source code automatically. The other advantage is that the macro is "typeless". That is, it works for any resolved data type. Finally, it allows you to get rid of "magic numbers" in expressions like:
for (i = 0; i < 20; i++) {
The down side of a parametized macro is that its code is duplicated everywhere it is used, as contrasted to a function call.
The pre-processor will replace the line "SET(myVar, 4);" with:
myVar |=(1<<4)
i.e. it sets myVar equal to 1 shift left 4 bits, which gives 16
which will have the same result as writing:
myVar = 16;
This is a simple means of setting a single bit within a variable. The others are similar except CLR clears a ginel bit, CHK tests if a particular bit is set, and TOG toggles a specific bit, either from 0 to 1, or 1 to 0.
The other point is that Macros can be resolved at compile time. in econjack's example, the compiler does the multiplication, and replaces the expression with a single constant. If you used a function, it might not do that.
evilbenito:
I'm super new to this, and I've just been following tutorials. And checking the reference for whatever I don't understand. I've doing mostly alright, except when I see these codes that I can't seem to find in the reference section. From what I gathered online they are bitwise operators, and they are old school C stuffs? I really don't know what I'm talking about, lol, so someone please help!
The following a bits and pieces I've encountered so far:
What are these? they look like variables, but no where in the code are these declared
Hi!
(1)
the first line defines a SET(x, y) that can be used in your program afterwards.
It belongs to the Bitwise operation in C.
For example, y =3 and if there is an 8 bit register whose value is 0000 0001, "1<<y" causes the bits to be shifted to the left by y =3, that is to say 0000 1000.
"|=" means x = x | (1<<y), that is to say x | 0000 1000.