標(biāo)題: 關(guān)于利用單片機(jī)定時器輸出脈沖的問題 [打印本頁]

作者: alonger1    時間: 2019-10-19 16:26
標(biāo)題: 關(guān)于利用單片機(jī)定時器輸出脈沖的問題
先利用單片機(jī)T1計數(shù)器計數(shù)5次,然后T1轉(zhuǎn)為定時輸出5個1s脈沖,利用led來顯示,然后又轉(zhuǎn)為計數(shù)方式,這樣反復(fù)循環(huán).
為什么我的程序能實(shí)現(xiàn)脈沖的輸出,但輸出的脈沖不能達(dá)到1s.
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int        

sbit led=P2^6;
uchar i=0;
int count;

void timer1_count()
{
        TMOD=0X40;
        TH1=0Xff;
        TL1=0Xfb;
        TR1=1;
}

void timer1_time()
{
        TMOD=0X00;
        TH1=0X3c;
        TL1=0Xb0;
        TR1=1;
}

void  main(void)
{
        while(1)
        {
                i=0;
                timer1_count();
                while(TF1==0);
                TF1=0;
                timer1_time();
                while(1)
                {
                        if(TF1==1)
                        {
                                TF1=0;
                                count++;
                                if(count==20)
                                {
                                        count=0;
                                        led=!led;
                                        i++;
                                }
                        }
                        if(i==10) break;
                }
  }         
}


作者: csmyldl    時間: 2019-10-19 18:28
因?yàn)槎〞r方式為0,是13位計數(shù),你的初始值為 TH1=0X3c;  TL1=0Xb0;(50ms),實(shí)際沒有50ms
作者: devcang    時間: 2019-10-19 18:52





圖中的 # 位置 ,,,,,修改1


作者: wulin    時間: 2019-10-19 22:43
樓主定時器設(shè)置錯誤,程序改這樣比較直觀。
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int        

  4. sbit led=P2^6;
  5. uchar i=0;
  6. uchar count;
  7. bit flag=0;

  8. void timer1_count()
  9. {
  10.         TMOD= 0x50;
  11.         TH1 = 0xff;
  12.         TL1 = 0xfb;
  13. }

  14. void timer1_time()
  15. {
  16.         TMOD= 0x10;
  17.         TH1 = 0x3C;
  18.         TL1 = 0xB0;
  19. }

  20. void main(void)
  21. {
  22.         timer1_count();
  23.         TR1=1;
  24.         while(1)
  25.         {
  26.                 if(!flag)
  27.                 {
  28.                         if(TF1)
  29.                         {
  30.                                 TF1=0;
  31.                                 flag=1;
  32.                                 timer1_time();
  33.                         }
  34.                 }
  35.                 else
  36.                 {
  37.                         if(TF1)
  38.                         {
  39.                                 TF1=0;
  40.                                 TH1 = 0x3C;
  41.                                 TL1 = 0xB0;
  42.                                 if(++count>=20)
  43.                                 {
  44.                                         count=0;
  45.                                         led=~led;
  46.                                         if(++i>=10)
  47.                                         {
  48.                                                 i=0;
  49.                                                 flag=0;
  50.                                                 timer1_count();
  51.                                         }
  52.                                 }
  53.                         }
  54.                 }
  55.         }
  56. }
復(fù)制代碼

作者: hhh402    時間: 2019-10-20 00:38
直接用定時器是不可能定時到1秒的,要用定時器中斷繼續(xù)累加才可能定時到1秒。
作者: alonger1    時間: 2019-10-20 08:10
csmyldl 發(fā)表于 2019-10-19 18:28
因?yàn)槎〞r方式為0,是13位計數(shù),你的初始值為 TH1=0X3c;  TL1=0Xb0;(50ms),實(shí)際沒有50ms

的確是這種情況,將TMOD=0x00改成0x10就能實(shí)現(xiàn)功能了,但請問為什么會出現(xiàn)這種情況,13為與16位計數(shù)有什么差別。
作者: cphnkj188    時間: 2019-10-20 09:05
13位定時器的溢出值達(dá)不到16位的66535,實(shí)現(xiàn)不了樓主所需效果
作者: alonger1    時間: 2019-10-20 13:14
cphnkj188 發(fā)表于 2019-10-20 09:05
13位定時器的溢出值達(dá)不到16位的66535,實(shí)現(xiàn)不了樓主所需效果

明白了,感謝感謝




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