First I'll tackle what
TCCR0B=TCCR0B&0b11111000|0x01;
means from a pure programming perspective:
Let's break it down into parts:
TCCR0B = TCCR0B & 0b11111000 | 0x01;
TCCR0B
is a register. It could be any variable really - it's just "a value" in this context. Just because it happens to control one of the timers is besides the point.
0b11111000
is a binary representation of a value. It's the same as the hexadecimal number 0xF8
. F is 15. 15 in binary is 1111. 8 is 8 in both hexadecimal and decimal. 8 in binary is 1000. Put the two together and you have 11111000. Incidentally, in decimal that is 248.
0x01
is hexadecimal for 1 in decimal - it's also 1 in binary.
Between them are bitwise operators - &
is a bitwise AND
operation, and |
is a bitwise OR
operation. AND
calculates which bits in a pair of binary numbers are both on, and OR
calculates which bits in a pair of binary numbers are on in either of the numbers.
So take the first one first. Say the TCCR0B
register contains the value 0x6F
, which is 157 in decimal, or 01101111 in binary. The first operation is to AND
that with 11111000:
01101111
11111000 AND
--------
01101000
You can see that the last three bits of the value have been cleared because they aren't 1 in both the left and right operands to the &
operator.
Ok, so the next operation is to OR
it with 0x01:
01101000
00000001 OR
--------
01101001
Effectively the two operands have been overlaid on each other and any bits that are set in either operand will be reflected in the result.
So finally the value is then stored in TCCR0B
.
In summary that specific operation clears the lower three bits then sets the lowest bit of TCCR0B.
Now what that actually means with respect to what the TCCR0B register does can be gleaned from the datasheet for the chip. For instance, take the ATMega328P chip from the Arduino UNO board. Section 14.9.2 "TCCR0B – Timer/Counter Control Register B" of the datasheet describes that register in detail. The bit-wise overview looks like this:
Those bits all correspond 1:1 to the binary bits from above in the same order. So the first stage, clearing of the lowest three bits, sets CS02, CS01 and CS00 to 0. The second stage, ORing 0x01, then sets CS00 to 1.
The datasheet goes on to tell you what those mean:
- Bits 2:0 – CS02:0: Clock Select
The three Clock Select bits select the clock source to be used by the Timer/Counter.
And a table to show the different meanings of the possible values:
The value that has been masked and created for those three bits is, of course, 001, which in that table is "CLK I/O / (No prescaling)", which is the timer running at the frequency of the IO clock. If you were to set those bits to 010, you'd get "CLK I/O / 8 (From prescaler)". So your masking would change to:
TCCR0B = TCCR0B & 0b11111000 | 0x02;
Or more explicitly so you can see the bits easier:
TCCR0B = TCCR0B & 0b11111000 | 0b010;
As to what the "A" and "B" mean (TCCR0A and TCCR0B), there are two registers that are used to control the timer. They each serve a different purpose. TCCR0A has bits that control PWM, and TCCR0B has bits that control the timer itself.
Not to be confused with OCR0A and OCR0B which are two separate numeric values that are compared to the timer's counter value TCNT0 to provide two separate PWM outputs (OC0A as Uno pin D6, and OC0B as Uno pin D5) synchronised from the same timebase. (See sections 14.9.4 OCR0A – Output Compare Register A and 14.9.4 OCR0B – Output Compare Register B in the datasheet)
i already asked this and the answer was something like "clears the low-order 3 bits". Like i said, i'm not very familiar with c++, bits and other stuff regarding the arduino.
- OK, you don't understand what "clear the low-order 3 bits" does. Then in another comment you say:This part is actually easy to understand for me as i work alot with coding language. also you can find easely online hex to decimal converters and so on.
It isn't clear what you are asking. First you say you aren't familiar with C++ bits, then you say that's not what you are asking.If i don't know what i'm looking for i can't properly consult a 438 pages datasheet.
- you look at the part for Timer 1 (or Timer 2 etc.) which is only a few pages. Then you look at the page describing TCCR0B. That describes each of those 8 bits. Each bit has a name (eg. WGMC13, CS10). Underneath the text in the datasheets explains what those bits do. The libraries on the Arduino are to help people write code without reading datasheets. If you want to go further, you have to read them.