<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> STM32 >> 瀏覽文章

        紅外遙控:通過Rainbow控制家電

        作者:huqin   來源:本站原創   點擊數:  更新時間:2014年04月20日   【字體:

           有了前文《紅外遙控:智能學習紅外遙控編碼》的介紹,接下來如果要通過Rainbow的紅外遙控取控制家電就非常簡單了。

            首先我們可以通過紅外接收程序取得遙控器相應按鍵的原始編碼,然后讓Rainbow產生38K載波,再按照原始編碼中的時間序列去控制載波發送和停止發送,即可以實現對家電的控制。

            STM32是3.3V低功耗MCU,GPIO管腳的電流一般在8mA左右,如果直接去推動紅外發光二極管發送紅外指令的話,紅外發射功率很小,距離稍微遠點(1m左右)、角度對得不準的話,家電就無法接收到紅外信號,表現為家電對發送的紅外指令不理會。為了解決這一問題,Rainbow中專門增加了紅外放大電路,讓紅外發送功率增加,使得Rainbow像普通紅外遙控器一樣對家電進行控制。

            本文主要涉及到紅外發射部分,紅外原始編碼的獲取可以通過前文《紅外遙控:智能學習紅外遙控編碼》介紹的程序提取。本文增加了串口接收功能,可以接收來自上位機串口傳來的數據,并根據上位機傳來的數據控制開關空調:上位機上發送1,開空調;發送0關空調。本文的程序在美的空調上測試通過,在軟件包的“Projects\IR-Send”文件夾包含了本文的完整工程,可以直接編譯、燒寫和調試。這個程序主要展示如何在Rainbow中接收來自上位機指令并遙控空調,看看main.cpp的代碼:
        #include "WProgram.h"
        #include "IRRemote.h"

        int main()
        {
            //初始化Rainbow
            boardInit();
            //初始化默認串口
            Serial.begin();
            Serial.println("IR-Send start...");

            //定義紅外發射對象,紅外發光二極管接到TIM2的CH1,即:PA0
            IRSend irSend;
            //美的空調:開
            uint16_t rawData_1[] =       {4486,4428,590,1604,590,534,563,1605,591,1603,592,533,563,535,563,1604,591,535,563,534,564,1606,
        589,534,563,535,563,1604,590,1603,592,535,563,1604,591,1607,588,535,563,1605,590,1607,588,1604,591,
        1632,564,1602,593,1605,591,534,563,1602,593,534,563,534,563,534,563,535,563,534,563,534,563,1604,
        591,534,563,1632,563,534,564,1604,590,1605,590,534,563,534,564,534,563,1605,591,533,563,1604,591,
        535,563,534,563,1605,590,1604,592,5263,4512,4427,592,1602,593,534,563,1602,593,1632,563,535,563,
        534,563,1605,590,535,564,533,563,1607,589,534,563,534,563,1604,592,1602,593,534,563,1604,591,1630,
        564,534,563,1631,564,1604,591,1604,592,1603,592,1604,591,1633,563,534,563,1604,592,534,563,535,563,
        510,587,534,563,534,564,510,587,1630,565,510,587,1631,564,511,586,1603,592,1604,591,511,587,511,
        587,509,588,1605,590,510,587,1603,593,510,587,511,587,1603,592,1604,591};
            //美的空調:關
            uint16_t rawData_0[] =
          {4464,4451,567,1628,567,532,565,1628,567,1629,567,531,565,532,567,1626,568,531,566,532,565,1628,
        568,532,565,532,566,1627,568,1630,565,532,566,1627,568,532,566,1629,566,1628,567,1629,567,1628,567,
        532,565,1629,567,1627,568,1628,567,531,567,531,565,532,566,532,565,1627,568,531,566,532,566,1628,
        567,1629,566,1627,568,533,565,532,565,532,566,531,566,532,566,531,565,533,565,532,565,1627,568,
        1629,567,1629,567,1628,567,1627,568,5286,4489,4452,567,1629,566,532,566,1628,568,1627,568,531,566,
        531,566,1628,568,530,566,531,565,1629,567,532,565,532,565,1630,566,1629,567,532,565,1628,567,533,
        565,1629,567,1628,567,1628,592,1604,590,508,577,1616,577,1620,590,1603,567,533,589,508,589,509,589,
        508,589,1603,592,508,590,507,590,1603,591,1633,562,1543,652,509,589,509,588,509,589,508,590,508,
        589,508,590,507,589,508,589,1605,590,1604,591,1604,591,1604,592,1604,592};

            uint16_t len1 = sizeof(rawData_1)/sizeof(uint16_t);
            uint16_t len0 = sizeof(rawData_0)/sizeof(uint16_t);
            //用38K的載波進行調制
            irSend.enableIROut(38);
            //根據串口輸入情況發射紅外編碼,輸入1打開空調,輸入0關閉空調
            while(1)
            {
                if(Serial.available())
                {
                    switch(Serial.read())
                    {
                        case 0x30:
                            //關空調
                            irSend.sendRaw(rawData_0, len0);
                            Serial.println("Close conditioner...");
                            break;
                        case 0x31:
                            //開空調
                            irSend.sendRaw(rawData_1, len1);
                            Serial.println("Open conditioner...");
                            break;
                    }
                }
            }
        }

            通過Rainbow發送紅外遙控原始編碼非常簡單,只需要經過以下幾個簡單步驟:
            1、定義紅外發送對象:IRSend irSend;
            2、設置載波的頻率:irSend.enableIROut(38),這里38表示載波頻率是38K
            3、準備紅外遙控原始編碼,可以通過遙控接收程序直接取得
            4、調用發送函數按照原始編碼的定義發送出去:irSend.sendRaw(rawData_0, len0)

            上面的程序編譯無誤后,將程序燒寫到Rainbow中,并將紅外發光二極管插入到Rainbow中專為紅外發光二極管設計的接口中,將紅外發光二極管對準空調,然后在電腦上運行串口調試工具并和Rainbow連接成功,在串口調試工具中輸入1發送到Rainbow,此時美的空調將會滴的一聲打開了;再輸入0發送出去,美的空調將會滴的一聲,然后關閉電源。
         

        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