Gree air conditioner infrared control based on STM32
1.Introduction to infrared rays
The electromagnetic waves with wavelengths from 760nm to 400um in the spectrum are called infrared, which is a kind of invisible light. At present, almost all video and audio equipment can be controlled via infrared remote control, such as TVs, air conditioners, DVD players, etc., where infrared remote control can be seen. This technology is widely used and the corresponding application devices are very cheap. Therefore, infrared remote control is an ideal way to control our daily equipment.
2. Gree air conditioner infrared protocol learning analysis
To realize the infrared remote control function of Gree air conditioner, you must first understand the characteristics of the infrared protocol. In the infrared protocol, it generally consists of several parts such as pilot code, data code, interval code, and repeat code. Next, a piece of infrared protocol data of Gree air conditioner will be captured through a logic analyzer and its data content will be actually analyzed.
- hardware equipment:
- STM32 development board
- Infrared receiving equipment
- logic analyzer
- Gree air conditioner remote control
2.1 Collect infrared signals
Capture infrared signals through the infrared receiving device in the picture above, and use a logic analyzer to analyze the captured data content. The captured waveform is as follows:
This time the main capture is the two key value data of turning on the air conditioner and turning off the air conditioner. Next, data analysis can be performed.
2. Waveform data analysis
-
Boot code
boot code signal: 9ms low level + 4.5ms high level -
Data 0
Data 0: 0.68ms low level + 0.53ms high level -
Data 1
Data 1: 0.68ms low level + 1.6ms high level -
Separation code
Separation code: 0.68ms low level + 20ms high level -
Interval code
Interval code: 0.68ms low level + 40ms high level -
Turn on air conditioner waveform
-
Turn off air conditioner waveform
3. Enter the capture mode to collect infrared data
In order to compile subsequent learning to realize the startup and key functions of the air conditioner, first, the actual key value data can be collected through the infrared receiver, and the key value data can be saved through the timer input capture method.
In this infrared case, the infrared receiver is connected to the PB9 pin of the development board, and data is collected through timer 4 input capture.
- Timer input capture configuration
/***********************定时器4输入捕获配置***********************
** 没有重映像 完全重映像
**TIM4_CH1 PB6 PD12
**TIM4_CH2 PB7 PD13
**TIM4_CH3 PB8 PD14
**TIM4_CH4 PB9 PD15
**
******************************************************************/
void TIM4_Input_Init(u16 psc,u16 arr)//72
{
RCC->APB2ENR|=1<<3;//PB
GPIOB->CRH&=~(0xF<<1*4);
GPIOB->CRH|=0x8<<1*4;
/*定时器配置*/
RCC->APB1ENR|=1<<2;//TIM4
RCC->APB1RSTR|=1<<2;
RCC->APB1RSTR&=~(1<<2);
TIM4->CNT=0;
TIM4->PSC=psc-1;
TIM4->ARR=arr;
/*配置定时器输入捕获模式*/
TIM4->CCMR2|=0x1<<8;//CH4配置为输入,CCR4保存CH4数据
TIM4->CCER|=1<<13;//捕获发生在下降沿
TIM4->DIER|=1<<4;//开启捕获中断
STM32_NVIC_SetPriority(TIM4_IRQn,1,1);//设置优先级
TIM4->CCER|=1<<12;//开启CH1输入捕获
TIM4->CR1|=1<<0;//开启定时器
}
- Infrared data collection example
char buff[100];
u8 buff_data=0;
/**************TIM4中断服务函数***************/
void TIM4_IRQHandler(void)
{
u16 time;
u8 static recv_stat=0;
u8 static i=0;
if(TIM4->SR&1<<0)
{
}
if(TIM4->SR&1<<4)//捕获中断
{
TIM4->CNT=0;
time=TIM4->CCR4;
if(TIM4_CH4)//上升沿触发
{
TIM4->CCER|=1<<13;//下一次捕获下降沿
if(recv_stat==0)//开始接收引导码
{
if(time>=8000 && time<=10000)recv_stat=1;//成功接收9ms低电平
else recv_stat=0;
}
else if(recv_stat==2)
{
//printf("引导码接收成功\r\n");
if(time<550 || time>750) recv_stat=0;//650us间隔时间接收失败
}
}
else //下降沿触发
{
TIM4->CCER&=~(1<<13);//下一次捕获上升沿
if(recv_stat==1)
{
if(time>=3500 && time<=5500)recv_stat=2;//引导码接收成功:9ms低+4.5高
else recv_stat=0;
}
else if(recv_stat==2)//开始接收实际数据
{
if(time>=400 && time<=650)//数据0:650us低+530us高
{
buff[i++]=0;
}
else if(time>=1500 && time<=1700)//数据1:650us低+1.6ms高
{
buff[i++]=1;
}
else if(time>=18000 && time<=21000)
{
buff[i++]=2;//间隔
}
else if(time>=38000 && time<=42000)
{
//printf("i=%d\r\n",i);
buff_data=i;
recv_stat=0;
i=0;
infrared_flag=3;
}
else
{
i=0;
recv_stat=0;
infrared_data=0;
}
}
}
}
TIM4->SR=0;//清除标志
}
4. Switch machine learning example
- hardware equipment
- STM32 development board
- Infrared sending head
4.1 38KHZ carrier signal transmission
The working frequency of the infrared transmitting module used this time is 38KHZ, and the high and low levels of the data sent are 1:1. To send a high level, you need to send a 38KHZ square wave pulse signal (1:1 mode for high and low levels). To send a low level, just pull the pin low directly.
/*****************发送38KHZ载波*********************
**38KHZ载波:1/38000HZ=26us,按照高低电平1:1发送载波
**形参:u32 time --持续时间
**u8 stat --转态(1表示发送,0表示不发送)
*****************************************************/
void Infrared_Send38KHZ(u32 time,u8 stat)
{
u32 i=0;
if(stat)
{
for(i=0;i<time/13;i++)
{
RED_IE=!RED_IE;
DelayUs(13);
}
}
else//发送低电平
{
RED_IE=0;
DelayUs(time);
}
}
4.2 Example of switch key value sending function
//格力空调协议数据发送
void Infrared_GreeSendData(u8*data,int cnt)
{
/*1.发送9ms低电平*/
Infrared_Send38KHZ(9000,1);
/*2.发送4.5ms高电平*/
Infrared_Send38KHZ(4500,0);
u32 i;
for(i=0;i<cnt;i++)
{
Infrared_Send38KHZ(650,1);//发送间隔时间,650us的低电平
if(data[i]=='1')
Infrared_Send38KHZ(1600,0);//数据1
else if(data[i]=='0')
Infrared_Send38KHZ(530,0);//数据0
else if(data[i]=='2')
Infrared_Send38KHZ(20000,0);//间隔码
}
Infrared_Send38KHZ(650,1);//发送间隔时间,650us的低电平
Infrared_Send38KHZ(40000,0);//间隔码
/*1.发送9ms低电平*/
Infrared_Send38KHZ(9000,1);
/*2.发送4.5ms高电平*/
Infrared_Send38KHZ(4500,0);
for(i=0;i<cnt;i++)
{
Infrared_Send38KHZ(650,1);//发送间隔时间,650us的低电平
if(data[i]=='1')
Infrared_Send38KHZ(1600,0);//数据1
else if(data[i]=='0')
Infrared_Send38KHZ(530,0);//数据0
else if(data[i]=='2')
Infrared_Send38KHZ(20000,0);//间隔码
}
Infrared_Send38KHZ(650,1);//发送间隔时间,650us的低电平
}