?? timer.c
字號:
#include "tdp.h"
#include <reg51.h>
#define TIMER0_COUNT 0xDC11 /* 10000h-((11059200Hz/(12*FREQ))-17) */
static xdata unsigned timer0_tick;
/* 定時器T0中斷服務函數 */
static void timer0_isr (void) interrupt 1 using 1 {
P1 |= 0x80;
TR0 = 0; /* 停止 T0 */
TL0 = TL0 + (TIMER0_COUNT & 0x00FF);
TH0 = TH0 + (TIMER0_COUNT >> 8);
TR0 = 1; /* 啟動T0 */
timer0_tick++;
P1 &= ~0x80;
}
/* 定時器T0初始化函數 */
void timer0_initialize (void) {
INT_DISABLE; /* 關中斷 */
timer0_tick = 0;
TR0 = 0; /* 停止T0 */
TMOD &= ~0x0F; /* 設置T0工作方式 */
TMOD |= 0x01;
TL0 = (TIMER0_COUNT & 0x00FF);
TH0 = (TIMER0_COUNT >> 8);
PT0 = 0; /* 設置T0中斷優先級 */
ET0 = 1; /* 允許T0中斷 */
TR0 = 1; /* 啟動T0 */
INT_ENABLE; /* 開中斷 */
}
/* 定時器T0節拍函數 */
unsigned timer0_count (void) {
xdata unsigned t;
INT_DISABLE;
t = timer0_tick;
INT_ENABLE;
return (t);
}
/* 定時器T0節拍計數函數 */
unsigned timer0_elapsed_count (unsigned count) {
return (timer0_count () - count);
}
/* 定時器T0延時函數 */
void timer0_wait (unsigned count) {
xdata unsigned start_count;
start_count = timer0_count ();
while (timer0_elapsed_count (start_count) <= count)
{
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -