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

📄 timer.c

📁 Keil_C51程序,C8051实现的TCP/IP功能源码
💻 C
字号:
//-----------------------------------------------------------------------------
// Net TIMER.C
//
// This module sets up the timer and handles the timer interrupt
//-----------------------------------------------------------------------------
#include "reg52.h"
#include "net.h"
#include "timer.h"

extern unsigned int 	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 UCHAR 	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;
//	Keep it some distance from rolling over
//  if (initial_sequence_nr & 0x10000000L) initial_sequence_nr = 1;	

	count1++;
	// These events happens every 0.5 seconds, not simultaneously
	if (count1 == 5) event_word |= EVENT_ARP_RETRANSMIT;
	if (count1 == 8) event_word |= EVENT_TCP_INACTIVITY;
	if (count1 == 10)event_word |= EVENT_RS232_ARRIVED;	
	if (count1 == 20)
	{
	 	count1 = 0;
	 	event_word |= EVENT_TCP_RETRANSMIT;
		LED	= ~LED;//350ms
	}

	count2++;
	if (count2 == 1714)
	{
		// This happens every 60.025 seconds, not simultaneous
		// with above tasks
		count2 = 0;
		event_word |= EVENT_AGE_ARP_CACHE;
	}
}
//--------------------------------------------------------------------------
// Setup Timer2 in mode 1 which is 16 bit auto reload
// Intr rate = 22.1184 MHz /(12 * (65536 - reload value))
// For 25 msec reload value = 19456 = 0x4C00
//12MHz -->reload value =65536-t(ms)*cystal/12=65536-25=
//--------------------------------------------------------------------------
void init_timer2(void)
{
	T2CON = 0x00;
	RCAP2H = 0x04; 		// Reload value  35ms
	RCAP2L = 0x00;
	TH2 = 0x04;			// Initial load value
	TL2 = 0x00;
	TR2 = 1;			// Timer 2 run
	PT2 = 0;			//优先级低
}

⌨️ 快捷键说明

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