?? timer.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 + -