如何在C中为AVR-Studio编写自定义reset()函数? [英] How to write a custom reset() function in C for AVR-Studio?

查看:198
本文介绍了如何在C中为AVR-Studio编写自定义reset()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我给出了这个信息 - Atmega128和Pic24e在程序地址0x0处有复位中断,编写一个函数reset()来重置一个程序,我也听说过一个简单的方法来强制你的系统重新启动,就是把它发送到一个无限循环中。



打电话给我很疯狂,但是这很简单:

  function reset(){ 
goto 0x00000000;
}

不确定您是否可以这样做,没有寻找一个复杂的解释,因为我敢肯定这是一个通用的问题。如果你可以的话,简短而又甜蜜:) p> goto 0x00000000 启动程序结束,但所有SFR未初始化,且中断未初始化错误可能发生,具体取决于代码的复杂程度,不应使用 goto 用于软件重置,这是不好的方法。



改为 AVR Libc参考手册指定用于软件复位的看门狗定时器的使用。通过使用 avr / wdt ,您可以轻松启用看门狗定时器。

  #include< avr / wdt.h> 

#define soft_reset()\
do \
{\
wdt_enable(WDTO_15MS); \
for(;;)\
{\
} \
} while(0)



$ b


注意!较旧的AVR将在复位时禁用看门狗定时器。对于这些较老的AVR,通过启用看门狗来进行软复位很容易,因为看门狗在复位后将被禁用。在较新的AVR中,一旦启用了看门狗,即使在复位后,它仍保持启用状态!对于这些较新的AVR,需要将一个函数添加到.init3部分(即在启动代码中,在main()之前),以便足够早地禁用看门狗,以便它不会持续重置AVR。

在开始时禁用看门狗。

  #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 函数之前执行,请看


我给出了这个信息 - Atmega128和Pic24e有复位中断在程序地址0x0处。

对于大多数情况,是的,但是如果你使用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 use goto 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

CAUTION! Older AVRs will have the watchdog timer disabled on a reset. For these older AVRs, doing a soft reset by enabling the watchdog is easy, as the watchdog will then be disabled after the reset. On newer AVRs, once the watchdog is enabled, then it stays enabled, even after a reset! For these newer AVRs a function needs to be added to the .init3 section (i.e. during the startup code, before main()) to disable the watchdog early enough so it does not continually reset the AVR.

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 before main function, look at Memory Sections for more details.

I'm given this info - "Atmega128 and Pic24e have the reset interrupt at the program address 0x0.

For most cases yes, but if you are using bootloader, the start address may defers.

这篇关于如何在C中为AVR-Studio编写自定义reset()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