|
10黑幣
0.png (46.95 KB, 下載次數(shù): 76)
下載附件
2021-4-13 16:31 上傳
這是我的仿真原理圖,現(xiàn)在的問題就是熱釋電開關(guān)按下后電機(jī)會一直停頓在一個角度,然后我設(shè)置的限位開關(guān)k1-k4按了后沒有反應(yīng),并且程序設(shè)置好的關(guān)門程序也沒有啟動。
下面是我寫的程序,求各位大神看看邏輯在哪里出錯了,我設(shè)置的k1和k3由于減速一個正向一個反向,k2和k4用于停止,rs是熱釋電紅外傳感器檢測人體熱信號的檢測開門,hw是防夾傷的,大概的程序是想表達(dá),人來開門,延時一段時間后自動關(guān)門,關(guān)門途中檢測紅外傳感器信號有沒有人,有人就開門,沒人繼續(xù)關(guān)門,直到k4發(fā)出停止信號。
單片機(jī)源程序如下:
- #include "reg51.h"
- //電機(jī)IO
- #define GPIO_MOTOR P1
- //按鍵IO
- sbit rs=P3^0; //熱釋電傳感器
- sbit hw=P3^1; //紅外傳感器
- sbit K1=P3^2; //正轉(zhuǎn)
- sbit K2=P3^3; //反轉(zhuǎn)
- sbit K3=P3^5; //加速
- sbit K4=P3^6; //減速
- unsigned char code FFW[8]={0xf4,0xf5,0xf1,0xf9,0xf8,0xfa,0xf2,0xf6}; //逆時針
- unsigned char code FFZ[8]={0xf2,0xfa,0xf8,0xf9,0xf1,0xf5,0xf4,0xf6}; //順時針
- unsigned char Direction=0,Speed=0;
- void Delay(unsigned int t);
- void kaimen(void);
- void guanmen(void);
- void motor(void);
- void main(void)
- {
- while(1)
- {
- kaimen();
- Delay(1000);
- if(K2==0)
- {
- guanmen();
- }
- motor();
- }
- }
- void motor (void)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(Direction==1)
- {
- GPIO_MOTOR = FFW[i];
- }
- if(Direction==2)
- {
- GPIO_MOTOR = FFZ[i];
- }
- if(Direction==3)
- {
- GPIO_MOTOR = 0x00;
- }
- Delay(Speed); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- void kaimen(void)
- {
- unsigned char j;
- if(rs==0) //檢測按鍵rs是否按下
- {
- Delay(1); //消除抖動
- if(rs==0)
- {
- Speed=180; //默認(rèn)初始速度 數(shù)字越小 延時時間越短 速度越快
- Direction=1;
- }
- while((j<200)&&(rs==0)) //檢測按鍵rs是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- if(K1==0)
- {
- {
- Speed=300;
- Direction=1;
- }
- while((j<200)&&(K1==0)) //檢測按鍵K1是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- if(K2==0)
- {
- {
- Speed=300;
- Direction=3;
- }
- while((j<200)&&(K2==0)) //檢測按鍵K2是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- }
- void guanmen (void)
- {
-
- unsigned char j;
-
- if(hw==1)
- {
- Delay(1);
- if(hw==1)
- {
- Speed=180; //默認(rèn)初始速度 數(shù)字越小 延時時間越短 速度越快
- Direction=2;
- }
- while((j<200)&&(hw==1)) //檢測按鍵hw是否斷開
- {
- Delay(1);
- j++;
- }
- }
- if(K3==0)
- {
- {
- Speed=300;
- Direction=2;
- }
- while((j<200)&&(K3==0)) //檢測按鍵K3是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- if(K4==0)
- {
- {
- Speed=300;
- Direction=3;
- }
- while((j<200)&&(K4==0)) //檢測按鍵K4是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- if(hw==0)
- {
- Delay(1); //消除抖動
- if(hw==0)
- {
- Speed=180;
- Direction=1;
- }
- while((j<200)&&(hw==0)) //檢測按鍵rs是否松開
- {
- Delay(1);
- j++;
- }
- j=0;
- }
- }
- void Delay(unsigned int t)
- {
- while(t--)
- {
- ;
- }
- }
復(fù)制代碼
|
|