找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 8731|回復(fù): 18
打印 上一主題 下一主題
收起左側(cè)

關(guān)于單片機(jī)延時(shí)程序的問題

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:137704 發(fā)表于 2017-12-29 16:36 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
30黑幣
#include<reg51.h>        //庫文件
#define uchar unsigned char//宏定義無符號(hào)字符型
#define uint unsigned int  //宏定義無符號(hào)整型


/********************************************************************
                            I/O定義
*********************************************************************/
sbit DOUT=P1^3;
sbit YOUT=P1^4;
sbit LED1=P3^7;
/********************************************************************
                            延時(shí)函數(shù)
*********************************************************************/
void delay()//延時(shí)程序
{
uchar m,n,s;
for(m=20;m>0;m--)
for(n=20;n>0;n--)
for(s=248;s>0;s--);
}
/********************************************************************
                            主函數(shù)
*********************************************************************/
void main()
{
        while(1)   //無限循環(huán)
        {
         if((DOUT==1&&YOUT==1))//當(dāng)DO輸出低電平時(shí)
         {
            delay();//延時(shí)抗干擾
                if((DOUT==1&&YOUT==1))//確定 DO口輸出低電平
            {
                   LED1=1;   //這個(gè)地方我想添加一個(gè)延時(shí)的程序 有大神知道怎么添加嗎? 延時(shí)6個(gè)小時(shí)左右
                                 
                }
          else
              {
                               LED1=0;

                }
         }
        }
}   

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:97438 發(fā)表于 2017-12-29 17:24 | 只看該作者
把需求說得清楚點(diǎn)~你要延時(shí)十天八天都是可以的。定時(shí)器計(jì)數(shù),然后判斷時(shí)間就可以了。
回復(fù)

使用道具 舉報(bào)

板凳
ID:258727 發(fā)表于 2017-12-29 20:48 | 只看該作者
通過定時(shí)器做延時(shí)是很好的
回復(fù)

使用道具 舉報(bào)

地板
ID:137704 發(fā)表于 2017-12-30 07:42 | 只看該作者
luoluoluo136 發(fā)表于 2017-12-29 17:24
把需求說得清楚點(diǎn)~你要延時(shí)十天八天都是可以的。定時(shí)器計(jì)數(shù),然后判斷時(shí)間就可以了。

就是DOUT YOUT都為1的時(shí)候 LED1一直點(diǎn)亮六小時(shí) 如果中途DOUT YOUT有一個(gè)為0的時(shí)候就退出循環(huán) LED1熄滅 直到 DOUT YOUT 再次都為1  
回復(fù)

使用道具 舉報(bào)

5#
ID:231046 發(fā)表于 2017-12-30 08:49 | 只看該作者
是可以用定時(shí)器做延時(shí),多長時(shí)間都 可以的
回復(fù)

使用道具 舉報(bào)

6#
ID:78322 發(fā)表于 2017-12-30 09:36 | 只看該作者
#include<reg51.h>        //庫文件 #define uchar unsigned char//宏定義無符號(hào)字符型 #define uint unsigned int  //宏定義無符號(hào)整型   /********************************************************************                             I/O定義 *********************************************************************/ sbit DOUT=P1^3; sbit YOUT=P1^4; sbit LED1=P3^7; uint16   Timecount; /********************************************************************                             延時(shí)函數(shù) *********************************************************************/ void delay()//延時(shí)程序 {     uchar m,n,s;     for(m=20;m>0;m--)     for(n=20;n>0;n--)     for(s=248;s>0;s--); } /********************************************************************                             主函數(shù) *********************************************************************/ void main() {     Timer0_init();     //開啟定時(shí)器0產(chǎn)生1s的中斷     while(1)   //無限循環(huán)     {         if((DOUT==1&&YOUT==1))//當(dāng)DO輸出低電平時(shí)         {             delay();//延時(shí)抗干擾             if((DOUT==1&&YOUT==1))//確定 DO口輸出低電平             {                 LED1=1;   //這個(gè)地方我想添加一個(gè)延時(shí)的程序 有大神知道怎么添加嗎? 延時(shí)6個(gè)小時(shí)左右                 Timecoun = 0;                 while(Timecoun < 3600);             }             else             {                 LED1=0;             }         }     } } void timeint(void) interrupt 1 {     Timecount++; }
回復(fù)

使用道具 舉報(bào)

7#
ID:259850 發(fā)表于 2017-12-30 09:51 | 只看該作者
加個(gè)定時(shí)器
回復(fù)

使用道具 舉報(bào)

8#
ID:78322 發(fā)表于 2017-12-30 12:02 | 只看該作者
#include<reg52.h>        //庫文件
#define uchar unsigned char//宏定義無符號(hào)字符型
#define uint unsigned int  //宏定義無符號(hào)整型

#define DELAYTIME     (200)

typedef enum{false=0, true = 1}  bool;
/********************************************************************
                            I/O定義
*********************************************************************/
sbit DOUT=P1^3;
sbit YOUT=P1^4;
sbit LED1=P3^7;
unsigned short   Timecount;
bool   LedOffflag;
bool   KeyFlag;
/********************************************************************
                            延時(shí)函數(shù)
*********************************************************************/
void delay()//延時(shí)程序
{
    uchar m,n,s;
    for(m=20;m>0;m--)
    for(n=20;n>0;n--)
    for(s=248;s>0;s--);
}
/********************************************************************
                            主函數(shù)
*********************************************************************/
void Timer0_init()
{
    EA   = 1;
    ET0  = 1;
    TR0  = 1;
    TMOD = 0x01; //定時(shí)器0 計(jì)數(shù)模式 定時(shí)器1模式1
    TH0  = 0x0C;
    TL0  = 0xB0;
}

