⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 C-Code for 8051 timer functions
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -