Estoy teniendo un problema en la ejecución de un deshabilitar el guardián de secuencia en un AVR ATtiny84A en realidad es resetear el chip, incluso a pesar de que el temporizador debe tener un montón de tiempo a la izquierda. Esto sucede de forma incoherente y cuando se ejecuta el mismo código en muchas partes físicas; algunos reset cada vez, algunos de reinicio a veces, y algunos nunca.
Para demostrar el problema, he escrito un sencillo programa que...
- Permite que el organismo de control con 1 segundo de tiempo de espera
- Restablece el watchdog
- Parpadea el LED blanco en 0,1 segundos
- Brilló el blanco LED apagado durante 0,1 segundos
- Desactiva el watchdog
El tiempo total entre el organismo de control habilitar y deshabilitar es menos de 0.3 segundos, sin embargo, a veces, un perro guardián de restablecimiento se produce cuando el deshabilitar la secuencia se ejecuta.
Aquí está el código:
#define F_CPU 1000000 // Name used by delay.h. We are running 1Mhz (default fuses)
#include <avr/io.h>
#include <util/delay.h>
#include <avr/wdt.h>
// White LED connected to pin 8 - PA5
#define WHITE_LED_PORT PORTA
#define WHITE_LED_DDR DDRA
#define WHITE_LED_BIT 5
// Red LED connected to pin 7 - PA6
#define RED_LED_PORT PORTA
#define RED_LED_DDR DDRA
#define RED_LED_BIT 6
int main(void)
{
// Set LED pins to output mode
RED_LED_DDR |= _BV(RED_LED_BIT);
WHITE_LED_DDR |= _BV(WHITE_LED_BIT);
// Are we coming out of a watchdog reset?
// WDRF: Watchdog Reset Flag
// This bit is set if a watchdog reset occurs. The bit is reset by a Power-on Reset, or by writing a
// logic zero to the flag
if (MCUSR & _BV(WDRF) ) {
// We should never get here!
// Light the RED led to show it happened
RED_LED_PORT |= _BV(RED_LED_BIT);
MCUCR = 0; // Clear the flag for next time
}
while(1)
{
// Enable a 1 second watchdog
wdt_enable( WDTO_1S );
wdt_reset(); // Not necessary since the enable macro does it, but just to be 100% sure
// Flash white LED for 0.1 second just so we know it is running
WHITE_LED_PORT |= _BV(WHITE_LED_BIT);
_delay_ms(100);
WHITE_LED_PORT &= ~_BV(WHITE_LED_BIT);
_delay_ms(100);
// Ok, when we get here, it has only been about 0.2 seconds since we reset the watchdog.
wdt_disable(); // Turn off the watchdog with plenty of time to spare.
}
}
En el inicio, el programa comprueba si la última puesta a cero fue causada por un organismo de control de tiempo de espera, y si es así, se enciende el LED rojo y se borra el guardián de restablecimiento de la bandera para indicar que un organismo de control reset sucedido. Creo que este código debe nunca ser ejecutado y el LED rojo debe nunca llegan, sin embargo, con frecuencia lo hace.
¿Qué está pasando aquí?