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

📄 timer.c

📁 mega128平台下
💻 C
字号:
#include "timer.h"
//#include "schedule.h"
#include<iom128v.h>
#include <macros.h>

Timer_struct T0,T1,T2,T3;

void mcu_sleep();
/*
//Timer接口
//Timer0
void start_T0(char type, unsigned int count);
void stop_T0(void);
//Timer1
void start_T1(char type, unsigned int count);
void stop_T1(void);
//Timer2
void start_T2(char type, unsigned int count);
void stop_T2(void);
//Timer3
void start_T3(char type, unsigned int count);
void stop_T3(void);
//init
void T0_init();
void T1_init();
void T2_init();
void T3_init();
*/
//Timer接口
//Timer0
void start_T0(char type, unsigned int count)
{ 
   //1024分频,外部时钟,最大7s计数,秒级精度100%
    unsigned char t_count; 
 
   if (type==1)
      T0.type = 1;
   else 
      T0.type = 0; 
   T0.ticks = count;
   T0.tickleft = count;
   if (T0.tickleft > 7000)
   {
     
	 T0.tickleft -= 7000;
     TCNT0 = 0x0; //setup
     
   }
   else
   {
      
     t_count = T0.tickleft*72/10 -1; 
	 T0.tickleft =0;

     TCNT0 = 0xff - (t_count & 0xff); //setup
     
   }
   
   TCCR0 = 0x07; //start timer
   TIMSK |= 0x01;
   T0.on=1;
   adjustPower();
  // post(mcu_sleep);
}
void stop_T0(void)
{
  TCCR0 = 0x0;
  TIMSK &=~0x01;
  T0.on=0;
}
//Timer1
void start_T1(char type, unsigned int count)
{
   unsigned int t_count; 
 
   if (type==1)
      T1.type = 1;
   else 
      T1.type = 0; 
   T1.ticks = count;
   T1.tickleft = count;
   if (T1.tickleft > 9000)
   {
     
	 T1.tickleft -= 9000;
     TCNT1H = 0x0; //setup
     TCNT1L = 0x0;
   }
   else
   {
      
     t_count = T1.tickleft*72/10 -1; 
	 T1.tickleft =0;

     TCNT1H = 0xff - ((t_count>>8) & 0xff); //setup
     TCNT1L = 0xff - (t_count & 0xff);
   }
   
   TCCR1B = 0x05; //1024分频 
   TIMSK |= 0x04;
   T1.on=1;
 
   // led1On();
}
void stop_T1(void)
{
   TCCR1B = 0x0;
   TIMSK &=~(0x04);
    T1.on=0;
}
//Timer2
void start_T2(char type, unsigned int count)
{
  //1024分频,最大35ms
   unsigned char t_count; 
 
   if (type==1)
      T2.type = 1;
   else 
      T2.type = 0; 
   T2.ticks = count;
   T2.tickleft = count;
   if (T2.tickleft > 35)
   {
     
	 T2.tickleft -= 35;
     TCNT2 = 0x0; //setup
   }
   else
   {
      
     t_count = T2.tickleft*72/10 -1; 
	 T2.tickleft =0;

     TCNT2 = 0xff - (t_count & 0xff); //setup
     
   }
   
   TCCR2 = 0x05; //start timer
   TIMSK |= 0x40;
   T2.on=1;

}
void stop_T2(void)
{
  TCCR2 = 0x0;
  TIMSK &= ~0x40;
   T2.on=0;
}
//Timer3
void start_T3(char type, unsigned int count)
{

   unsigned int t_count; 
 
   if (type==1)
      T3.type = 1;
   else 
      T3.type = 0; 
   T3.ticks = count;
   T3.tickleft = count;
   if (T3.tickleft > 9000)
   {
     
	 T3.tickleft -= 9000;
     TCNT3H = 0x0; //setup
     TCNT3L = 0x0;
   }
   else
   {
      
     t_count = T3.tickleft*72/10 -1; 
	 T3.tickleft =0;

     TCNT3H = 0xff - ((t_count>>8) & 0xff); //setup
     TCNT3L = 0xff - (t_count & 0xff);
   }
   
   TCCR3B = 0x05; //1024分频 
   ETIMSK |= 0x04;
   T3.on=1;
}
void stop_T3(void)
{
   TCCR3B = 0x0; 
   ETIMSK &= ~0x04;
    T3.on=0;
}
//init
void T0_init()
{
  T0.num = 0;
  T0.on = 0;
  T0.type = 0;  //类型,reapeat=1  单次=0
  T0.ticks = 0;  //执行周期
  T0.tickleft = 0;  //剩余时间,单位为秒
  T0.adjustedticks = 0;
  TCCR0 = 0x00; //stop
  ASSR  = 0x08; //set async mode
  
 // TCCR0 = 0x05; //start timer
}

void T1_init(void)
{
  T1.num = 0;
  T1.on = 0;
  T1.type = 0;  //类型,reapeat=1  单次=0
  T1.ticks = 0;  //执行周期
  T1.tickleft = 0;  //剩余时间,单位为秒
  T1.adjustedticks = 0;

 TCCR1B = 0x00; //stop
 //TCNT1H = 0xE3; //setup
 //TCNT1L = 0xE1;
 
 TCCR1A = 0x00;

 //TCCR1B = 0x05; //start Timer
}

 

