main.c

来自「C-Code for 8051 timer functions」· C语言 代码 · 共 60 行

C
60
字号
#include <reg52.h>
#include <stdio.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 Timer2 for 16-bit auto-reload.
The timer counts to 0xFFFF, overflows,
is reloaded, and generates an interrupt.
--------------------------------------*/
T2CON = 0x80;                /* 10000000 */

/*--------------------------------------
Set the reload values to be 1000 clocks.
--------------------------------------*/
RCAP2L = (65536UL-1000UL);
RCAP2H = (65536UL-1000UL) >> 8;

TL2 = RCAP2L;
TH2 = RCAP2H;

/*--------------------------------------
--------------------------------------*/
ET2 = 1;                      /* Enable Timer 2 Interrupts */
TR2 = 1;                      /* Start Timer 2 Running */
EA = 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 + -
显示快捷键?