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
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.
I'm on my phone and have been pretty slow trying to cut & paste stuff while eating breakfast and added info that those are #definded not preprocessor math - focus on that part of my message
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
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.
Perhaps I should add a line to my signature: "Please reload before replying to my responses - I often edit."
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.
Perhaps I should add a line to my signature: “Please reload before replying to my responses - I often edit.”
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.
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.
I agree. That would be handy. It would stop me having to add that line to my signature, too.
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.
sterretje:
You're right; I was thinking hexadecimal
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).