bool GetKey(void)
{
    if((DOUT==1&&YOUT==1))
         return true;
    else return false;
}


void main()
{
    Timer0_init();     //開啟定時(shí)器0產(chǎn)生1s的中斷
    while(1)   //無限循環(huán)
    {
        KeyFlag = GetKey();
        
        if(KeyFlag)
        {
            LED1 = 1;
            Timecount = 0;
            LedOffflag = false;
        }
        else if(LedOffflag)
        {
            LED1 = 0;
        }
    }
}
void timeint(void) interrupt 1
{
    TH0 = 0x00;
    TL0 = 0xB0;
    Timecount++;
    if(Timecount >= DELAYTIME)
    {
        Timecount = DELAYTIME;
        LedOffflag = true;
    }
}
這是重新寫的程序,需要延時(shí)多長時(shí)間,就修改宏DELAYTIME的值。
回復(fù)

使用道具 舉報(bào)

9#
ID:137704 發(fā)表于 2018-1-5 14:42 | 只看該作者
立里 發(fā)表于 2017-12-30 12:02
#include        //庫文件
#define uchar unsigned char//宏定義無符號(hào)字符型
#define uint unsigned int ...

我的單片機(jī)是STC12C5204PWM的 外部晶振 12MHZ 照著你的程序燒錄后 只要一通電 LED就亮著 不管1.3 1.4是高電平還是低電平
回復(fù)

使用道具 舉報(bào)

10#
ID:271988 發(fā)表于 2018-1-5 15:47 | 只看該作者
可以設(shè)置定時(shí)中斷,定時(shí)6個(gè)小時(shí)的
回復(fù)

使用道具 舉報(bào)

11#
ID:239750 發(fā)表于 2018-1-5 17:23 | 只看該作者
用通用定時(shí)器
回復(fù)

使用道具 舉報(bào)

12#
ID:268280 發(fā)表于 2018-1-5 17:51 | 只看該作者
void delay(uint time)        //延時(shí)函數(shù)
{
        uint x,y;
        for(x=time;x>0;x--)
        for(y=110;y>0;y--);
回復(fù)

使用道具 舉報(bào)

13#
ID:272089 發(fā)表于 2018-1-6 08:28 | 只看該作者
死循環(huán)前面加個(gè)
TMOD=0x01;
EA=ET0=TR0=1;

主函數(shù)后面加一個(gè)

time0() interrupt 1
{
     TL0=(65536-50000)%0X100;
     TH0=(65536-50000)/0X100;
     c++;
     if(c>=1200){
               c=0;
               if(t>0)t--;
      }
}
注意主函數(shù)那要定義uint c=0,t;
只要在你要延時(shí)的地方加一個(gè)t=6;就可以了
回復(fù)

使用道具 舉報(bào)

14#
ID:272089 發(fā)表于 2018-1-6 08:31 | 只看該作者

回復(fù)

使用道具 舉報(bào)

15#
ID:83493 發(fā)表于 2018-1-7 08:53 | 只看該作者
延時(shí)可以用定時(shí)器,自己寫的延時(shí)delay函數(shù)時(shí)間不太準(zhǔn)確
回復(fù)

使用道具 舉報(bào)

16#
ID:60379 發(fā)表于 2018-1-7 11:54 | 只看該作者
這里講解已經(jīng)非常詳細(xì)完整了
http://www.torrancerestoration.com/bbs/dpj-78271-1.html
回復(fù)

使用道具 舉報(bào)

17#
ID:272717 發(fā)表于 2018-1-7 14:29 | 只看該作者
dayehetiantian 發(fā)表于 2018-1-5 15:47
可以設(shè)置定時(shí)中斷,定時(shí)6個(gè)小時(shí)的

定時(shí)器定時(shí)延遲比較準(zhǔn)確
回復(fù)

使用道具 舉報(bào)

18#
ID:272964 發(fā)表于 2018-1-7 23:16 | 只看該作者
#include<reg51.h>        //庫文件 #define uchar unsigned char//宏定義無符號(hào)字符型 #define uint unsigned int  //宏定義無符號(hào)整型   /********************************************************************                             I/O定義 *********************************************************************/ sbit DOUT=P1^3; sbit YOUT=P1^4; sbit LED1=P3^7; /********************************************************************                             延時(shí)函數(shù) *********************************************************************/ void delay()//延時(shí)程序 { uchar m,n,s; for(m=20;m>0;m--) for(n=20;n>0;n--) for(s=248;s>0;s--); } /********************************************************************                             主函數(shù) *********************************************************************/ void main() {         while(1)   //無限循環(huán)         {          if((DOUT==1&&YOUT==1))//當(dāng)DO輸出低電平時(shí)          {             delay();//延時(shí)抗干擾                 if((DOUT==1&&YOUT==1))//確定 DO口輸出低電平             {                    LED1=1;   //這個(gè)地方我想添加一個(gè)延時(shí)的程序 有大神知道怎么添加嗎? 延時(shí)6個(gè)小時(shí)左右                                                   }           else               {                                LED1=0;                  }          }         } }   
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表