Someone please help me understand these few lines of codes.

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:

#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+

I know the #define is similar to const, but what is a bit set/clear macro? also what does -+ mean in the comment?

TCCR1A=0b10000010;
TCCR1B=0b00011001;
ICR1=110;
OCR1A=55;

What are these? they look like variables, but no where in the code are these declared

but what is a bit set/clear macro?

That is a comment.

also what does -+ mean in the comment?

Random fingering of keys.

What are these?

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.

#define SET(x,y) (x |=(1<<y))
#define CLR(x,y) (x &= (~(1<<y)))
#define CHK(x,y) (x & (1<<y))
#define TOG(x,y) (x^=(1<<y))

What does SET CLR CHK TOG do?

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.

I suspect it would be useful if someone could explain how any one of the macros works and how you use it in a program.

I don't feel confident to do so myself.

...R

#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.

As one example:

#define SET(x,y) (x |=(1<<y))

If, somewhere in your code, you use:

int myVar = 0;
...
SET(myVar, 4);

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.

Regards,
Ray L.

Ray: wouldn't the statement:

SET(x, 4);

have to be used instead as

SET(myVar, 4);

to work as stated in your example?

econjack:
Ray: wouldn't the statement:

SET(x, 4);

have to be used instead as

SET(myVar, 4);

to work as stated in your example?

Typo. Fixed.

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.

Is there a difference between:
SET(val,bit)
and
bitSet(val,bit) ?

One is an "official" Arduino function, but it really depends on how they are defined.

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:

#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+

I know the #define is similar to const, but what is a bit set/clear macro? also what does -+ mean in the comment?

TCCR1A=0b10000010;
TCCR1B=0b00011001;
ICR1=110;
OCR1A=55;

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.

Other lines can also be explained in this way.

(2)
the second part:

TCCR1A=0b10000010;
TCCR1B=0b00011001;
ICR1=110;
OCR1A=55;

TCCR1A and TCCR1B are respectively the high 8 bit register and low 8 bit register of the Timer1 in the microprocessor.

This block of code means that using the fast PWM mode to generate PWM signal with a duty cycle of 55/110 = 50%.

Best regards,

@gaojie058
Do you really think that the OP is going to care about your answer 4 years after the last activity in this post?