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 |
---|---|---|
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
Atmel® Studio
Integrated Development Environment |
| | | | |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Additional Files
Files |
---|
Connection Diagram
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
- Select ATMEGA328PB_HFO_WDT_Final.atsln from the downloaded project files.
- From the Build menu select 'Rebuild Solution'.
Select 'GCC C Executable Project' and give it the name Project1. Choose a location to save the project on your computer.
3
Task 3 - Programmer Settings
If debugWire is enabled, please disable it.
- Click Tools > Device Programming:
- Click the buttons which are marked as red arrows in the screenshot.
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 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
- 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.
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.
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.
- 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:
- 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:
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.