標題: 8051單片機定時器程序 [打印本頁] 作者: luguang06 時間: 2026-5-10 19:50 標題: 8051單片機定時器程序 The post is intened to be used as a record with the STC-89C52RC development board.
<!-- more -->
環境介紹:硬件為KST-51 開發板 v1.3.2,編譯環境KeilV5 9.54,程序燒錄工具 STC-ISP_v6.94L
資料: [KST-51_DataSheet] [STC89C52RC-en]
## 寄存器
### 定時器:
**八位重裝載模式** 定時器定時時間計算:CNT(255 - TL1) = 921600 * T(s)
同時在八位重裝載模式下,TL1 從零開始計數,TL1為八位,也就是累積至255,當255 + 1 時 TF1 = 1,同時需要注意 TF1 這里需要手動清零。
對于**查詢法/輪詢法**實現定時器,以下是示例程序
// Realize the 10ms LED repeatedly light, e.g. LED = 1 sustain 10ms, later LED = 0 sustain 10ms continuously repeating.
// The the procedure, the LED signal is at a frequency of 50 hertz.
// Implementation of timer using query method
void Timer0_ISR() interrupt 1 {
static U8 i = 0; // Here, the"Static" keyword is used to archieve the effect of being assigned only once.
TH0 = T0_RELOAD_HIGH;
TL0 = T0_RELOAD_LOW;