不知道从何时开始,Atmel官网没了,再也找不到那些相关的资源了。翻看了我的下载记录,找到了几个8051的官方例子,分享过来。#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)
{
}
}
|