0
\$\begingroup\$

I'm writing a code to enable and disable the watchdog timer in ATmega32. I'm using the function in the datasheet that disables the wdt. Also, I'm toggling led in the beginning of the code to know if the uC resets or not. The led keeps toggling which means that wdt is not off and the uC resets over and over.

This is my code

typedef unsigned char u8;
typedef unsigned long u32;

#define DDRA    *( (volatile u8 *) 0x3A )
#define PORTA   *( (volatile u8 *) 0x3B )
#define WDTCR   *( (volatile u8 *) 0x41 )
#define MCUCSR  *( (volatile u8 *) 0x54 )
#define SREG    *( (volatile u8 *) 0x5F )

// delay about one second
void
delayOneSecond( void )
{
    u32 i;
    for( i = 0; i <= 70e3; i++ )
    {
        asm("NOP");
    }
}

// toggle PA0 pin
void
toggleLed( void )
{
    // PA0 direction output
    DDRA |= 0x01;

    // PA0 high, delay 1 sec
    PORTA |= 0x01;
    delayOneSecond();

    // PA0 low, delay 1 sec
    PORTA &= ~(0x01);
    delayOneSecond();
}

// disable watchdog function (from datasheet)
void WDT_off(void)
{
    // clear global interrupt
    SREG &= ~(1<<7);
    /* Write logical one to WDTOE and WDE */
    WDTCR |= (1<<3) | (1<<4);
    /* Turn off WDT */
    WDTCR = 0x00;
    // set global interrupt
    SREG |= (1<<7);
}

int main( void )
{
    MCUCSR &= ~(0x1F);      // clear reset flags
    toggleLed();
    WDTCR = 7;              // watchdog prescaler
    WDTCR |= (1<<3);        // watchdog enable

    WDT_off();

    while( 1 ) {}

    return 0;
}

I have tried many things to disable wdt and none of them worked. I've read that wdt is affected by the fuse bits and the bootloader, but I don't know much about them.

uC: AVR ATmega32, toolchain: AVR-GCC, IDE: Eclipse.

Any help would be appreciated. Thanks in advance!

\$\endgroup\$
  • 1
    \$\begingroup\$ the posted code fails to reset the watchdog, just before turning it off. Typically by executing the wdr instruction or calling: _WDR(); \$\endgroup\$ – user3629249 May 4 at 23:29
  • \$\begingroup\$ @user3629249 I used asm("WDR"); just befor turning off in WDT_off() function and it keeps resetting the uC, nothing changed. \$\endgroup\$ – Gamal Othman May 5 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.