Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] atemga328p timer0 TCCR0A, TCCR0B bits configuration

Status
Not open for further replies.

codemaster11

Advanced Member level 4
Joined
Sep 2, 2019
Messages
115
Helped
9
Reputation
18
Reaction score
7
Trophy points
18
Activity points
1,218
need help with below lines of code

1) TCCR0A = 0;// set entire TCCR0A register to 0.
2) TCCR0B = 0;// set entire TCCR0B register to 0.

understand line 1 & 2 that both registers are set to 0.

but not clear how the CTC mode turn on with code used in line#3. as the entire TCCR0A = 0x00;
for CTC mode WGM01 = 1; but i don't know the default WGM01 bit state. if WGM01 in TCCR0A is 0
then "how the left bits shift operator along with bitwise OR operator change WGM01 = 0 to WGM01 = 1" which is for CTC mode turning on.

same is the case for below line#4 h registers are set to = 0x00;

// turn on CTC mode
3) TCCR0A |= (1 << WGM01);

// Set CS01 and CS00 bits for 64 prescaler
4) TCCR0B |= (1 << CS01) | (1 << CS00);

// enable timer compare interrupt
5) TIMSK0 |= (1 << OCIE0A);
 

Solution
It's not the same. Notice the |= (or) operation. The difference matters if you want to set more than one bit.
that's right . post#4 & 5 has solve my problem of defining bit state using bitwise shift << along with bitwise OR operators.

thanks
WGM01, CS01, CS00, and OCIE0A are names of individual bits in the corresponding 8 bit registers. Each of these bits is defined as a number from 0 to 7 that corresponds to the bit in the register it represents. This is defined in an #include file for the appropriate microcontroller. So shifting 1, WGM01 times places a 1 in that bit location in the TCCR0A register. If one looks at the assembler code that the C compiler generates from this line, it will show that it may have used a set bit instruction to set the bit number assigned to WGM01, rather than using a shift instruction. Using shift is C's way of saying to set an individual bit.
 

    codemaster11

    Points: 2
    Helpful Answer Positive Rating
So shifting 1, WGM01 times places a 1 in that bit location in the TCCR0A register. If one looks at the assembler code that the C compiler generates from this line, it will show that it may have used a set bit instruction to set the bit number assigned to WGM01, rather than using a shift instruction. Using shift is C's way of saying to set an individual bit.
it tells us that shifting a bit 1 place in C language set the corresponding bit in timer0A or timer0B
mean that "TCCR0A |= (1 << WGM01)" set it to WGM01 = 1; if this the case will it may not be more simple to
code it as TCCR0A = 0x02; ?
Capture.JPG
 
Last edited:

Yes, it is more simple. The trade off is that using the shift method with the name of the bit shows the name of the bits you are setting. One could include the explanation in comments instead to indicate this.
 

    codemaster11

    Points: 2
    Helpful Answer Positive Rating
"TCCR0A |= (1 << WGM01)" set it to WGM01 = 1; if this the case will it may not be more simple to
code it as TCCR0A = 0x02; ?
It's not the same. Notice the |= (or) operation. The difference matters if you want to set more than one bit.

(1 << WGM01) is evaluated at compile time and is identical to 0x02, just different ways to specify a literal (a constant value).
 

    codemaster11

    Points: 2
    Helpful Answer Positive Rating
It's not the same. Notice the |= (or) operation. The difference matters if you want to set more than one bit.
that's right . post#4 & 5 has solve my problem of defining bit state using bitwise shift << along with bitwise OR operators.

thanks
 

Solution
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top