Mahesha Padyana
Mahesha Padyana

Reputation: 431

Realization of Truth table in C

I want to set various clock sources in a function as per the truth table below. Basically I want to write to the TCCR0B register(Atmega328) according to the parameter I pass to setClockSource function. The image of the table and registers is given below. Register and Modes I am not able to makeout how best it can be done. I thought of using enum for various modes as below.

    enum CLOCK_SOURCE{
      NO_CLOCK_SOURCE=0x0;
      NO_PRESCALING=0x01;
      CLK_8=0x02;
     // and so on
}

But the problem is in setClockSource() function, how should I write to TCCR0B register without affecting bits 3-7? Shall I first clear last 3 bits and then OR TIMER_MODE values with TCCR0B? Without clearing, I may not guarantee the correct values for last 3 bits I guess. What is the efficient way?

void setClockSource (enum CLOCK_SOURCE clockSource)
{
    TCCR0B&=0xF8;    // First Clear last 3 bits
    TCCR0B|=clockSource;
}

Do we have library functions available to set clock source? I am using Atmega studio

Upvotes: 0

Views: 176

Answers (1)

norekhov
norekhov

Reputation: 4318

Do it like this:

void setClockSource (CLOCK_SOURCE clockSource)
{
    TCCR0B = TCCR0B & 0xF8 | clockSource;
}

Thus you will keep high bits and set lower bits.

Upvotes: 1

Related Questions