|
基于stc15w的紅外控制程序(實(shí)測(cè)stc15w408as、15w204s、15w4k56s4均可用,使用外中斷P3.3接收信號(hào)),可以和萬(wàn)能紅外遙控器和智能音箱結(jié)合實(shí)現(xiàn)遠(yuǎn)程控制和語(yǔ)音控制,前提需要知道紅外碼
紅外解碼16位解碼.zip
(57.26 KB, 下載次數(shù): 113)
2019-6-7 23:41 上傳
點(diǎn)擊文件名下載附件
紅外解碼
紅外接收(P3.3中斷).zip
(46.72 KB, 下載次數(shù): 105)
2019-12-17 20:27 上傳
點(diǎn)擊文件名下載附件
紅外控制
單片機(jī)源程序如下:
- /*********************************************************************************************
- 程序名: DHT11-1602
- 編寫(xiě)人: fby
- 硬件支持: STC89C52RC DHT11 LCD1602
- 接口說(shuō)明: DHT11:VCC DATA NC GND
- *********************************************************************************************
- 說(shuō)明:1、單片機(jī)是STC89C52RC,晶振頻率11.0592MHz,實(shí)驗(yàn)成功。
- 2、好東西要共享,大家隨意折騰~~
- *********************************************************************************************/
- #include<reg51.h>
- #include<intrins.h>
- #include<stdio.h>
- #define uchar unsigned char //無(wú)符號(hào)字符型類型數(shù)據(jù)
- #define uint unsigned int //無(wú)符號(hào)整型數(shù)據(jù)
- #define ulong unsigned long //無(wú)符號(hào)整型數(shù)據(jù)
- #define xiao float //xiaoshu
- #define IODATA P0
- unsigned char IrValue[24]; //接收收據(jù)
- unsigned char Time;
- sbit RS=P2^6; //定義rs端口
- sbit RW=P2^5; //定義rw端口
- sbit E=P2^7;
-
- sbit IRIN=P3^2;//紅外數(shù)據(jù)io
-
- /*******************************************************************************
- * 函數(shù)名 : DelayMs()
- * 函數(shù)功能 : 延時(shí)
- * 輸入 : x
- * 輸出 : 無(wú)
- *******************************************************************************/
- void DelayMs(unsigned int x) //0.14ms誤差 0us
- {
- unsigned char i;
- while(x--)
- {
- for (i = 0; i<13; i++)
- {}
- }
- }
- /*******************************************************************************
- * 函數(shù)名 : IrInit()
- * 函數(shù)功能 : 初始化紅外線接收
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- *******************************************************************************/
- void IrInit()
- {
- IT0=1;//下降沿觸發(fā)
- EX0=1;//打開(kāi)中斷0允許
- EA=1; //打開(kāi)總中斷
- IRIN=1;//初始化端口
- }
- /*******************************************************************************
- * 函數(shù)名 : ReadIr()
- * 函數(shù)功能 : 讀取紅外數(shù)值的中斷函數(shù)
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- *******************************************************************************/
- void ReadIr() interrupt 0
- {
- unsigned char j,k;
- unsigned int err;
- Time=0;
- DelayMs(70);
- if(IRIN==0) //確認(rèn)是否真的接收到正確的信號(hào)
- {
-
- err=1000; //1000*10us=10ms,超過(guò)說(shuō)明接收到錯(cuò)誤的信號(hào)
- /*當(dāng)兩個(gè)條件都為真是循環(huán),如果有一個(gè)條件為假的時(shí)候跳出循環(huán),免得程序出錯(cuò)的時(shí)
- 侯,程序死在這里*/
- while((IRIN==0)&&(err>0)) //等待前面9ms的低電平過(guò)去
- {
- DelayMs(1);
- err--;
- }
- if(IRIN==1) //如果正確等到9ms低電平
- {
- err=500;
- while((IRIN==1)&&(err>0)) //等待4.5ms的起始高電平過(guò)去
- {
- DelayMs(1);
- err--;
- }
- for(k=0;k<8;k++) //共有4組數(shù)據(jù)
- {
- for(j=0;j<8;j++) //接收一組數(shù)據(jù)
- {
- err=60;
- while((IRIN==0)&&(err>0))//等待信號(hào)前面的560us低電平過(guò)去
- // while (!IRIN)
- {
- DelayMs(1);
- err--;
- }
- err=500;
- while((IRIN==1)&&(err>0)) //計(jì)算高電平的時(shí)間長(zhǎng)度。
- {
- DelayMs(1);//0.14ms
- Time++;
- err--;
- if(Time>30)
- {
- EX0=1;
- return;
- }
- }
- IrValue[k]>>=1; //k表示第幾組數(shù)據(jù)
- if(Time>=8) //如果高電平出現(xiàn)大于565us,那么是1
- {
- IrValue[k]|=0x80;
- }
- Time=0; //用完時(shí)間要重新賦值
- }
- }
- }
-
- return;
-
- }
- }
- //數(shù)據(jù)處理
- void sjcl()
- {
- IrValue[8]=IrValue[0]>>4; //高位 用戶碼
- IrValue[9]=IrValue[0]&0x0f; //低位
- IrValue[10]=IrValue[1]>>4; //高位 用戶反碼
- IrValue[11]=IrValue[1]&0x0f; //低位
- IrValue[12]=IrValue[2]>>4; //高位 數(shù)據(jù)碼
- IrValue[13]=IrValue[2]&0x0f; //低位
- IrValue[14]=IrValue[3]>>4; //高位 數(shù)據(jù)反碼
- IrValue[15]=IrValue[3]&0x0f; //低位
-
- IrValue[16]=IrValue[4]>>4; //高位 數(shù)據(jù)反碼
- IrValue[17]=IrValue[4]&0x0f; //低位
- IrValue[18]=IrValue[5]>>4; //高位 數(shù)據(jù)反碼
- IrValue[19]=IrValue[5]&0x0f; //低位
- IrValue[20]=IrValue[6]>>4; //高位 數(shù)據(jù)反碼
- IrValue[21]=IrValue[6]&0x0f; //低位
- IrValue[22]=IrValue[7]>>4; //高位 數(shù)據(jù)反碼
- IrValue[23]=IrValue[7]&0x0f; //低位
- }
-
- /*********************************************************************************************
- 函數(shù)名:延時(shí)函數(shù)
- 調(diào) 用:Delay30us();/Delay20ms();/Delay1s();
- 參 數(shù):無(wú)
- 返回值:無(wú)
- 結(jié) 果:延時(shí)相應(yīng)時(shí)間
- 備 注:振蕩晶體為11.0592MHz(所有延時(shí)函數(shù)Copy自STC-ISP(v6.85).exe)
- **********************************************************************************************/
- void Delay30us() //@11.0592MHz-40
- {
- unsigned char i;
- i = 11;
- while (--i);
- }
- void Delay20ms() //@11.0592MHz
- {
- unsigned char i, j;
- i = 36;
- j = 217;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay1s() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- i = 8;
- j = 1;
- k = 243;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- /********************************************************************
- * 名稱 : bit Busy(void)
- * 功能 : 這個(gè)是一個(gè)讀狀態(tài)函數(shù),讀出函數(shù)是否處在忙狀態(tài)
- * 輸入 : 輸入的命令值
- * 輸出 : 無(wú)
- ***********************************************************************/
- void Busy(void)
- {
- bit busy_flag = 1;
- IODATA = 0xff;
- RS = 0; //指令關(guān)
- Delay30us();
- RW = 1; //讀狀態(tài)
- Delay30us();
- E = 1; //使能高電平
- while(1)
- {
- busy_flag = (bit)(IODATA & 0x80); //強(qiáng)制取最高位
- if(busy_flag == 0)
- {
- break;
- }
- }
- E = 0; //E 1->0,rs=0,rw=1,
- //將數(shù)據(jù)寄存器內(nèi)的數(shù)據(jù)讀到DB0~DB7,分別將狀態(tài)標(biāo)志BF和地址計(jì)數(shù)器(AC)內(nèi)容讀到DB7和DB6~DB0。
- }
- /********************************************************************
- * 名稱 : lcd1602_write_cmd(uchar del)
- * 功能 : 1602命令函數(shù)
- * 輸入 : 輸入的命令值
- * 輸出 : 無(wú)
- ***********************************************************************/
- void lcd1602_write_cmd(uchar del)
- {
- RS = 0;
- Delay30us();
- RW = 0;
- Delay30us();
- E = 0;
- Delay30us();
- IODATA = del;
- Delay30us();
- E = 1;
- Delay30us();
- E = 0; //E 1->0,rs=0,rw=0, 將指令代碼寫(xiě)入指令寄存器中
- }
- /********************************************************************
- * 名稱 : lcd1602_write_data(uchar del)
- * 功能 : 1602寫(xiě)數(shù)據(jù)函數(shù)
- * 輸入 : 需要寫(xiě)入1602的數(shù)據(jù)
- * 輸出 : 無(wú)
- ***********************************************************************/
- void lcd1602_write_data(uchar del)
- {
- Busy();
- Delay30us();
- RS = 1;
- Delay30us();
- RW = 0;
- Delay30us();
- E = 0;
- Delay30us();
- IODATA = del;
- Delay30us();
- E = 1;
- Delay30us();
- E = 0; //E 1->0,rs=1,rw=0, 數(shù)據(jù)寫(xiě)入數(shù)據(jù)寄存器中
- }
- /********************************************************************
- * 名稱 : lcd1602_init()
- * 功能 : 1602初始化,請(qǐng)參考1602的資料
- * 輸入 : 無(wú)
- * 輸出 : 無(wú)
- ***********************************************************************/
- void lcd1602_init(void)
- {
- Delay20ms();
- lcd1602_write_cmd(0x38); //8位數(shù)據(jù),雙列,5*7字形
- Delay20ms();
- lcd1602_write_cmd(0x38);
- Delay20ms();
- lcd1602_write_cmd(0x38);
- Busy();
- lcd1602_write_cmd(0x08);// 只開(kāi)顯示
- Busy();
- lcd1602_write_cmd(0x01); //清屏
- Busy();
- lcd1602_write_cmd(0x06);//顯示地址遞增,即寫(xiě)一個(gè)數(shù)據(jù)后,顯示位置右移一位
- Busy();
- lcd1602_write_cmd(0x0c); //開(kāi)啟顯示屏,關(guān)光標(biāo),光標(biāo)不閃爍
- }
- /********************************************************************
- * 名稱 : lcd1602_char(uchar hang,uchar lie,char sign)
- * 功能 : 改變液晶中某位的值,如果要讓第一行,第五個(gè)字符顯示"b" ,調(diào)用該函數(shù)如下
- L1602_char(1,5,'b')
- * 輸入 : 行,列,需要輸入1602的數(shù)據(jù)
- * 輸出 : 無(wú)
- ***********************************************************************/
- void lcd1602_char(uchar hang,uchar lie,char sign)
- {
- uchar a;
- if(hang == 1)
- {
- a = 0x80;
- }
- if(hang == 2)
- {
- a = 0xc0;
- }
- a = a + lie - 1;
- Busy();
- lcd1602_write_cmd(a); //置數(shù)據(jù)存貯器地址
- Busy();
- lcd1602_write_data(sign);
- }
- /********************************************************************
- * 名稱 : lcd1602_string(uchar hang,uchar lie,uchar *p)
- * 功能 : 改變液晶中某位的值,如果要讓第一行,第五個(gè)字符開(kāi)始顯示"ab cd ef" ,調(diào)用該函數(shù)如下
- L1602_string(1,5,"ab cd ef;")
- * 輸入 : 行,列,需要輸入1602的數(shù)據(jù)
- * 輸出 : 無(wú)
- ***********************************************************************/
- void lcd1602_string(uchar hang,uchar lie,uchar *p)
- {
- uchar a;
- if(hang == 1)
- {
- a = 0x80;
- }
- if(hang == 2)
- {
- a = 0xc0;
- }
- a = a + lie - 1;
- while(1)
- {
- Busy();
- lcd1602_write_cmd(a);
- Busy();
- lcd1602_write_data(*p);
- a++;
- p++;
- if((*p == '\0')||(a==0x90)||(a==0xd0)) //字符結(jié)束或每行最后一個(gè)字符
- {
- break;
- }
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
|
|