|
以前買了個遙控模塊放了很久,現(xiàn)在把它拿出來做個遙控電扇,嘻嘻!但是測了一下,這個模塊在單片機上四個控制端始終顯示高電平,單片機引腳無法在低電平時進行判斷,無奈只能選擇了循環(huán)顯示的程序。本人覺得還行,按一下,低檔,按一下,二檔,再按一下,高檔,最后按一下關(guān)掉!大俠勿噴哦!
制作出來實物圖如下:
單片機源程序如下:
- /********************************************************************
- *********************************************************************/
- #include<reg52.h> //庫文件
- #define uchar unsigned char//宏定義無符號字符型
- #define uint unsigned int //宏定義無符號整型
- /********************************************************************
- 初始定義
- *********************************************************************/
- uchar count; //按鍵計數(shù),每按一下,count 加1
- uchar a,b; //定義字符型變量
- uchar temp; //定義字符型變量
- sbit BY1=P3^4; //定義按鍵的輸入端(為單片機P2口的P2.1按鍵)
- //sbit P10=P1^0;sbit P11=P1^1;sbit P12=P1^2;sbit P13=P1^3;sbit P14=P1^4;sbit P15=P1^5;sbit P16=P1^6;
- /********************************************************************
- 延時函數(shù)
- *********************************************************************/
- void delay10ms(void) //延時程序
- {
- uchar i,j;
- for(i=20;i>0;i--)
- for(j=248;j>0;j--);
- }
- /********************************************************************
- 按鍵判斷函數(shù)
- *********************************************************************/
- void key() //按鍵判斷程序
- {
- if(BY1==0) //判斷是否按下鍵盤
- {
- delay10ms(); //延時,軟件去干擾
- if(BY1==0) //確認(rèn)按鍵按下
- {
- count++; //按鍵計數(shù)加1
- if(count==4) //計8次重新計數(shù)
- {
- count=0;//將count 清零
- }
- }
- while(BY1==0);//按鍵鎖定,每按一次count 只加1.
- }
- }
- /********************************************************************
- 主函數(shù)
- *********************************************************************/
- void main()
- {
- temp=0xfe; //定義為11111110
- while(1)
- {
- key(); //調(diào)用按鍵判斷函數(shù)
-
- a=temp<<count;//左移count位
- b=temp>>(8-count);//右移8-count位
- P1=a|b; //求值 P10=
- // if(P14==0){P10=1;P11=1;P12=1;P13=1;}
- }
- }
-
- /********************************************************************
- 結(jié)束
- *********************************************************************/
復(fù)制代碼
所有資料51hei提供下載:
遙控電扇.zip
(69.26 KB, 下載次數(shù): 37)
2018-6-5 11:19 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|