Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

AVR Atmega 16 Watchdog Timer

Status
Not open for further replies.

abdoalghareeb

Member level 5
Joined
Feb 5, 2010
Messages
83
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,974

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//======================================================
//======================================================
#include <mega16.h>
#include <delay.h>
 
bit f=0;
 
#define     LED     PORTA.7
#define     sw1     PINA.0
#define     sw2     PINA.1
 
void main(void)
{
PORTA=0x03;
DDRA=0x80;
 
WDTCR = 0x0e;
 
while (1)
{ 
     
#asm("WDR")
 
if (~sw1){f=1;}
if (~sw2){f=0;  LED=0;}
 
if (f)
    {   
    while (1)   {  LED ^=1; delay_ms(100);  }
    } 
 
};
}
//======================================================
//======================================================


In this code I try to test Watchdog Timer in AVR atmega 16
by pressing sw1 the led start flashing and it will continue even if I press sw2 because the progress still in while(1) loop
so I guess the Watchdog Timer can solve this problem and reset the MC
that are my Watchdog Timer settings but they didn't work

thanks..........................................................................................
 
Last edited by a moderator:

abdoalghareeb said:
that are my Watchdog Timer settings but they didn't work
Just before while(1) line try this:
Code:
WDTCR = 0x1e;
WDTCR = 0x0e;

Please come back with the results.
 

Yes I do
I follow the post and my code become :

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//=========================================================
//=========================================================
#include <mega16.h>
#include <delay.h>
 
bit f=0;
 
#define     out     PORTA.7
#define     sw1     PINA.0
#define     sw2     PINA.1
 
void main(void)
{
PORTA=0x03;
DDRA=0x80;
 
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/1024k
#pragma optsize-
WDTCR=0x1E;
WDTCR=0x0E;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
 
while (1)
{      
#asm("wdr")
if (~sw1){f=1;}
if (~sw2){f=0;}
if (f)
    {        
    while (1)  { out ^=1;delay_ms(100); }
    }  
};
}
//=========================================================
//=========================================================



but the LED still flashing
 
Last edited by a moderator:

abdoalghareeb said:
but the LED still flashing
If you are using debugger, then you may have confirmed that no reset takes place. If not, then add a few seconds delay before while(1) to understand if a reset is actually done or not, because code enters inside blinking very fast. If you are using debugger place a breakpoint at the first line of main() to see if code hits it or not.
 
Last edited:

I think this is cause....

void delay_ms(unsigned int n)

generates a delay of n milliseconds.
This function automatically resets the wtachdog timer every 1ms by generating the wdr instruction.
 

The delay function doesn't reset the watchdog, it is just a delay.
What is the source of your quote?
 

He didn't answer if he is using a debugger. If so, then the solution could be found in a few minutes if not seconds (and not 5 months!!!), post #6 explains how to do so.

add a few seconds delay before while(1) to understand if a reset is actually done or not, because code enters inside blinking very fast
At least did you try that out? With so small time from reset to while(), you are not sure if the MCU resets or not. Don't trust what you see, visualization depends on the wathcdog timing as well. Add a delay of 2-3 seconds before while(). Then you will be 100% sure what is going on.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top