📄 timer.c
字号:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Jim Brady
// Do not use commercially without author's permission
// Last revised August 2002
// Net TIMER.C
//
// This module sets up the timer and handles the timer interrupt
//-----------------------------------------------------------------------------
#include "C8051f040.h"
#include "net.h"
#include "timer.h"
extern UINT volatile event_word;
extern ULONG idata initial_sequence_nr;
//--------------------------------------------------------------------------
// Timer 2 interrupt service routing. Intr vector location is
// address 0x002B. The timer generates an interrupt every 25 msec
// It is set to auto reload so do not need to reload it.
//--------------------------------------------------------------------------
void timer2_interrupt(void) interrupt 5
{
static UINT count1 = 0;
static UINT count2 = 0;
TF2 = 0; // Clear interrupt flag
// Advance the initial sequence number
initial_sequence_nr += 6250;
// Keep it some distance from rolling over
if (initial_sequence_nr & 0x10000000L) initial_sequence_nr = 1;
count1++;
// These events happens every 0.50 seconds, not simultaneously
if (count1 == 5) event_word |= EVENT_ARP_RETRANSMIT;
if (count1 == 10) event_word |= EVENT_TCP_INACTIVITY;
if (count1 == 15) event_word |= EVENT_READ_ANALOG;
if (count1 == 20)
{
count1 = 0;
event_word |= EVENT_TCP_RETRANSMIT;
}
count2++;
if (count2 == 2401)
{
count2 = 0;
event_word |= EVENT_AGE_ARP_CACHE;
}
}
//--------------------------------------------------------------------------
// 将定时器2设置为模式1自动重载
// rate = 22.1184 MHz /(12 * (65536 - reload value))
// 25 毫秒 reload value = 19456 = 0x4C00
//--------------------------------------------------------------------------
void init_timer2(void)
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x04;
RCAP2H = 0xBF;
TMR2H = 0xBF;
TMR2CF = 0X80;
SFRPAGE = SFRPAGE_SAVE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -