EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / AVR ATMEGA32 watchdog problem

AVR ATMEGA32 watchdog problem

|

Microcontroller › AVR › AVR ATMEGA32 watchdog problem

  • This topic has 1 reply, 2 voices, and was last updated 9 years, 1 month ago by GANEEV SINGH.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • March 19, 2011 at 11:24 am #807
    vikas
    Participant
    #include <avr/io.h>
    #include <avr/signal.h>
    #include <util/delay.h>
    #include <avr/eeprom.h>
     
    #define F_CPU 12000000UL
    void COUNT_PULSES(void);
    void PULSE_OUT(void);
     
    unsigned int PULSES1=0;
    float PULSES=45;
    float PULSES2=45;
    int FLAG=0;
    int FLAG1=0;
    char lcd_buffer[20];
    unsigned int ech EEMEM=0;
     
    int main()
    {
     
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;
    TCCR1A=0b00000000;
    TCCR1B=0b00000111;   //SELECT CLOCK HRER CS
      
    // Port A initialization
    PORTA=0x00; //ACD
    DDRA=0x00; //input
    // Port B initialization
    PORTB=0x00; //PUSH BUTTONS
    DDRB=0x00; //input
    // Port C initialization
    DDRC=0xFF; //LCD
    PORTC=0x00; //output
    // Port D initialization
    PORTD=0x00;
    DDRD=0x00; //INPUT
     
        // Watchdog Timer initialization
        // Watchdog Timer Prescaler: OSC/256k
     
      WDTCR=0b00011111;
    //WDTCR=0b00001111;
     
    _delay_ms(8000);
     
    TIMSK=0b00000100; 
      sei();
     
    //READ FROM THE 
     
    PULSES2 = (unsigned int) eeprom_read_word((uint16_t *) 10);
    _delay_us(20);
    WDTCR=0b00011111;
    WDTCR=0b00001111;
     
    while(1)
    {
     
    if((PINA & 0x01) != 0 && FLAG1==0 )
    {
    FLAG1=1;
    TCNT1=0;
    COUNT_PULSES();
    }
    else 
    {
    if((PINB & 0x01) != 0 && FLAG==0)
    {
    if((PINB & 0x01) != 0)
    {
    FLAG=1;
    PULSE_OUT();   //pulse out 1 or 2
    WDTCR=0b00011111;
    WDTCR=0b00001111;
     
    _delay_ms(100);
    FLAG=0;
    }
     
    }
    }
     
    _delay_ms(10);
     
    WDTCR=0b00011111;
    WDTCR=0b00001111;
    }
    return 0;
    }
     
     
     
    void COUNT_PULSES()
    {
       
       WDTCR=0b00011111;
    WDTCR=0b00001111;
     
    _delay_ms(1500);
    WDTCR=0b00011111;
    WDTCR=0b00001111;
     
    PULSES=TCNT1;
    PULSES2= PULSES;
    if (PULSES2 > 4)
      {
    eeprom_write_word ((uint16_t *) 10, (uint16_t)PULSES2); 
    _delay_us(20);
    }
    else
    {
    PULSES2 = (unsigned int) eeprom_read_word((uint16_t *) 10);
    _delay_us(20);
    }
     
    WDTCR=0b00011110;
    WDTCR=0b00001110;
     
    while((PINA & 0x01) != 0 )
    {
    _delay_ms(5);
    WDTCR=0b00011110;
    WDTCR=0b00001110;
     
    }
    FLAG1=0;
    }
     
     
     
     
    void PULSE_OUT()
    {
     
      
    PULSES2 = (unsigned int) eeprom_read_word((uint16_t *) 10);
    _delay_us(10);
    PULSES= PULSES2;
    WDTCR=0b00011111;
    WDTCR=0b00001111;
    if (PULSES == 0)
    {
    __asm__ __volatile__( “SBI  0x15,2” );     // 2 cycles
      
    _delay_us(10);
     
    __asm__ __volatile__( “CBI  0x15,2” );    // 2 cycles
    }
    else if (PULSES > 0 && PULSES < 10)
    {
        __asm__ __volatile__( “SBI  0x15,2” );    // 2 cycles
      
    _delay_us(20);
     
    __asm__ __volatile__( “CBI  0x15,2” );    // 2 cycles
    }
    else if (PULSES == 10)
    {
        __asm__ __volatile__( “SBI  0x15,2” );    // 2 cycles
      
    _delay_us(1);
     
    __asm__ __volatile__( “SBI  0x15,4” );    // 2 cycles
     
    _delay_us(20);
     
    __asm__ __volatile__( “CBI  0x15,2” );    // 2 cycles
    __asm__ __volatile__( “CBI  0x15,4” );    // 2 cycles

    }
                 else
                {
               __asm__ __volatile__( “SBI  0x15,2” );    // 2 cycles

      
    _delay_us(1);
     
    __asm__ __volatile__( “SBI  0x15,4” );    // 2 cycles
     
    _delay_us(20);
     
    __asm__ __volatile__( “CBI  0x15,2” );    // 2 cycles
    __asm__ __volatile__( “CBI  0x15,4” );    // 2 cycles

    }
    }



    I am not getting the pulses defined in the PULSE_OUT() function.
    Input to controllers are fine. Code is working accordingly without the watchdog.
    Watchdog is of 2 sec.
    EEPROM is working.
    With watchdog, just not getting pulses (in asm code pulse_out()).
    Can anybody trace the bug?

    Thanks
     

    January 22, 2017 at 3:45 pm #14356
    GANEEV SINGH
    Participant

    Hi Vikas 

    I do not see watchdog timer being disabled anywhere in this code. What you are doing is you are clearing off the WDTOE bit everytime you write the second command i.e. WDTCR=0b00001111; rather you should write WDTCR=0b00010111;

    For further understanding, I will suggest you to go through with Atmega32's datasheet and especially the WACHDOG TIMER stuff.

    Hope this helps :)

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • What diode should I use? March 7, 2026
  • LM016L + PCF8574 I2C Backpack Schematic with Backlight Control March 7, 2026
  • Expensive hobby March 7, 2026
  • USING EASYEDA BUT SOMETHING ISN'T RIGHT? March 7, 2026
  • What is this component and what is its function? March 6, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise