標(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)問題,但又不清楚是哪里有問題
各位大神,幫幫看一下,謝謝!



作者: 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

  1. #include "reg51.h"

  2. typedef unsigned char BYTE;
  3. typedef unsigned int WORD;

  4. //-----------------------------------------------

  5. /* define constants */
  6. #define FOSC 11059200L

  7. #define T1MS (65536-FOSC/12/1000)   //1ms timer calculation method in 12T mode

  8. /* define SFR */
  9. sbit ET2 = IE^5;

  10. sfr T2CON = 0xc8;                   //timer2 control register
  11. sbit TF2  = T2CON^7;
  12. sbit TR2  = T2CON^2;

  13. sfr T2MOD = 0xc9;                   //timer2 mode register
  14. sfr RCAP2L = 0xca;
  15. sfr RCAP2H = 0xcb;
  16. sfr TL2 = 0xcc;
  17. sfr TH2 = 0xcd;

  18. sbit LED = P2^0;               //work LED, flash once per second

  19. /* define variables */
  20. WORD count;                         //1000 times counter

  21. //-----------------------------------------------

  22. /* Timer2 interrupt routine */
  23. void tm2_isr() interrupt 5 using 1
  24. {
  25.     TF2 = 0;
  26.     if (count-- == 0)               //1ms * 1000 -> 1s
  27.     {
  28.         count = 1000;               //reset counter
  29.         LED = ! LED;                              //work LED flash
  30.     }
  31. }

  32. //-----------------------------------------------

  33. /* main program */
  34. void main()
  35. {
  36.     RCAP2L = TL2 = T1MS;            //initial timer2 low byte
  37.     RCAP2H = TH2 = T1MS >> 8;       //initial timer2 high byte
  38.     TR2 = 1;                        //timer2 start running
  39.     ET2 = 1;                        //enable timer2 interrupt
  40.     EA = 1;                         //open global interrupt switch
  41.     count = 0;                      //initial counter

  42.     while (1);                      //loop
  43. }

復(fù)制代碼





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1