標(biāo)題:
80C52單片機(jī)定時(shí)器T2使用問題請教
[打印本頁]
作者:
趙光祥
時(shí)間:
2018-5-17 22:10
標(biāo)題:
80C52單片機(jī)定時(shí)器T2使用問題請教
用T2來控制LED延時(shí)1s亮滅,如下圖,但燒寫進(jìn)單片機(jī)后發(fā)現(xiàn)控制不了,
覺得應(yīng)該是初始化出現(xiàn)問題,但又不清楚是哪里有問題
各位大神,幫幫看一下,謝謝!
截圖20180517215315.png
(84.16 KB, 下載次數(shù): 62)
下載附件
2018-5-17 21:56 上傳
作者:
zhanghyg
時(shí)間:
2018-5-18 08:55
你TH2和TL2初值0xff不對(duì),LED=!LED,這句也錯(cuò)了,應(yīng)該LED=~LED。
作者:
chaoself
時(shí)間:
2018-5-23 16:19
為何要用定時(shí)器2呢?用0與1都可以呀
作者:
zjjhtony
時(shí)間:
2018-5-23 19:30
初始化時(shí)需要加2條語句(仿真可行):
EA=1;//開總中斷允許
ET2=1; //開定時(shí)器T2的中斷允許
否則你的定時(shí)器T2溢出但不產(chǎn)生中斷。
作者:
angmall
時(shí)間:
2018-5-23 19:38
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
/* define SFR */
sbit ET2 = IE^5;
sfr T2CON = 0xc8; //timer2 control register
sbit TF2 = T2CON^7;
sbit TR2 = T2CON^2;
sfr T2MOD = 0xc9; //timer2 mode register
sfr RCAP2L = 0xca;
sfr RCAP2H = 0xcb;
sfr TL2 = 0xcc;
sfr TH2 = 0xcd;
sbit LED = P2^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer2 interrupt routine */
void tm2_isr() interrupt 5 using 1
{
TF2 = 0;
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
LED = ! LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void main()
{
RCAP2L = TL2 = T1MS; //initial timer2 low byte
RCAP2H = TH2 = T1MS >> 8; //initial timer2 high byte
TR2 = 1; //timer2 start running
ET2 = 1; //enable timer2 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
while (1); //loop
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1