Files
hpw421/project/example/ble/ble_peripheral/app/src/rf433.c
T
2026-07-03 18:08:25 +08:00

528 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "rf433.h"
#include "xc_drv_gpio.h"
#include "key.h"
#include "led.h"
#include "uart_1.h"
#include "switch_s.h"
#include "timeslice.h"
#include "dbg.h"
//用bit表示通道
#define CH_GPIO2 0x01
#define CH_GPIO5 0x10
uint32_t receive_temp;
uint32_t receive_data;
uint8_t high_level_time;
uint8_t low_level_time;
uint8_t receive_data_cnt;
uint8_t adress_H=0;
uint8_t adress_L=0;
uint8_t key_state=0;
uint8_t KEY_STATE_re=0; // 长按
uint8_t long_flag_s=0;
static uint8_t power=2;
uint8_t res_data=255; //433返回的最后一个控制字节(低八位)
static uint16_t long_time=0; //表示长按短按 该值一定要大于8,68是指68ms通过逻辑分析仪测量一帧433的时间
static uint16_t time_300ms=1; //表示:当长按的时候要300ms执行一次
uint8_t adress_ture_flag=0;
uint8_t adress_H_array[5]={0,0,0,0,0};
uint8_t adress_L_array[5]={0,0,0,0,0};
uint8_t adress_ch_flag[5]={0,0,0,0,0};
uint8_t adress_index_ture=0xFF;
uint8_t adress_index=0; //地址下标
uint8_t flash_ch1_state=0;
uint8_t flash_ch2_state=0;
union rf433_flag rf_flag = {0};
typedef struct {
uint32_t KEY_STATE_Click; // 单击
uint32_t KEY_STATE_LONG_PRESS; // 长按
} _Key_TypeDef;
_Key_TypeDef Key_TypeDef={0,0};
void flash_run(void)
{
set_TaskComps_timer(2,1000);
}
void rf433_gpio_init(void){
GPIO_InitCfg_t GPIO_InitCfg = { 0 }; // 清零初始化配置结构体
GPIO_InitCfg.Mux = GPIO_Mux0; // 选择功能复用器0
GPIO_InitCfg.FunSel = GPIO_Dx; // 选择GPIO功能(非特殊功能)
GPIO_InitCfg.Pull = GPIO_PULLUP; // 上拉电阻使能
GPIO_InitCfg.Dir = GPIO_DIR_INPUT; // 输出方向
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
GPIO_InitCfg.Pin = RF433_PIN; // 设置RF引脚
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
}
void chx_gpio_init(void)
{
GPIO_InitCfg_t GPIO_InitCfg = { 0 }; // 清零初始化配置结构体
GPIO_InitCfg.Mux = GPIO_Mux0; // 选择功能复用器0
GPIO_InitCfg.FunSel = GPIO_Dx; // 选择GPIO功能(非特殊功能)
GPIO_InitCfg.Pull = GPIO_NOPULL; //下拉电阻使能
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
GPIO_InitCfg.Pin = RELAY_CH1_PIN; // 设置通道5引脚
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
GPIO_InitCfg.Pin = RELAY_CH2_PIN; // 设置通道2引脚
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
if(flash_ch1_state==1)
{
xc_gpio_write_pin(GPIO_5,GPIO_PIN_SET);
}
else
{
xc_gpio_write_pin(GPIO_5,GPIO_PIN_RESET);
}
if(flash_ch2_state==1)
{
xc_gpio_write_pin(GPIO_2,GPIO_PIN_SET);
}
else
{
xc_gpio_write_pin(GPIO_2,GPIO_PIN_RESET);
}
}
//该函数选择哪个按键为单击,哪个为长按按键
static void set_click(void)
{
if(res_data==black_on||res_data==black_off||res_data==white_on_off||res_data==white_w){
Key_TypeDef.KEY_STATE_Click=1; //单击
}
else{
Key_TypeDef.KEY_STATE_LONG_PRESS=1; //长按
KEY_STATE_re=1;
}
}
void rf433_receive(void)//rf433接收,查询周期100us
{
if (RF_DATA == LOW_LEVEL)
{
low_level_time++;//记录低电平时间
rf_flag.rf_receive_flag.current_level = 0; //当前电平状态
}
else if (RF_DATA == HIGH_LEVEL)
{
high_level_time++;//记录高电平时间
if (!rf_flag.rf_receive_flag.current_level)//每个上升沿检测,0→1,一个完整的周期检测
{
if ((high_level_time > 1 && high_level_time < 7) && (low_level_time > 100 && low_level_time < 130)) //判同步码,时间间隔13ms以内 //1-7115-130 12 15 115-130 100
{
//进来这里需要13ms
rf_flag.rf_receive_flag.sync_code = 1;//同步码接收成功标志
receive_data_cnt = 0;
receive_temp = 0;
long_time=frames_time;
}
else if (rf_flag.rf_receive_flag.sync_code)//开始接收数据包
{
if ((high_level_time >= 1 && high_level_time <= 7) && (low_level_time >= 7 && low_level_time <= 16)) //数据低电平
{
receive_temp = receive_temp << 1;
receive_data_cnt++;//接收24位位数
if (receive_data_cnt == 24)//24位数据接收完成
{
//进来这个if需要40ms
rf_flag.rf_receive_flag.receive_finish = 1;//24位数据接收完成标志
rf_flag.rf_receive_flag.sync_code = 0;
receive_data = receive_temp;//将接收到的编码复制到解码寄存器中
res_data=receive_temp; //将接收到的编码复制到解码寄存器中
}
}
else if ((high_level_time >= 7 && high_level_time <= 16) && (low_level_time >= 2 && low_level_time<= 7)) //数据高电平
{
receive_temp = receive_temp << 1;
receive_temp |= 1;
receive_data_cnt++;
if (receive_data_cnt == 24)
{
//进来这个if需要40ms3748f3
rf_flag.rf_receive_flag.receive_finish = 1;//24位数据接收完成标志
rf_flag.rf_receive_flag.sync_code = 0;
receive_data = receive_temp;//将接收到的编码复制到解码寄存器中
res_data=receive_temp;
}
}
else
{
rf_flag.rf_receive_flag.sync_code = 0;//接收失败
}
long_time=frames_time;//该值一定要大与68,68是指68ms通过逻辑分析仪测量一帧的时间
}
if(long_time>0)
{ //一直按着会进入该if
//P05=0; //433LED灯
long_time--;
long_flag_s=0;
if( KEY_STATE_re==1)
{
if(time_300ms==0)
{
time_300ms=Short_time;
long_flag_s=1;
KEY_STATE_re=0;
}
else if(time_300ms>0)
time_300ms--;
}
}
else
{ //松手
time_300ms=LongPress_time;
long_time=0;
long_flag_s=0;
Key_TypeDef.KEY_STATE_Click=0;
KEY_STATE_re=0;
}
low_level_time = 0;//清零低电平时间
high_level_time = 1;//高电平时间开始计数
}
rf_flag.rf_receive_flag.current_level = 1;//当前电平状态
}
}
void app_key_callback_handler(uint16_t key_value) //按键回调函数
{
switch(key_value)
{
// case REC_KEY_UP_SHORT_UP: //短按
//
// key_state=KEY_SHORT;
// power=4;
// led_state=2;
// break;
case REC_KEY_UP_LONG_DOWN: //长按
led_state=1;
key_state=KEY_LONG;
break;
case MODE_DOUBLE_CLICK: //双击
key_state=KEY_SHORT;
power=3;
led_state=2;
break;
case MODE_QUAD_CLICK: //四击
key_state=KEY_SHORT;
power=4;
led_state=2;
break;
default:
break;
}
}
void ble_mode_scan(void)
{
if(receive_mode)
{
switch(receive_mode)
{
case 1:
receive_mode=0;
key_state=KEY_SHORT;
power=3;
led_state=2;
break;
case 2:
receive_mode=0;
key_state=KEY_SHORT;
power=4;
led_state=2;
break;
case 4:
receive_mode=0;
led_state=1;
key_state=KEY_LONG;
break;
default : break;
}
}
}
void _433_fun(void)
{
if(Key_TypeDef.KEY_STATE_Click==1||KEY_STATE_re==1) return;
set_click(); //设置单击长按
uint8_t ch_mask = adress_ch_flag[adress_index_ture];
switch(res_data)
{
case 0x03: //如果是3键遥控或者是正方形白色遥控
if(ch_mask & CH_GPIO2){
xc_gpio_toggle_pin(GPIO_2);
flash_ch2_state=xc_gpio_read_pin(GPIO_2);
}
if(ch_mask & CH_GPIO5){
xc_gpio_toggle_pin(GPIO_5);
flash_ch1_state=xc_gpio_read_pin(GPIO_5);
}
break;
case 0x04: //如果是黑色长方形遥控或者椭圆遥控关闭按键
if(ch_mask & CH_GPIO2)
{
xc_gpio_write_pin(GPIO_2,GPIO_PIN_RESET);
flash_ch2_state=xc_gpio_read_pin(GPIO_2);
}
if(ch_mask & CH_GPIO5)
{
xc_gpio_write_pin(GPIO_5,GPIO_PIN_RESET);
flash_ch1_state=xc_gpio_read_pin(GPIO_5);
}
break;
case 0x02: //如果是黑色长方形遥控或者椭圆遥控打开按键
if(ch_mask & CH_GPIO2)
{
xc_gpio_write_pin(GPIO_2,GPIO_PIN_SET);
flash_ch2_state=xc_gpio_read_pin(GPIO_2);
}
if(ch_mask & CH_GPIO5)
{
xc_gpio_write_pin(GPIO_5,GPIO_PIN_SET);
flash_ch1_state=xc_gpio_read_pin(GPIO_5);
}
break;
default:
break;
}
}
void scan_433(void)
{
uint8_t i=0;
if(rf433_sw_flag==1)
{
rf433_sw_flag=0;
power=3;
key_state=KEY_SHORT;
led_state=2;
}
else if(rf433_sw_flag==2)
{
rf433_sw_flag=0;
power=4;
key_state=KEY_SHORT;
led_state=2;
}
if(power==3 && rf_flag.rf_receive_flag.receive_finish==1 && key_state==KEY_SHORT ) //双击对码GPIO2
{
key_state=0;
power=2;
led_state=0;
uint8_t ch_mask=CH_GPIO2; //对应位掩码
adress_H=((receive_data>>16)&0xff); //读取高八位地址码
adress_L=((receive_data>>8)&0xff); //读取低八位地址码
if(adress_index>4)
{
adress_index=0;
}
adress_ture_flag=0;
for(i=0;i<5;i++)
{
if(adress_H_array[i]==adress_H && adress_L_array[i]==adress_L)
{
adress_ture_flag=1;
adress_index_ture=i; //存放找到的地址下标
adress_ch_flag[i] |= ch_mask;
break;
}
}
if(adress_ture_flag==0)
{
uint8_t current_idx = adress_index ;
adress_H_array[current_idx] = adress_H;
adress_L_array[current_idx] = adress_L;
adress_ch_flag[current_idx] = ch_mask; // 初始掩码
adress_index++;
}
}
else if(power==4 && rf_flag.rf_receive_flag.receive_finish==1 && key_state==KEY_SHORT ) //四击对码GPIO5
{
key_state=0;
power=2;
led_state=0;
uint8_t ch_mask=CH_GPIO5; //对应位掩码
adress_H=((receive_data>>16)&0xff); //读取高八位地址码
adress_L=((receive_data>>8)&0xff); //读取低八位地址码
if(adress_index>4)
{
adress_index=0;
}
adress_ture_flag=0;
for(i=0;i<5;i++)
{
if(adress_H_array[i]==adress_H && adress_L_array[i]==adress_L)
{
adress_ture_flag=1;
adress_index_ture=i; //存放找到的地址下标
adress_ch_flag[i] |= ch_mask;
break;
}
}
if(adress_ture_flag==0)
{
uint8_t current_idx = adress_index ;
adress_H_array[current_idx] = adress_H;
adress_L_array[current_idx] = adress_L;
adress_ch_flag[current_idx] = ch_mask; // 初始掩码
adress_index++;
}
}
if(power==2 && rf_flag.rf_receive_flag.receive_finish==1)
{
adress_index_ture = 0xFF; // 默认无效
for(i=0; i<5; i++)
{
if( adress_H_array[i] == ((receive_data>>16)&0xff) &&
adress_L_array[i] == ((receive_data>>8)&0xff) )
{
adress_index_ture = i; // 找到索引
break;
}
}
// 找到有效索引执行
if(adress_index_ture != 0xFF)
{
// adress_H=((receive_data>>16)&0xff); //读取高八位地址码
// adress_L=((receive_data>>8)&0xff); //读取低八位地址码
//LOGI("%d %x %x\r\n",adress_index_ture,adress_H,adress_L);
_433_fun();
flash_run();
}
rf_flag.rf_receive_flag.receive_finish=0;
}
if( key_state==KEY_LONG )
{
key_state=0;
memset(adress_H_array, 0, sizeof(adress_H_array));
memset(adress_L_array, 0, sizeof(adress_L_array));
memset(adress_ch_flag, 0, sizeof(adress_ch_flag));
adress_index=0;
flash_run();
}
}