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

📄 timer.c

📁 此源码为商用的电力抄表系统的从机端源程序。通过电力载波模块
💻 C
字号:
/*=============================================================================*/
// time.c  - source file for lme2200 API
// 
// Copyright 2005, Leaguer MicroElectronics Co., Ltd
// www.leaguerme.com
/*=============================================================================*/

#include <REG922.H>

#include "sart.h"
#define uint unsigned int
#define uchar unsigned char

uchar tm_count, count_reload; 
uchar inter;
// Logic timers
void (*t0_proc)();   // pointer to a functin that returns void
uint t0_count;
uint t0_val;
bit t0_enable;

void (*t1_proc)();   // pointer to a functin that returns void
uint t1_count;
uint t1_val;
bit t1_enable;
/**/
void (*t2_proc)();   // pointer to a functin that returns void
uint t2_count;
uint t2_val;
bit t2_enable;

void (*t3_proc)();   // pointer to a functin that returns void
uchar t3_count;
uchar t3_val;
bit t3_enable;
/**/
void (*t4_proc)();   // pointer to a functin that returns void
uint t4_count;
uint t4_val;
bit t4_enable;

/*=============================================================================*/

void timer0_init(uchar interval, uchar count)
{

  

  TMOD = (TMOD & 0xF0) | 0x01;  /* Set Mode 2 (8 bit reload) 使用T0与串口*/
  TAMOD = 0x00;              

  TH0 = interval;  // count 185 times from 71 to 256   
               // = 50 us, Reload TL0 to count count_val clocks  TH0 = 0x47 */
  TL0 = 0;             // In LPC922
  inter=TH0;
 
  EX1 = 1;	  //<----------------开外部中断1
  IT1 = 1;	  //<---------------- 外部中断下沿触发
  ET0 = 1;                      /* Enable Timer 0 Interrupts */
  TR0 = 1;                      /* Start Timer 0 Running */
  EA = 1;                       /* Global Interrupt Enable */

 // IP0 = 0X02;	  // TO定时器为一级优先级
 // IP0H = 0X00;
  count_reload = count;
  tm_count = count;

  t0_enable = 0;
  t1_enable = 0;
  t2_enable = 0;
  t3_enable = 0;
  t4_enable = 0;
}


/*=============================================================================*/
// Timer 1 Interrupt Service Routine.

// Interrupt is generated everytime timer1 overflows.
/*=============================================================================*/

void timer0_ISR (void) interrupt 1 
{
  TH0   = inter;  		  //重装计时 1ms
  TL0	 = 0; 



   if (t0_enable == 1) {  //1.6秒定时
      t0_count++;
      if (t0_count == t0_val) {
	    t0_count = 0;
	    t0_proc();
	  }
     }

   if (tm_count-- == 0) {//<--------------计10ms
	
     tm_count = count_reload;   //10

    if (t1_enable == 1) {   //定时LED为300ms
      t1_count++;
      if (t1_count == t1_val) {
	    t1_count = 0;
	    t1_proc();   
	  }
    }

    if (t2_enable == 1) {     //40S定时器
      t2_count++;
      if (t2_count == t2_val) {
	    t2_count = 0;
	    t2_proc();
	  }
    }

  
  if (t3_enable == 1) {    //150ms计中断脉宽
      t3_count++;
      if (t3_count == t3_val) {
	    t3_count = 0;
	    t3_proc();
	  }
    }
    if (t4_enable == 1) {    //
      t4_count++;
      if (t4_count == t4_val) {
	    t4_count = 0;
	    t4_proc();
	  }
   }  
 }

}
/*===============================看门狗初始化==================================*/

void wdt_init()
{
  ACC = WDCON;	   //<------读取WDT控制寄存器
  ACC = ACC| 0x74;  //<------置位ACC.2,准备启动WDT
  WDL = 0xff;	   //<------设置8位倒计时初值
  WDCON = ACC;	   //<------启动WDT
  WFEED1 = 0xA5;	   //<------清第一部分
  WFEED2 = 0x5A;	   //<------清第二部分

}
/*=============================================================================*/

void clr_wdt()
{
  EA     =  0;		 //<------关中断
  WFEED1 = 0xA5;		 //<------清第一部分
  WFEED2 = 0x5A;		 //<------清第二部分
  //	Rxok   = ~Rxok;		 //<------显示
  EA     =  1;		 //<------开中断
}
/*=============================================================================*/

void setup_t0(uint t0, void (*proc)())
{
  t0_val = t0;    //100次为1s
  t0_proc = proc; //时间到设timeout_1s = 1;
  t0_count = 0;
  t0_enable = 1;
  
}
/*=============================================================================*/


void disable_t0(void)
{
  t0_enable = 0;
}


void setup_t1(uint t, void (*proc)())
{
  t1_val = t;
  t1_proc = proc;
  t1_count = 0;
  t1_enable = 1;
  
}

void disable_t1(void)
{
  t1_enable = 0;
  Rxok   = 1;
}
/**/
/*=============================================================================*/

void setup_t2(uint t, void (*proc)())
{
  t2_val = t;
  t2_proc = proc;
  t2_count = 0;
  t2_enable = 1;
  
}

/*=============================================================================*/

/**//**/
void disable_t2(void)
{
  t2_enable = 0;
}

void setup_t3(uint t, void (*proc)())
{
  t3_val = t;
  t3_proc = proc;
  t3_count = 0;

  t3_enable = 1;
}

void disable_t3(void)
{
  t3_enable = 0;
}

void setup_t4(uint t, void (*proc)())
{
  t4_val = t;
  t4_proc = proc;
  t4_count = 1;

  t4_enable = 1;
}

void disable_t4(void)
{
  t4_enable = 0;

}
/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$<<<<<软件说明>>>>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$* /

/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$<<<<<软件说明>>>>>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/



⌨️ 快捷键说明

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