標(biāo)題:
怎么讓我按下啟動(dòng)程序只運(yùn)行一次?51單片機(jī)程序請(qǐng)教
[打印本頁]
作者:
849663919
時(shí)間:
2018-12-11 14:36
標(biāo)題:
怎么讓我按下啟動(dòng)程序只運(yùn)行一次?51單片機(jī)程序請(qǐng)教
怎么讓我按下啟動(dòng)程序只運(yùn)行一次 我試過去掉while(1)他就不執(zhí)行之后的程序了
各位大佬出出主意謝謝了
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
sbit LED20=P2^0;
sbit LED21=P2^1;
sbit LED22=P2^2;
sbit LED23=P2^3;
sbit P3_4=P3^4;
uint time;
void initT0( )
{
TMOD = 0x01; //定時(shí)器0,工作方式1
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256; //50ms中斷一次 中斷20次,1s 改數(shù)值等于改秒數(shù) 20=1s
EA = 1;
ET0 = 1;
TR0 = 1;
}
void T0int( ) interrupt 1
{
TH0 = (65536-65536)/256;
TL0 = (65536-65536)%256;
time++;
}
void main(void)
{
initT0();
if(P3_4==0) //啟動(dòng)
{
while(1) //循環(huán)程序
{
if(time==1) //如果中斷為1
{
LED=0; //機(jī)械手得電
}
if(time==40) //啟動(dòng)后2S
{
LED1=0; //導(dǎo)軌正
}
if(time==140) //啟動(dòng)后7S(上一部后5S)
{
LED=1; //機(jī)械手放
LED1=1; //導(dǎo)軌正失電
LED2=0; //導(dǎo)軌反
LED3=0; //推桿正
}
if(time==240) //啟動(dòng)后12S(上一部后5S)
{
LED2=1; //導(dǎo)軌反失電
}
if(time==340) //啟動(dòng)后17S(上一部后5S)
{
LED3=1; //推桿正失電
LED4=0; //右側(cè)電機(jī)正
LED5=0; //左側(cè)電機(jī)正
}
if(time==360) //啟動(dòng)后18S(上一部后1S,電機(jī)停1S)
{
LED4=1; //右側(cè)電機(jī)正失電
LED5=1; //左側(cè)電機(jī)正失電
}
if(time==380) //啟動(dòng)后19S(上一部后1S)
{
LED6=0; //右側(cè)電機(jī)反
LED7=0; //左側(cè)電機(jī)反
}
if(time==400) //啟動(dòng)后20S(上一部后1S)
{
LED6=1; //右側(cè)電機(jī)失電
LED7=1; //左側(cè)電機(jī)失電
LED20=0; //推桿反
}
if(time==600) //啟動(dòng)后30S(上一部后10S)
{
LED20=1; //推桿反失電
LED21=0; //下側(cè)電機(jī)正
}
if(time==620) //啟動(dòng)后31S(上一部后1S)
{
LED21=1; //下側(cè)電機(jī)正失電
}
if(time==640) //啟動(dòng)后32S(上一部后1S)
{
LED22=0; //下側(cè)電機(jī)反
}
if(time==660) //啟動(dòng)后33S(上一部后1S)
{
LED22=1; //下側(cè)電機(jī)反失電
time=0; //中斷清零
}
}
}
}
作者:
hbnpmw
時(shí)間:
2018-12-11 22:22
while(1) //循環(huán)程序
{
if(P3_4==0) //啟動(dòng)
{
}
作者:
hbnpmw
時(shí)間:
2018-12-11 22:25
while(1)
{
if(P3_4==0) //啟動(dòng)
{
//在這里添加你的處理函數(shù)
}
}
這樣存在的問題:
1. 你按下P3_4時(shí)間長的話,程序會(huì)多執(zhí)行幾次
建議:
在按鍵部分增加一個(gè)延時(shí),按鍵的延時(shí)掃描程序咱們論壇中有很多,希望你能自己尋找一下。
如果 還有疑問,我們繼續(xù)溝通
作者:
wulin
時(shí)間:
2018-12-12 09:32
本帖最后由 wulin 于 2018-12-12 13:36 編輯
在你的程序基礎(chǔ)上修改,每次按下鍵后只執(zhí)行一次,程序沒有跑完再按無效。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
sbit LED20=P2^0;
sbit LED21=P2^1;
sbit LED22=P2^2;
sbit LED23=P2^3;
sbit P3_4=P3^4;
uint time,i;
bit j=0;//啟動(dòng)位變量
void initT0( )
{
TMOD = 0x01; //定時(shí)器0,工作方式1
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256; //50ms中斷一次 中斷20次,1s 改數(shù)值等于改秒數(shù) 20=1s
EA = 1;
ET0 = 1;
TR0 = 0;//初始化關(guān)閉定時(shí)器
}
void T0int( ) interrupt 1
{
TH0 = (65536-65536)/256;
TL0 = (65536-65536)%256;
time++;
}
void main(void)
{
initT0();
while(1) //循環(huán)程序
{
if(P3_4==0) //按鍵啟動(dòng)
{
i=500;
while(i--); //消抖延時(shí)
if((P3_4==0)&&(j==0)) //再次判斷按鍵(程序沒有跑完按鍵無效)
{
j=1; //啟動(dòng)位變量1
while(!P3_4);//等待按鍵釋放
TR0=1; //啟動(dòng)定時(shí)器
}
}
if(j==1)
{
if(time==1) //如果中斷為1
{
LED=0; //機(jī)械手得電
}
if(time==40) //啟動(dòng)后2S
{
LED1=0; //導(dǎo)軌正
}
if(time==140) //啟動(dòng)后7S(上一部后5S)
{
LED=1; //機(jī)械手放
LED1=1; //導(dǎo)軌正失電
LED2=0; //導(dǎo)軌反
LED3=0; //推桿正
}
if(time==240) //啟動(dòng)后12S(上一部后5S)
{
LED2=1; //導(dǎo)軌反失電
}
if(time==340) //啟動(dòng)后17S(上一部后5S)
{
LED3=1; //推桿正失電
LED4=0; //右側(cè)電機(jī)正
LED5=0; //左側(cè)電機(jī)正
}
if(time==360) //啟動(dòng)后18S(上一部后1S,電機(jī)停1S)
{
LED4=1; //右側(cè)電機(jī)正失電
LED5=1; //左側(cè)電機(jī)正失電
}
if(time==380) //啟動(dòng)后19S(上一部后1S)
{
LED6=0; //右側(cè)電機(jī)反
LED7=0; //左側(cè)電機(jī)反
}
if(time==400) //啟動(dòng)后20S(上一部后1S)
{
LED6=1; //右側(cè)電機(jī)失電
LED7=1; //左側(cè)電機(jī)失電
LED20=0; //推桿反
}
if(time==600) //啟動(dòng)后30S(上一部后10S)
{
LED20=1; //推桿反失電
LED21=0; //下側(cè)電機(jī)正
}
if(time==620) //啟動(dòng)后31S(上一部后1S)
{
LED21=1; //下側(cè)電機(jī)正失電
}
if(time==640) //啟動(dòng)后32S(上一部后1S)
{
LED22=0; //下側(cè)電機(jī)反
}
if(time==660) //啟動(dòng)后33S(上一部后1S)
{
LED22=1; //下側(cè)電機(jī)反失電
j=0; //啟動(dòng)位變量0
TR0=0; //關(guān)閉定時(shí)器
time=0; //中斷清零
}
}
}
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1