main.c

来自「本光盘的所有代码均在Keil C51 7.0以上版本编译通过。读者的电脑只需要能」· C语言 代码 · 共 68 行

C
68
字号
#include <reg51f.h>

/*------------------------------------------------
This function adjusts the watchdog timer compare
value to the current PCA timer value + 0xFF00.

Note that you must write to CCAP4L first, then
write to CCAP4H.
------------------------------------------------*/
void watchdog_reset (void)
{
unsigned int newval;

newval  = (CH << 8) | CL;
newval += 0xFF00;

CCAP4L = newval;
CCAP4H = newval >> 8;
}

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
unsigned int i;

/*--------------------------------------
Configure PCA Module 4 as the watchdog
and make sure it doesn't time-out
immediately.
--------------------------------------*/
watchdog_reset ();
CCAPM4 = 0x48;

/*--------------------------------------
Configure the PCA for watchdog timer.
--------------------------------------*/
CMOD = (CMOD & 0x01) | 0x40;

/*--------------------------------------
Start the PCA Timer.

From this point on, we must reset the
watchdog timer every 0xFF00 clock
cycles.  If we don't the watchdog timer
will reset the MCU.
--------------------------------------*/
CR = 1;

/*--------------------------------------
Do something for a while and make sure
that we don't get reset by the watchdog.
--------------------------------------*/
for (i = 0; i < 1000; i++)
  {
  watchdog_reset ();
  }

/*--------------------------------------
Stop updating the watchdog and we should
get reset.
--------------------------------------*/
while (1)
  {
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?