void T2_init()
{
  T2.num = 0;
  T2.on = 0;
  T2.type = 0;  //类型,reapeat=1  单次=0
  T2.ticks = 0;  //执行周期
  T2.tickleft = 0;  //剩余时间,单位为秒
  T2.adjustedticks = 0;
  TCCR2 = 0x00; //stop

 // TCCR2 = 0x05; //start
}
void T3_init()
{
  T3.num = 0;
  T3.on = 0;
  T3.type = 0;  //类型,reapeat=1  单次=0
  T3.ticks = 0;  //执行周期
  T3.tickleft = 0;  //剩余时间,单位为秒
  T3.adjustedticks = 0;
  TCCR3B = 0x00; //stop
  TCCR3A = 0x00;
}
void mcu_sleep()
{
    MCUCR = 0x38;
    asm("sleep");
}

#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
  	unsigned char t_count = 6000; 
	//led2Toggle();
  	// printf("T0\n");
	//led0Toggle();
   	if (T0.tickleft == 0)
   	{
	 	post(T0_timeout);
		adjustPower();
     	if (T0.type==1)
     	{
        	T0.tickleft = T0.ticks;
	 		T0.on =1;
	 	}
	 	else
	 	{
	    	TCCR0 = 0x0;
	    	TIMSK &= ~0x01;
	    	T0.on =0;
	    	return ;
	 	}
   	}
   
    if (T0.tickleft > 7000)
    {
        T0.tickleft -= 7000;
		TCNT0 = 0x0; //setup
		t_count = t_count*72/10-1; 
    }
    else
    {
        t_count = T0.tickleft*72/10 -1; 
		T0.tickleft =0;
        TCNT0 = 0xff - (t_count & 0xff); //setup
        
    }
    
    TCCR0 = 0x07; //1024分频 
    TIMSK |= 0x01;
    adjustPower();
  // led0Toggle();
   // post(mcu_sleep);

}



#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
 //TIMER1 has overflowed
   unsigned int t_count = 8000; 
  // printf("T1\n");
  led0Toggle();
   if (T1.tickleft == 0)
   {
	 post(T1_timeout);
   	 if (T1.type==1)
   	 {
       T1.tickleft = T1.ticks;
       T1.on =1;
     }
     else
	 {
	   TCCR1B = 0x0;
	   TIMSK &= ~0x04;
	   T1.on =0;
	   return;
	 }
   }
   
      if (T1.tickleft > 9000)
      {
         T1.tickleft -= 9000;
		 TCNT1H = 0x0; //setup
         TCNT1L = 0x0;
         t_count = t_count*72/10-1; 
      }
      else
      {
         t_count = T1.tickleft*72/10 -1; 
		 T1.tickleft =0;
         TCNT1H = 0xff - ((t_count>>8) & 0xff); //setup
         TCNT1L = 0xff - (t_count & 0xff);
      }
      TCCR1B = 0x05; //1024分频 
      TIMSK |= 0x04;
	
 
 }
 

#pragma interrupt_handler timer2_ovf_isr:11
void timer2_ovf_isr(void)
{
  unsigned char t_count = 30; 
 // printf("T2\n");
   if (T2.tickleft == 0)
   {
	 post(T2_timeout);
	 if (T2.type==1)
	 {
        T2.tickleft = T2.ticks;
        T2.on=1;
     }
     else
	 {
	   TCCR2 = 0x0;
	   TIMSK &= ~0x40;
	   T2.on =0;
       return ;
	 }
   }
   
      if (T2.tickleft > 35)
      {
         T2.tickleft -= 35;
		 TCNT2 = 0x0; //setup
		 t_count = t_count*256/1000-1;
      }
      else
      {
         t_count = T2.tickleft*256/1000 -1; 
		 T2.tickleft =0;
         TCNT2 = 0xff - (t_count & 0xff); //setup
        
      }
      TCCR2 = 0x05; //1024分频 
      TIMSK |= 0x40;
	
}

 
 
#pragma interrupt_handler timer3_ovf_isr:30
void timer3_ovf_isr(void)
{
 //TIMER3 has overflowed
  unsigned int t_count = 8000; 
  
   if (T3.tickleft == 0)
   {// led0Toggle();
	 post(T3_timeout);
	//  printf("post success\n");
	 
	 if (T3.type==1)
	 {
        T3.tickleft = T3.ticks;
	    T3.on =1;
	 }
	 else
	 {
	    TCCR3B = 0x0;
	    ETIMSK &= ~0x04;
	    T3.on =0;
	    return ;
	 }
   }
      if (T3.tickleft > 9000)
      {
         T3.tickleft -= 9000;
		 TCNT3H = 0x0; //setup
         TCNT3L = 0x0;
         t_count = t_count*72/10-1;
      }
      else
      {
         t_count = T3.tickleft*72/10 -1; 
		 T3.tickleft =0;
         TCNT3H = 0xff - ((t_count>>8) & 0xff); //setup
         TCNT3L = 0xff - (t_count & 0xff);
      }
      TCCR3B = 0x05; //1024分频 
      ETIMSK |= 0x04;
	
 
}



⌨️ 快捷键说明

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