问题描述
我给出了这个信息 - Atmega128和Pic24e在程序地址0x0处有复位中断,编写一个函数reset()来重置一个程序,我也听说过一个简单的方法来强制你的系统重新启动,就是把它发送到一个无限循环中。
打电话给我很疯狂,但是这很简单:
function reset(){
goto 0x00000000;
}
不确定您是否可以这样做,没有寻找一个复杂的解释,因为我敢肯定这是一个通用的问题。如果你可以的话,简短而又甜蜜:) p> goto 0x00000000
启动程序结束,但所有SFR未初始化,且中断未初始化错误可能发生,具体取决于代码的复杂程度,不应使用 goto
用于软件重置,这是不好的方法。
改为指定用于软件复位的看门狗定时器的使用。通过使用,您可以轻松启用看门狗定时器。
#include< avr / wdt.h>
$来自AVR Libc的p $ p>
#define soft_reset()\
do \
{\
wdt_enable(WDTO_15MS); \
for(;;)\
{\
} \
} while(0)
$ b在开始时禁用看门狗。
#include< AVR / wdt.h>
$ b $ //函数Pototype
void wdt_init(void)__attribute __((naked))__attribute __((section(.init3)));
//函数实现
void wdt_init(void)
{
MCUSR = 0;
wdt_disable();
return;
.init3
是在main
函数之前执行,请看对于大多数情况,是的,但是如果你使用bootloader的话,起始地址可以推迟。
so I've been assigned the task of writing a custom reset() function for an AVR compilation.
I'm given this info - "Atmega128 and Pic24e have the reset interrupt at the program address 0x0. Write a function reset() to reset a program. I've also heard an easy way to force your system to reboot is by sending it into an infinite loop.
Call me crazy, but is this as simple as writing:
function reset(){ goto 0x00000000; }
Not sure if you can do that, and not looking for a complex explanation, as I'm pretty sure this is a generic question. Short and sweet if you can :)
解决方案
goto 0x00000000
starts the program over but with all SFR not initialized, and the interrupts not initialized. Errors may happen depending on the complexity of your code. You should not usegoto
for software reset, that's bad way.Instead AVR Libc Reference Manual specifies the usage of watchdog timer for software reset. By using avr/wdt you could easily enable watchdog timer.
#include <avr/wdt.h> #define soft_reset() \ do \ { \ wdt_enable(WDTO_15MS); \ for(;;) \ { \ } \ } while(0)
from AVR Libc
To disable watchdog at start time.
#include <avr/wdt.h> // Function Pototype void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); // Function Implementation void wdt_init(void) { MCUSR = 0; wdt_disable(); return; }
.init3
is executed beforemain
function, look at Memory Sections for more details.For most cases yes, but if you are using bootloader, the start address may defers.
这篇关于如何在C中为AVR-Studio编写自定义reset()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!