#include "ev1527.h" volatile uint32_t TIM1_cnt=0; // 定时周期 #define TIME_CYCLE 80 // #define TIME_CYCLE 20 // 定义引导码的最小和最大持续时间(单位:us) #define MIN_LEAD_CODE (5600 / TIME_CYCLE) #define MAX_LEAD_CODE (16000 / TIME_CYCLE) // 定义数据位持续时间的最小和最大范围(单位:us) #define MIN_BIT_DURATION (80 / TIME_CYCLE) #define MAX_BIT_DURATION (2400 / TIME_CYCLE) // 定义功能字节在接收缓冲区中的索引位置 #define FUNCTION_BYTE_INDEX 2 // 定义功能值 #define FUNCTION_1 0x08 #define FUNCTION_2 0x04 #define FUNCTION_3 0x02 #define FUNCTION_4 0X01 #define FUNCTION_5 0X03 #define FUNCTION_6 0X05 #define FUNCTION_7 0X07 #define FUNCTION_8 0X00 // 定义数据解码状态枚举 typedef enum { LEAD_CODE, // 引导码状态 HIGH_BIT, // 高位数据位状态 LOW_BIT, // 低位数据位状态 DATA_PROCESS, // 数据处理状态 FUNCTION_PROCESS // 功能处理状态 } Decode_State_t; // 定义全局变量和缓冲区 #define ARRAY_SIZE 128 static uint32_t Lead_Code_Count = 0; // 引导码计数 static uint32_t High_Bit_Count = 0; // 高位数据位计数 static uint32_t Low_Bit_Count = 0; // 低位数据位计数 static uint32_t High_Bit_Duration = 0; // 高位数据位持续时间 static uint32_t Low_Bit_Duration = 0; // 低位数据位持续时间 static uint8_t Received_Buffer[ARRAY_SIZE] = {0}; // 接收数据缓冲区 static uint8_t lastDataArray[ARRAY_SIZE] = {0}; // 上一次接收数据缓冲区 static uint8_t Received_Byte_Count = 0; // 接收数据字节计数 static uint8_t consecutiveEqualCount = 0; // 数据接收相同计数 static uint8_t Bit_Count = 0; // 接收数据位计数 static uint8_t Received_Data = 0; // 接收到的数据 static Decode_State_t RF_Decode_State = LEAD_CODE; // 数据解码状态 static uint32_t Lead_Code_Count_Start =0; static uint32_t High_Bit_Count_Start =0; static uint32_t Low_Bit_Count_Start =0; void Decode_Data(void); void Execute_Function(void); void Reset_Decode_Parameters(void); /**----------------------------------------------------------------------------------------------** **函数名 :EV1527端口配置 **功能说明:初始化IO口,不同单片机的配置输入模式不一样,自行修改。 **----------------------------------------------------------------------------------------------**/ void EV1527_Init(void) { DATA_433_GPIO_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; // 配置上拉输入 GPIO_InitStruct.Pin = DATA_433_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(DATA_433_GPIO_PORT, &GPIO_InitStruct); } /**----------------------------------------------------------------------------------------------** **函数名 :RF信号解码函数 **功能说明:解码从433MHz接收到的信号,并根据解码结果执行相应功能 **调用说明:80us调用一次 **----------------------------------------------------------------------------------------------**/ void RF_Signal_Decode(void) { switch (RF_Decode_State) { case LEAD_CODE: // 引导码 // 判断是否低电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_RESET) { // printf("_"); // Lead_Code_Count++; // Lead_Code_Count = TIM1_cnt - Lead_Code_Count_Start; } else // 高电平判断范围 { Lead_Code_Count = TIM1_cnt - Lead_Code_Count_Start; printf("+"); // 判断引导码范围是否合法 if (Lead_Code_Count >= MIN_LEAD_CODE && Lead_Code_Count <= MAX_LEAD_CODE) { Lead_Code_Count = 0; Reset_Decode_Parameters(); // 重置解码参数 RF_Decode_State = HIGH_BIT; // 进入高位数据位判断状态 printf("+"); } else { printf("."); Reset_Decode_Parameters(); // 引导码范围不合法,重置解码参数 } } break; case HIGH_BIT: // 判断是否高电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_SET) { // High_Bit_Count++; // High_Bit_Count = TIM1_cnt; High_Bit_Count_Start = TIM1_cnt; } else // 低电平判断范围 { High_Bit_Count = TIM1_cnt - High_Bit_Count_Start; // 判断高位数据位范围是否合法 if (High_Bit_Count >= MIN_BIT_DURATION && High_Bit_Count <= MAX_BIT_DURATION) { High_Bit_Duration = High_Bit_Count; // 保存计数值,用于区分0和1 High_Bit_Count = 0; RF_Decode_State = LOW_BIT; // 进入低位数据位判断状态 printf("-"); } else { Reset_Decode_Parameters(); // 高位数据位范围不合法,重置解码参数 } } break; case LOW_BIT: // 判断是否低电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_RESET) { //Low_Bit_Count++; // Low_Bit_Count = TIM1_cnt; Low_Bit_Count_Start = TIM1_cnt; } else // 高电平判断范围 { Low_Bit_Count = TIM1_cnt - Low_Bit_Count_Start; // 判断低位数据位范围是否合法 if (Low_Bit_Count >= MIN_BIT_DURATION && Low_Bit_Count <= MAX_BIT_DURATION) { Low_Bit_Duration = Low_Bit_Count; // 保存计数值,用于区分0和1 Low_Bit_Count = 0; RF_Decode_State = DATA_PROCESS; // 进入数据处理状态 printf("o"); } else { Reset_Decode_Parameters(); // 低位数据位范围不合法,重置解码参数 } } break; case DATA_PROCESS: Decode_Data(); // 解码数据 if (Received_Byte_Count == 3) { // 接收到全部数据,包括地址和数据 RF_Decode_State = FUNCTION_PROCESS; } else { // 数据没接收完 RF_Decode_State = HIGH_BIT; // 继续解码数据 } break; case FUNCTION_PROCESS: Execute_Function(); // 执行功能 Reset_Decode_Parameters(); // 重置解码参数 break; default: Reset_Decode_Parameters(); // 默认状态,重置解码参数 break; } } #if 0 /**----------------------------------------------------------------------------------------------** **函数名 :RF信号解码函数 **功能说明:解码从433MHz接收到的信号,并根据解码结果执行相应功能 **调用说明:80us调用一次 **----------------------------------------------------------------------------------------------**/ void RF_Signal_Decode(void) { switch (RF_Decode_State) { case LEAD_CODE: // 引导码 // 判断是否低电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_RESET) { Lead_Code_Count++; } else // 高电平判断范围 { // 判断引导码范围是否合法 if (Lead_Code_Count >= MIN_LEAD_CODE && Lead_Code_Count <= MAX_LEAD_CODE) { Lead_Code_Count = 0; Reset_Decode_Parameters(); // 重置解码参数 RF_Decode_State = HIGH_BIT; // 进入高位数据位判断状态 } else { Reset_Decode_Parameters(); // 引导码范围不合法,重置解码参数 } } break; case HIGH_BIT: // 判断是否高电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_SET) { High_Bit_Count++; } else // 低电平判断范围 { // 判断高位数据位范围是否合法 if (High_Bit_Count >= MIN_BIT_DURATION && High_Bit_Count <= MAX_BIT_DURATION) { High_Bit_Duration = High_Bit_Count; // 保存计数值,用于区分0和1 High_Bit_Count = 0; RF_Decode_State = LOW_BIT; // 进入低位数据位判断状态 } else { Reset_Decode_Parameters(); // 高位数据位范围不合法,重置解码参数 } } break; case LOW_BIT: // 判断是否低电平 if (HAL_GPIO_ReadPin(DATA_433_GPIO_PORT, DATA_433_PIN) == GPIO_PIN_RESET) { Low_Bit_Count++; } else // 高电平判断范围 { // 判断低位数据位范围是否合法 if (Low_Bit_Count >= MIN_BIT_DURATION && Low_Bit_Count <= MAX_BIT_DURATION) { Low_Bit_Duration = Low_Bit_Count; // 保存计数值,用于区分0和1 Low_Bit_Count = 0; RF_Decode_State = DATA_PROCESS; // 进入数据处理状态 } else { Reset_Decode_Parameters(); // 低位数据位范围不合法,重置解码参数 } } break; case DATA_PROCESS: Decode_Data(); // 解码数据 if (Received_Byte_Count == 3) { // 接收到全部数据,包括地址和数据 RF_Decode_State = FUNCTION_PROCESS; } else { // 数据没接收完 RF_Decode_State = HIGH_BIT; // 继续解码数据 } break; case FUNCTION_PROCESS: Execute_Function(); // 执行功能 Reset_Decode_Parameters(); // 重置解码参数 break; default: Reset_Decode_Parameters(); // 默认状态,重置解码参数 break; } } #endif /**----------------------------------------------------------------------------------------------** **函数名 :Reset_Decode_Parameters **功能说明:重置解码参数,用于开始新的解码周期 **----------------------------------------------------------------------------------------------**/ void Reset_Decode_Parameters(void) { Bit_Count = 0; Received_Data = 0x00; Received_Byte_Count = 0; Lead_Code_Count = 0; High_Bit_Count = 0; Low_Bit_Count = 0; High_Bit_Duration = 0; Low_Bit_Duration = 0; RF_Decode_State = LEAD_CODE; Lead_Code_Count_Start = TIM1_cnt; High_Bit_Count_Start = TIM1_cnt; } /**----------------------------------------------------------------------------------------------** **函数名 :Decode_Data **功能说明:解码数据位,将解码后的数据存入相应的缓冲区中 **----------------------------------------------------------------------------------------------**/ void Decode_Data(void) { Received_Data <<= 1; // 根据高低电平持续时间判断0和1,然后将数据移位存入缓冲区 if (High_Bit_Duration > Low_Bit_Duration) { Received_Data |= 0x01; } else { Received_Data &= 0xFE; } Bit_Count++; // 每接收8位数据,存入数据数组 if (Bit_Count == 8) { Received_Buffer[Received_Byte_Count] = Received_Data; Received_Data = 0x00; Bit_Count = 0; Received_Byte_Count++; } } /**----------------------------------------------------------------------------------------------** **函数名 :Execute_Function **功能说明:执行功能,根据解码后的数据进行相应操作 **----------------------------------------------------------------------------------------------**/ void Execute_Function(void) { // 判断解码后的功能字节,并执行相应操作 switch (Received_Buffer[FUNCTION_BYTE_INDEX]) { case FUNCTION_1: // 执行功能1 printf("\r\n Function_1 \r\n"); break; case FUNCTION_2: // 执行功能2 printf("\r\n Function_2 \r\n"); break; case FUNCTION_3: // 执行功能3 printf("\r\n Function_3 \r\n"); break; case FUNCTION_4: // 执行功能4 printf("\r\n Function_4 \r\n"); break; case FUNCTION_5: // 执行功能5 printf("\r\n Function_5 \r\n"); break; case FUNCTION_6: // 执行功能6 printf("\r\n Function_6 \r\n"); break; case FUNCTION_7: // 执行功能7 printf("\r\n Function_7 \r\n"); break; case FUNCTION_8: // 执行功能8 printf("\r\n Function_8 \r\n"); break; default: // 默认操作 printf("\r\n --- code =%02x",Received_Buffer[FUNCTION_BYTE_INDEX]); break; } } uint8_t RF; uint8_t decode_ok; //解码成功 uint8_t hh_w,ll_w; //高,低电平宽度 uint8_t ma_x; //接收到第几位编码了 uint8_t bma1,bma2,bma3,bma4; //用于接收过程存放遥控编码,编码比较两次,这是第一次 uint8_t mma1,mma2,mma3,mma4; uint8_t mmb1,mmb2,mmb3,mmb4; //用于接收过程存放遥控编码,第二次 //extern uint8_t mmbl,mmb2,mmb3,mmb4; uint8_t rf_okl,rf_ok2,rf_ok; //解码过程中的临时接收成功标志,接收到一个完整的遥控命令后置1,通知解码程序可以解码了 uint8_t old_rc5; //保存上一次查询到的电平状态 uint8_t tb_ok; //接收到同步的马时置1 uint8_t D0,D1,D2,D3; uint16_t s,s1; uint8_t bt_auto; //自动设置遥控接收波特率标志 extern uint8_t rf_data[4]; #if 0 #define BIT1_HIGH_US 1000 // duty cycle = #define BIT1_LOW_US 500 // #define BIT0_HIGH_US 400 #define BIT0_LOW_US 1100 #endif #if 1 // RC_PROJECTOR #define BIT1_HIGH_US 1000 // duty cycle = #define BIT1_LOW_US 500 // #define BIT0_HIGH_US 250 #define BIT0_LOW_US 1200 #define ENDING_LOW_US 12000 #define ENDING_HIGH_US 200 #endif #if 0 // SOS BUTTON #define BIT1_HIGH_US 800 // duty cycle = #define BIT1_LOW_US 300 // #define BIT0_HIGH_US 300 #define BIT0_LOW_US 800 #define ENDING_LOW_US 8000 #define ENDING_HIGH_US 245 #endif //#define CNT_SHORT_US 305 // 305 usec #define LEADING_MS 12 // 8 msec void STS_RF_write_send_1(void) { HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, SET); HAL_Delay_Us(BIT1_HIGH_US); HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, RESET); HAL_Delay_Us(BIT1_LOW_US); } void STS_RF_write_send_0(void) { HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, SET); HAL_Delay_Us(BIT0_HIGH_US); HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, RESET); HAL_Delay_Us(BIT0_LOW_US); } void STS_RF_write_send_leading(void) { #if 0 STS_RF_write_send_1(); STS_RF_write_send_0(); STS_RF_write_send_1(); #endif HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, RESET); //HAL_Delay(CNT_LONG_MS); // 8 msec long low level HAL_Delay_Us(LEADING_MS*1000); HAL_Delay_Us(LEADING_MS*1000); HAL_Delay_Us(LEADING_MS*1000); } void STS_RF_write_send_ending(void) { HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, SET); HAL_Delay_Us(ENDING_HIGH_US); HAL_GPIO_WritePin(RF_Send_GPIO_Port, RF_Send_GPIO_Pin, RESET); HAL_Delay_Us(ENDING_LOW_US); } void STS_RF_Send_Button_Multi_Times(uint8_t *rf_payload, uint8_t one_button, uint8_t rf_length, uint8_t mt) { // normally, send 5-8 cycles of same address and cmd code uint8_t push_button_cmd = one_button; for (uint8_t i=0;i< mt;i++) { rf_payload[2] |= ((push_button_cmd)); printf("%d : Address=%02X:%02X:%01x CMD=%01X\r\n",i, rf_payload[0], rf_payload[1], rf_payload[2]&0xF0, push_button_cmd); STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length); rf_payload[2] &= 0xF0; // cmd set to 0 } } void STS_RF_Send_Multi_Times(uint8_t *rf_payload, uint8_t rf_length, uint8_t mt) { // normally, send 5-8 cycles of same address and cmd code // uint8_t push_button_cmd = 0x01; for (uint8_t i=0;i< mt;i++) { rf_payload[2] |= ((i+1) <<4); printf("%d : Address=%02X:%02X:%01x CMD=%01X\r\n",i, rf_payload[0],rf_payload[1],rf_payload[2]&0x0F, (rf_payload[2]>>4)&0x0F); STS_RF_Send_AddressBit_and_CmdBit(rf_payload, rf_length); rf_payload[2] &= 0x0F; // cmd set to 0 } } void STS_RF_Send_AddressBit_and_CmdBit(uint8_t *rf_payload, uint8_t rf_length) { // send the combined address code and cmd code // 20 bit address + 4 bit cmd code == 24 bits == 3 bytes // send leading or sync signal for receiver to sync the RX clock // printf("leading sync signal ...\r\n"); //STS_RF_write_send_leading(); // printf("address bits and cmd bits ...\r\n"); // send address and cmd code for (uint8_t i=0; i < rf_length; i++) { for (uint8_t j=0; j < 8; j++) { // if (0x01&(rf_payload[i]>>j)) // TODO XXXX if (0x80&(rf_payload[i]<