Skip to content

Instantly share code, notes, and snippets.

@saper-2
Created January 30, 2019 20:40
Show Gist options
  • Save saper-2/8d8c1eaaefe0e15bbcfe016b90bbf0bc to your computer and use it in GitHub Desktop.
Save saper-2/8d8c1eaaefe0e15bbcfe016b90bbf0bc to your computer and use it in GitHub Desktop.
mega8 MCUCSR (MCUSR for mega328) test
/*
* 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"
uint8_t reg = 0;
int main(void)
{
reg = MCUCSR; // MCUSR for ATmega328
// led
PORTB |= 1<<0;
DDRB |= 1<<0;
// button
DDRC &= ~(1<<5);
PORTC |= 1<<5;
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
reg = MCUCSR;
usart_send_strP(PSTR("\r\nMCUCSR(1)="));
usart_send_hex_byte(reg);
wdt_disable();
while((PINC & (1<<5)) == 0) {
delay1ms(50);
PORTB ^= 0x01;
}
delay1ms(250);
reg = MCUCSR;
usart_send_strP(PSTR("\r\nMCUCSR(2)="));
usart_send_hex_byte(reg);
// enable WDT
wdt_enable(WDTO_250MS);
wdt_reset();
reg = MCUCSR;
usart_send_strP(PSTR("\r\nMCUCSR(3)="));
usart_send_hex_byte(reg);
while (1)
{
delay1ms(100);
PORTB ^= 0x01;
while((PINC & (1<<5)) == 0) {
delay1ms(20);
PORTB ^= 0x01;
}
wdt_reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment