Register

Hey there,
this code is setting up the watchdog timer:

  //SETUP WATCHDOG TIMER
WDTCSR = (24);//change enable and WDE - also resets
WDTCSR = (33);//prescalers only - get rid of the WDE and WDCE bit
WDTCSR |= (1<<6);//enable interrupt mode

But what does it do? Which bits are set?

Best regards

MAurice W.

And I mean that I actually don't know how this kind of setting up bits works.

The first statement does nothing, since it's immediately overwritten by the second.
Edit: That would usually be true, but not in this case. There's magic afoot. (Thanks westfw, for enlightening me.)
See here: http://forum.arduino.cc/index.php?topic=422272.msg2909642#msg2909642

After the second statement, WDTCSR holds 0b00100001. (33)
After the third statement, WDTCSR holds 0b01100001, so bits 0, 5 and 6 are set.
Bits 0, 1, 2 and 5 set up the watchdog timer prescaler.
Bit 0 is WDP0
Bit 1 is WDP1
Bit 2 is WDP2
Bit 3 is WDE (Watchdog System Reset Enable)
Bit 4 is WDCE (Watchdog Change Enable)
Bit 5 is WDP3
Bit 6 is WDIE (Watchdog Interrupt Enable)
Bit 7 is WDIF (Watchdog Interrupt Flag)

The datasheet has more info, such as the actual timeouts for the various prescaler settings.

OldSteve got there first but this is what I was preparing for you.

It looks like someone has expanded the constants and lost the ‘meaningful’ names.

This
WDTCSR |= (1<<6);//enable interrupt mode

looks like it was originally
WDTCSR |= (1<<WDIE);//enable interrupt mode

anyway it is expanded to:

WDTCSR = WDTCSR | (1<<WDIE)

which is expanded to
WDTCSR = WDTCSR | (1<<6)

which is expanded to
WDTCSR = WDTCSR | b01000000

which means that bit 6 in register is ORed with 1 and has the value 1

6v6gt:
OldSteve got there first but this is what I was preparing for you.

It looks like someone has expanded the constants and lost the ‘meaningful’ names.

This
WDTCSR |= (1<<6);//enable interrupt mode

looks like it was originally
WDTCSR |= (1<<WDIE);//enable interrupt mode

anyway it is expanded to:

WDTCSR = WDTCSR | (1<<WDIE)

which is expanded to
WDTCSR = WDTCSR | (1<<6)

which is expanded to
WDTCSR = WDTCSR | b01000000

which means that bit 6 in register is ORed with 1 and has the value 1

It was a good idea to point out that this is what it should have looked like:-WDTCSR |= (1<<WDIE);

OldSteve:
After the second statement, WDTCSR holds 0b00100001. (33)

How do you get from 0b00100001 to 33?

sterretje:
How do you get from 0b00100001 to 33?

I don't know how OldSteve did it by I confirmed it with an online calculator, assuming the 33 was decimal. Or what should it be ?

sterretje:
How do you get from 0b00100001 to 33?

0b00100001 (binary) is 33 (decimal).

void setup()
{
    Serial.begin(115200);

    int val = 0b00100001;

    Serial.println(val); // Prints "33"
}

void loop(){}

A "0b" prefix indicates that the following value is binary.

Edit: Arduino will not accept just "b" as a prefix, as in b00100001
Edit2: I should add, it will accept "B" though, as in B00100001

If I'd meant hex earlier, I would have written 0x33. I used 33 (decimal) because that's how it was presented in the opening post.

Edit: Arduino will not accept just "b" as a prefix, as in b00100001

But it will with a capital B - B00100001

These are actually plenty of #define, it is not something the pre-processor decodes by itself

J-M-L:
But it will with a capital B - B00100001

Yep. I said that.

OldSteve:
Yep. I said that.

Guess I was typing this while you were editing :slight_smile:

J-M-L:
Guess I was typing this while you were editing :slight_smile:

Yeah. There was a 7 minute difference, but if you already had the page loaded, you would have missed my edit. All is forgiven. :smiley:

I'm on my phone and have been pretty slow trying to cut & paste stuff while eating breakfast :slight_smile: and added info that those are #definded not preprocessor math - focus on that part of my message :slight_smile:

Which means that this notation does not work for more than 8 bits by the way because they did not create the #define beyond all the 8 bits possibilities (with or without the leading 0)

J-M-L:
I'm on my phone and have been pretty slow trying to cut & paste stuff while eating breakfast :slight_smile:

I'm afraid I'm pretty bad when it comes to editing, too. I post a reply, but my brain keeps rolling over and I think of extra info I should have added, so edit the reply. :frowning:

Perhaps I should add a line to my signature: "Please reload before replying to my responses - I often edit." :smiley:

J-M-L:
Which means that this notation does not work for more than 8 bits by the way because they did not create the #define beyond all the 8 bits possibilities (with or without the leading 0)

It works for a 16 bit unsigned value:-

void setup()
{
    Serial.begin(115200);
    unsigned int val = 0b0100000000001001;
    Serial.println(val); // Prints "16393"
}

void loop(){}

Edit: (I'm doing it again)
Or for a two's complement 16-bit negative value:-

void setup()
{
    Serial.begin(115200);
    int val = 0b1011111111110111; // -16393
    Serial.println(val);
}

void loop(){}

OldSteve:
I’m afraid I’m pretty bad when it comes to editing, too. I post a reply, but my brain keeps rolling over and I think of extra info I should have added, so edit the reply. :frowning:

Perhaps I should add a line to my signature: “Please reload before replying to my responses - I often edit.” :smiley:

Yes, I’ve been caught like this before when someone has edited a post as I was replying.
I hate the sight of that yellow card appearing when I am previewing/submitting a post because it often means I have to reword my post so it fits in with the new context, but I agree it is useful. It would also be useful if something similar warned of an edit to an earlier post in the thread. YellowCard.PNG

oops - lost in context.. I meant B00001111 is the same in binary as B1111. The defines I think take this into account. Those are the leading 0 had in mind.

I don't think the standard has a way to create "binary literals" but , recent GCC and Clang support this feature using a syntax similar to the hex syntax, except it's b instead of x:

int foobar = 0b00100101; // will be calculated by pre-processor

6v6gt:
It would also be useful if something similar warned of an edit to an earlier post in the thread. YellowCard.PNG

I agree. That would be handy. It would stop me having to add that line to my signature, too. :slight_smile:

J-M-L:
oops - lost in context… I meant B00001111 is the same in binary as B1111. The defines I think take this into account. Those are the leading 0 had in mind.

I thought I might have misunderstood, but wasn’t sure, when you said “Which means that this notation does not work for more than 8 bits by the way”. (Hence my 16-bit examples.)

Anyway, it’s all clear for anyone reading this thread in the future now, after our additional info. :wink:

6v6gt:
I don't know how OldSteve did it by I confirmed it with an online calculator, assuming the 33 was decimal. Or what should it be ?

You're right; I was thinking hexadecimal :disappointed_relieved:

sterretje:
You're right; I was thinking hexadecimal :disappointed_relieved:

Normally I'd use hex myself, but since the opening post used decimal, I thought it was more suitable to stick with decimal 33, instead of 0x21, which might have confused him (or her).