main.c

来自「这是单片机C51典型应用设计代码」· C语言 代码 · 共 58 行

C
58
字号
#include <REG517.H>

/*------------------------------------------------
Timer 2 Interrupt Service Routine.

Set a breakpoint on 'overflow_count++' and run the
program in the debugger.  You will see this line
executes every 1000 clock cycles (or 1,000 Hz).

So, overflow_count is actually a 1/1,000 sec
timer.
------------------------------------------------*/
static unsigned long overflow_count = 0;

void timer1_ISR (void) interrupt 5
{
TF2 = 0;            /* Clear the interrupt request */
overflow_count++;   /* Increment the overflow count */
}

/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set the reload values to be 1000 clocks.
--------------------------------------*/
CRCL = (65536UL-1000UL);
CRCH = (65536UL-1000UL) >> 8;

TL2 = CRCL;
TH2 = CRCH;

/*--------------------------------------
Set Timer2 for 16-bit auto-reload.
The timer counts to 0xFFFF, overflows,
is reloaded, and generates an interrupt.
--------------------------------------*/
T2CON = 0x11;                /* 0XX10001 */

/*--------------------------------------
--------------------------------------*/
ET2 = 1;                      /* Enable Timer 2 Interrupts */
EAL = 1;                      /* Global Interrupt Enable */

/*--------------------------------------
Do Nothing.  Actually, the timer 2
interrupt will occur every 1000 clocks.
Since the oscillator runs at 12 MHz,
the interrupt will happen every 1 KHz.
--------------------------------------*/
while (1)
  {
  }
}

⌨️ 快捷键说明

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