I've banged my head against the wall for a while now and figure I need some help. I have two sets of code that should be identical in terms of behavior, but they are not. In the first case I "brute force" the code:
void SetupAndIntrClass::watchdogSetup() {
cli();
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
// WDTCSR = 1<<WDP3 | 1<<WDP0; /* 8.0 seconds */
//WDTCSR = 1<<WDP2 | 1<<WDP1 | 1<<WDP0; /* 2.0 seconds */
//WDTCSR = 1<<WDP2 | 1<<WDP1; /* 1.0 seconds */
//WDTCSR = 1<<WDP2 | 1<<WDP0; /* 0.5 seconds */
//WDTCSR = 1<<WDP2; /* 0.25 seconds */
WDTCSR = 1<<WDP1 | 1<<WDP0; /* 0.125 seconds */
//WDTCSR = 1<<WDP1; /* 64 ms */ // seems to have issues on prototype
//WDTCSR = 1<<WDP0; /* 32 ms */ // seems to have issues in lab
/* Enable the WD interrupt (note no reset). */
WDTCSR |= (1<<WDIE);
sei();
}
and in the other case, I try to streamline things with a if/else structure (or the commented out switch/case structure which also has a problem) as follows:
// prescale choices are (in ms) 32, 64, 125,250,500,1000,2000,8000
void SetupAndIntrClass::watchdogSetup(int prescale) {
cli();
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
// switch (prescale) {
// case 32:
// WDTCSR = 1<<WDP0; /* 32 ms */
// break;
// case 64:
// WDTCSR = 1<<WDP1; /* 64 ms */
// break;
// case 125:
// /* 0.125 seconds */
// WDTCSR = 1<<WDP1 | 1<<WDP0;
// break;
// case 250:
// WDTCSR = 1<<WDP2; /* 0.25 seconds */
// break;
// case 500:
// WDTCSR = 1<<WDP2 | 1<<WDP0; /* 0.5 seconds */
// break;
// case 1000:
// WDTCSR = 1<<WDP2 | 1<<WDP1; /* 1.0 seconds */
// break;
// case 2000:
// WDTCSR = 1<<WDP2 | 1<<WDP1 | 1<<WDP0; /* 2.0 seconds */
// break;
// case 8000:
// WDTCSR = 1<<WDP3 | 1<<WDP0; /* 8.0 seconds */
// break;
// }
if (prescale==32)
{
WDTCSR = 1<<WDP0; /* 32 ms */
}
else if (prescale==64)
{
WDTCSR = 1<<WDP1; /* 64 ms */
}
else if (prescale==125)
{
WDTCSR = 1<<WDP1 | 1<<WDP0;/* 0.125 seconds */
}
else if (prescale==250)
{
WDTCSR = 1<<WDP2; /* 0.25 seconds */
}
else if (prescale==500)
{
WDTCSR = 1<<WDP2 | 1<<WDP0; /* 0.5 seconds */
}
else if (prescale==1000)
{
WDTCSR = 1<<WDP2 | 1<<WDP1; /* 1.0 seconds */
}
else if (prescale==2000)
{
WDTCSR = 1<<WDP2 | 1<<WDP1 | 1<<WDP0; /* 2.0 seconds */
}
else
{
WDTCSR = 1<<WDP3 | 1<<WDP0; /* 8.0 seconds */
}
/* Enable the WD interrupt (note no reset). */
WDTCSR |= (1<<WDIE);
sei();
}
to call the first version of the function, I use
watchdogSetup();
for the second I use
watchdogSetup(125);
What am I missing???
I am using a ATMEGA328p in a standalone application. I should note that I declare the functions in the *.h files differently. For the first case I have
void watchdogSetup();
and for the second case I have
void watchdogSetup(int prescale);
Thanks in advance