Created
January 30, 2019 20:07
-
-
Save saper-2/543a5db42d31b6190eaa8d4339735845 to your computer and use it in GitHub Desktop.
Reset detection on ATMega8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* resetevents.c | |
* | |
* Created: 2019-01-30 20:43:30 | |
* Author : saper | |
*/ | |
#include <avr/io.h> | |
#include <inttypes.h> | |
//#include <avr/interrupt.h> | |
#include <avr/wdt.h> | |
#include <avr/pgmspace.h> | |
#include "lib/delay.h" | |
#include "lib/usart.h" | |
int main(void) | |
{ | |
uint8_t reg = 0; | |
reg = MCUCSR; // MCUSR for ATmega328 | |
// led | |
PORTB |= 1<<0; | |
DDRB |= 1<<0; | |
// button | |
DDRC &= ~(1<<5); | |
PORTC |= 1<<5; | |
// F_CPU=16MHz , UBRR=25 => 38400bps | |
usart_config(25, USART_TX_ENABLE | USART_RX_ENABLE , USART_MODE_8N1);//| USART_RX_INT_COMPLET | |
usart_send_strP(PSTR("\r\nSystem reset...\r\nMCUCSR=")); | |
usart_send_hex_byte(reg); | |
usart_send_char(' '); | |
usart_send_bin_byte(reg); | |
if ((reg & 0x01) == 0x01) { | |
usart_send_strP(PSTR("\r\nCPU reset from: Power-ON\r\n")); | |
} else if ((reg & 0x02) == 0x02) { | |
usart_send_strP(PSTR("\r\nCPU reset from: RESET pin\r\n")); | |
} else if ((reg & 0x04) == 0x04) { | |
usart_send_strP(PSTR("\r\nCPU reset from: Brown-out (under/over voltage)\r\n")); | |
} else if ((reg & 0x08) == 0x08) { | |
usart_send_strP(PSTR("\r\nCPU reset from: WATCHDOG\r\n")); | |
} | |
// zero MCUCSR | |
MCUCSR = 0; // MCUSR for ATmega328 | |
// disable wdt to let user relese button | |
wdt_disable(); | |
while((PINC & (1<<5)) == 0) { | |
delay1ms(50); | |
PORTB ^= 0x01; | |
} | |
delay1ms(250); | |
// enable WDT | |
wdt_enable(WDTO_250MS); | |
wdt_reset(); | |
while (1) | |
{ | |
delay1ms(100); | |
PORTB ^= 0x01; | |
while((PINC & (1<<5)) == 0) { | |
delay1ms(20); | |
PORTB ^= 0x01; | |
} | |
wdt_reset(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<0> | |
System reset... | |
MCUCSR=01 00000001 | |
CPU reset from: Power-ON | |
<0> | |
System reset... | |
MCUCSR=02 00000010 | |
CPU reset from: RESET pin | |
<0> | |
System reset... | |
MCUCSR=08 00001000 | |
CPU reset from: WATCHDOG | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment