说明
本例子只做软件异常时重置系统用,不需要用作定时器或唤醒用,所以不用打开watchdog中断。需要注意的是:如果上次启动开启过watchdog reset,想要关闭,下次启动必须清除CONFIG4内的WDT配置,否则watchdog还是启动的。
主要代码只是在CONFIG4寄存器对看门狗定时器WDT做使能关闭的配置([7:4],0xF0关闭重置只做定时器,0x50和其他任意值开启重置,但0x50在空闲或掉电模式下停止运行),由于是页擦除,所以先保存CONFIG0到CONFIG4的值再写入。
在操作前要执行TA时效访问保护。
参考代码
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"
#define EN_WATCHDOG
#define CRLF "rn"
#define CFG_READ 0xC0
#define CFG_ERASE 0xE2
#define CFG_BYTE_PROGRAM 0xE1
int is_watchdog_reset_enabled(void)
{
UINT8 cf4;
set_IAPEN;
IAPAL = 0x04;
IAPAH = 0x00;
IAPCN = CFG_READ;
set_IAPGO; //get CONFIG4 data
cf4 = IAPFD;
clr_IAPEN;
//printf("watchdog: cf4=0x%bX"CRLF, cf4);
if ((cf4 & 0xF0) == 0xF0)
{
return 0;
}
else if ((cf4 & 0xF0) == 0x50)
{
return 1; // watchdog reset enabled, but disabled in idle and power down mode
}
else
{
return 2; // watchdog reset enabled, but enabled in idle and power down mode
}
}
#ifdef EN_WATCHDOG
#define FEED_DOG() set_WDCLR
/***********************************************************************
WDT CONFIG enable
warning : this macro is only when ICP not enable CONFIG WDT function
copy this marco code to you code to enable WDT reset.
************************************************************************/
void Enable_WDT_Reset_Config(void)
{
set_IAPEN;
IAPAL = 0x04;
IAPAH = 0x00;
IAPFD = 0x5F; //0x0F; // CONFIG4
IAPCN = CFG_BYTE_PROGRAM;
set_CFUEN;
set_IAPGO; //trigger IAP
while((CHPCON&SET_BIT6)==SET_BIT6); //check IAPFF (CHPCON.6)
clr_CFUEN;
clr_IAPEN;
}
#else
/***********************************************************************
WDT CONFIG disable
warning : this macro is for CONFIG already enable WDT Reset to disable.
Since erase CONFIG is full page
step 1. storage CONFIG value in RAM
step 2. modify RAM CONFIG4 WDT value to disable
step 3. erase CONFIG page
step 4. re writer CONFIG.
************************************************************************/
void Disable_WDT_Reset_Config(void)
{
UINT8 cf0,cf1,cf2,cf3,cf4;
set_IAPEN;
IAPAL = 0x00;
IAPAH = 0x00;
IAPCN = CFG_READ;
set_IAPGO; //Storage CONFIG0 data
cf0 = IAPFD;
IAPAL = 0x01;
set_IAPGO; //Storage CONFIG1 data
cf1 = IAPFD;
IAPAL = 0x02;
set_IAPGO; //Storage CONFIG2 data
cf2 = IAPFD;
IAPAL = 0x03;
set_IAPGO; //Storage CONFIG3 data
cf3 = IAPFD;
IAPAL = 0x04;
set_IAPGO; //Storage CONFIG4 data
cf4 = IAPFD;
cf4 |= 0xF0; //Moidfy Storage CONFIG4 data disable WDT reset
set_CFUEN;
IAPCN = CFG_ERASE; //Erase CONFIG all
IAPAH = 0x00;
IAPAL = 0x00;
IAPFD = 0xFF;
set_IAPGO;
IAPCN = CFG_BYTE_PROGRAM; //Write CONFIG
IAPFD = cf0;
set_IAPGO;
IAPAL = 0x01;
IAPFD = cf1;
set_IAPGO;
IAPAL = 0x02;
IAPFD = cf2;
set_IAPGO;
IAPAL = 0x03;
IAPFD = cf3;
set_IAPGO;
IAPAL = 0x04;
IAPFD = cf4;
set_IAPGO;
clr_CFUEN;
clr_IAPEN;
}
#endif /* EN_WATCHDOG */
void init_watchdog(void)
{
printf("watchdog: WDCON=0x%bX"CRLF, WDCON);
if (WDCON & SET_BIT3) // WDTRF
{
printf("WARNING: Last time, the system was reset by the watchdog!"CRLF);
clr_WDTRF;
}
//----------------------------------------------------------------------------------------------
// WDT Init !!! ENABLE CONFIG WDT FIRST !!!
// Warning:
// Always check CONFIG WDT enable first, CONFIG not enable, SFR can't enable WDT reset
// Please call Enable_WDT_Reset_Config() function to enable CONFIG WDT reset
//----------------------------------------------------------------------------------------------
if (is_watchdog_reset_enabled())
{
#ifndef EN_WATCHDOG
Disable_WDT_Reset_Config(); //Software disable WDT reset CONFIG setting
return;
#endif
}
else
{
#ifdef EN_WATCHDOG
Enable_WDT_Reset_Config(); //Software enable WDT reset CONFIG setting
#endif
}
#ifdef EN_WATCHDOG
TA=0xAA;TA=0x55;WDCON|=0x07; //Setting WDT prescale
set_WDCLR; //Clear WDT timer
while((WDCON|~SET_BIT6)==0xFF); //confirm WDT clear is ok before into power down mode
// EA = 1;
set_WDTR; //WDT run
#endif
}
void main(void)
{
init_watchdog();
while(1)
{
#ifdef EN_WATCHDOG
FEED_DOG();
#endif
printf("main: do something.."CRLF);
}
}
上一篇:N76E003开发笔记(关于使用N76E003过程遇到的问题)
下一篇:N76E003配置串口异步双工收发
推荐阅读最新更新时间:2024-11-13 14:08
设计资源 培训 开发板 精华推荐
- MC34072VDR2G高频脉宽调制典型应用
- OP213FPZ精密称重传感器秤放大器的典型应用
- 用于确定 L6566B SMPS 电源多模式控制器的 OVP 和 OCP 上的 IC 行为的典型外部电路
- LT1086CT-3.6 用于遥感的低压差正稳压器的典型应用
- ADP1765 5A 固定输出工作线性稳压器的典型应用
- STM8L15LPBOARD,基于 STM8L(32KB 闪存)低功耗 MCU 的低功耗演示板
- #第七届立创电赛#基于ina199的USB电流表
- 10kHZ-255MHz 信号发生器 OLED LCD
- 血栓弹力图
- LT1993-2 的典型应用 - 800MHz 低失真、低噪声差分放大器 ADC 驱动器 (Av = 2V/V)