AVR® Reset Sources Example Project

 Objective

This project goes through several different reset conditions: Power On Reset (POR), Brown Out Reset (BOR) and Watch Dog Timer (WDT) timeout, and shows how each one works on a 328PB Xplained board. Some external circuitry is shown to produce a variable voltage input but an adjustable power supply will also work.

For more details on the AVR® Reset Sources visit AVR Reset Sources Overview.

 Materials

Hardware Tools (Optional)

Tool About Purchase
ATmega328PB-XplainedMini-50px.png
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

Additional Files

Files

 Connection Diagram

project1.png

 Procedure

1

Task 1 - Project Open and Build

  • Download the project source files from the Exercise Files section above and unzip it to your computer.
  • Open Atmel Studio 7
  • Select File > Open > Project/Solution
step1.png
  • Select ATMEGA328PB_HFO_WDT_Final.atsln from the downloaded project files.
  • From the Build menu select 'Rebuild Solution'.
step2.png

Select 'GCC C Executable Project' and give it the name Project1. Choose a location to save the project on your computer.

2

Task 2 - Board Preparation

Make sure the USB Cable is connected between the board and the PC.

step3.png

3

Task 3 - Programmer Settings

If debugWire is enabled, please disable it.

  • Click Tools > Device Programming:
step4.png
  • Click the buttons which are marked as red arrows in the screenshot.
step5.png

4

Task 4 - Programming the Fuse Bits

  • Select the 'Fuses' options in the left option bar and set the bits as shown in the figure, which will enable CFD, BOD threshold as 2.7 V, and External Clock.
step6.png

5

Task 5 - Device Programming

Select the 'Memories' options in the left option bar and finish the steps as shown in the figure, which programs the compiled binary file into ATMEGA328PB.

step7.png

6

Task 6 - Project Code

Here is the full code that we reference.
You can also download this in the Exercise Section.

/*
* ATMEGA328PB_HFO_WDT_Final.c
*
* Created: 2017/03/08 17:15:35
* Author : A17582
*/

#define F_CPU 8000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>

//initialize watchdog
void WDT_Init(void)
{
    //disable interrupts
    cli();
    WDTCSR   =   (1<<WDCE )|(1<<WDE  );                     // Enable configuration change.
    WDTCSR   =  (1<<WDIE)|                                    // Enable Watchdog Interrupt Mode.
    (1<<WDCE )|(1<<WDE  )|                                    // Enable Watchdog System Reset Mode if unintentionally enabled.
    (0<<WDP3 )|(1<<WDP2 )|(1<<WDP1)|(1<<WDP0);                // Set Watchdog Timeout period to 4.0 sec.    
    //Enable global interrupts
    sei();
}

//Watchdog timeout ISR
ISR(WDT_vect)
{
    //Burst of  0.1Hz pulses
    for (uint8_t i=0;i<4;i++)
    {
        //LED OFF
        PORTB &= ~(1 << PINB5);                    // Set PORTB5 Low
        _delay_ms(80);
        //LED ON
        PORTB |= (1 << PINB5);                    // Set PORTB5 On
        _delay_ms(20);
    }
}

#define  BORFbit 2
#define  PORFbit 0

int main(void)
{
    unsigned char i;
    DDRB |= (1 << PINB5);                        // Set PORTB5 as output ,
    DDRB &= ~(1<<PINB7);                        //Set PORTB7 as input

    if(MCUSR & 1 ){
        MCUSR=0;        
        for ( i=0;i<4;i++)
        {
            //LED OFF    
            PORTB &= ~(1 << PINB5);                // Set PORTB5 Low
            _delay_ms(300);
            //LED ON
            PORTB |= (1 << PINB5);                // Set PORTB5 On
            _delay_ms(300);
        }

    }
    else if(MCUSR  & 4) {
        MCUSR=0;
        for (i=0;i<8;i++)
        {
            //LED OFF
            PORTB &= ~(1 << PINB5);                // Set PORTB5 Low
            _delay_ms(100);
            //LED ON
            PORTB |= (1 << PINB5);                // Set PORTB5 On
            _delay_ms(100);
        }
    }
    else if(MCUSR  & 8) {
        MCUSR=0;
        WDT_Init();
        for (i=0;i<8;i++)
        {
            wdt_reset();
            //LED OFF
            PORTB &= ~(1 << PINB5);                // Set PORTB5 Low
            _delay_ms(20);
            //LED ON
            PORTB |= (1 << PINB5);                // Set PORTB5 On
            _delay_ms(80);
        }
        MCUSR=0;
    }

    //initialize watchdog
    WDT_Init();

    //delay to detect reset
    //_delay_ms(500);

    while(1){

        PORTB |= (1 << PINB5);                    // Set PORTB5 high
        _delay_ms(250);
        _delay_ms(250);
        _delay_ms(250);
        _delay_ms(250);

        PORTB &= ~(1 << PINB5);                    // Set PORTB5 Low
        _delay_ms(250);
        _delay_ms(250);
        _delay_ms(250);
        _delay_ms(250);

        if((PINB&1<<PINB7)==0){
            PORTB |= (1 << PINB5);                // Set PORTB5 high
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);
            _delay_ms(500);

        }
        wdt_reset();
    }

}

7

Task 7 - Reset Test Circuit

  • Build the Reset test circuit shown in the schematic.
step12.png

7

Task 8 - POR Test

  • Pull out the USB Cable
  • Make sure the External Adjustable VCC Cable’s switch is at right side (3.2 V)
  • Clip The External adjustable VCC cable to the board
step9.png
  • Result: The orange LED will respond by flashing (0.3 ms, four times) based on the code section below because the POR reset is detected.
step10.png

9

Task 9 - BOR Test

  • After the POR test, slide the switch on the External Adjustable VCC Cable to the left side (2.5 V)
  • Slide the switch on the External Adjustable VCC Cable back to the right side (3.2 V)
  • Result: the orange LED will respond by flashing (0.1 ms, eight times) based on the code highlighted below because the BOR reset is detected.
step13.png

10

Task 10 - WDT Test

  • Push the Button on the board for about 1 second and then release the button. Extended delays will be triggered in the main routine, which will cause a WDT timeout.

Note: The WDT timer is set to 2 seconds.

step14.png
  • Result: the orange LED will respond by fast flashing (0.02 ms on, 0.08 ms off, four times) at first as the WDT interrupt ISR:
step15.png
  • Result: the orange LED will again respond by fast flashing (0.02 ms on, 0.08 ms off, four times) as the WDT reset is detected:
step16.png

 Analysis

The project shows three ways that a reset can occur: POR, BOR and WDT Timeout. Each has a unique application and can all run in the same application. The code examples are just a reference to how these types of resets can be set up and implemented.

 Conclusions

The project helps explain how the reset circuitry within the AVR functions and how to implement it. The code section can be reused in future applications that may require a similar reset structure. By no means is this the only way to design resets in the AVR device, this is just a simple sample project that helps explain the operation and allows you to apply your knowledge and understanding of the reset structure to a specific application.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.