有了前文《红外遥控:智能学习红外遥控编码》的介绍,接下来如果要通过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;
}
}
}
} |