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

📄 timer.c

📁 51avr tcpip http协议
💻 C
字号:
//-----------------------------------------------------------------------------
// Net TIMER.C
//
// This module sets up the timer and handles the timer interrupt
//-----------------------------------------------------------------------------

#include "net.h"
#include "timer.h"
#include "general.h"
#include <avr/signal.h>

extern unsigned char CONTROL_L;
extern unsigned int event_word;
extern unsigned long initial_sequence_nr;

#define Timer0V   (unsigned char)(256-F_CPU/32/1000)

static unsigned char Count1ms = 0;


void Timer0_Init (void)
{
   TCNT0 = Timer0V;//148;/* reset TCNT0 for 1ms  */
   TCCR0 = 3;                       /* count with clk of F_CPU/32 */
   TIMSK = 1<<TOIE0;                /* enable TCNT0 overflow interrupt*/
}

//1ms timer
SIGNAL(SIG_OVERFLOW0)
{
    static unsigned char Count25ms = 25;
    static unsigned char Count1s = 40;
	static unsigned int count1 = 0;
	static unsigned int count2 = 0;

	TCNT0 = Timer0V;   /* reset counter to get this interrupt again */
    if(Count1ms)Count1ms--;

	if (Count25ms) Count25ms--;
	else
	{
		Count25ms=25;    		//25ms
		RunOff;
		if (Count1s) Count1s--;
		else
		{
			Count1s=40;			//1s, run led invert
			if(CONTROL_L)RunOn;	 
		}

	    // 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)
	    {
		   // This happens every 60.025 seconds, not simultaneous
		   // with above tasks
		   count2 = 0;
		   event_word |= EVENT_AGE_ARP_CACHE;
	    }
	}
}


void Delay1ms(unsigned char T)
{
	Count1ms=T;
	while (Count1ms);
}


int swapint(int k)
{
  return (((k>>8)&0x00ff)+((k<<8)&0xff00));
}


long swaplong(long k)
{
  return ((swapint(k>>16)&0x0000ffff)+(((ULONG)swapint(k)<<16)&0xffff0000));
}

⌨️ 快捷键说明

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