450 lines
13 KiB
C
450 lines
13 KiB
C
#include "RF433.h" // Device header
|
|
#include "xc_drv_gpio.h"
|
|
#include "uart.h"
|
|
#include "xc_drv_uart.h"
|
|
#include "mode.h"
|
|
#include "timer.h"
|
|
#include "pwm.h"
|
|
union rf433_flag rf_flag = {0};
|
|
unsigned char high_level_time;
|
|
unsigned char low_level_time;
|
|
unsigned char receive_data_cnt;
|
|
uint32_t receive_temp;
|
|
uint32_t receive_data;
|
|
|
|
|
|
|
|
uint8_t res_data=255; //433返回的最后一个控制字节(低八位)
|
|
static uint16_t long_time=0; //表示长按短按 该值一定要大与68,68是指68ms通过逻辑分析仪测量一帧433的时间
|
|
static uint16_t time_300ms=1; //表示:当长按的时候要300ms执行一次
|
|
static uint8_t POWER_flag;
|
|
|
|
uint32_t flag_1h =0;
|
|
uint8_t is_time=0;
|
|
|
|
|
|
uint32_t adress=0;
|
|
uint32_t time_5s=1;
|
|
uint16_t long_press_cnt=0;
|
|
|
|
|
|
uint8_t adress_H=0;
|
|
uint8_t adress_L=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 match_success=0;
|
|
|
|
|
|
void Delay_50us(unsigned int n)
|
|
{
|
|
unsigned char i;
|
|
while(n--)
|
|
{
|
|
for(i=0;i<104;i++);
|
|
}
|
|
}
|
|
void Delay_ms(unsigned int n)
|
|
{
|
|
while(n--)
|
|
{
|
|
Delay_50us(20);
|
|
}
|
|
}
|
|
|
|
static uint8_t power=0;
|
|
void Lock_Pwm7xd(void){
|
|
power=1;
|
|
}
|
|
|
|
void rf433_gpio_init(){
|
|
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.Dir =GPIO_DIR_OUTPUT;
|
|
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
|
|
GPIO_InitCfg.Pin = RF433_PIN; // 设置LED1引脚
|
|
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
|
|
|
|
// GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
|
|
// GPIO_InitCfg.Int = NOT_INT; // 不使能中断
|
|
// GPIO_InitCfg.Pin = RF_dd; // 设置LED1引脚
|
|
// xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
|
|
}
|
|
|
|
static uint8_t ssse=0;
|
|
uint8_t long_flag_s=0;
|
|
|
|
// 在固定大小数组中添加新元素(滑动窗口)
|
|
void addToFixedArray(uint8_t arr[], uint8_t newValue) {
|
|
uint8_t i;
|
|
// 所有元素向后移动一位
|
|
for( i = 4; i > 0; i--) {
|
|
arr[i] = arr[i - 1];
|
|
}
|
|
|
|
// 在头部插入新值
|
|
arr[0] = newValue;
|
|
}
|
|
|
|
|
|
static void Encoder_key_NoCode(){
|
|
uint8_t i=0,j=0,isok=0,updata=0;
|
|
uint16_t times_s=0;
|
|
|
|
if((res_data==Brightness_H||res_data==Brightness_L||res_data==B_Brightness_H||res_data==B_Brightness_L||res_data==F_Brightness_10||res_data==F_Brightness_100)&&time_5s<=40000&&ssse==0){
|
|
long_press_cnt++;
|
|
|
|
if(long_press_cnt>200){
|
|
time_5s=40011;
|
|
ssse=1;
|
|
long_press_cnt=0;
|
|
|
|
if(res_data==Brightness_H||res_data==B_Brightness_H||res_data==F_Brightness_10){
|
|
adress=receive_data >>8;
|
|
adress_H = (receive_data >> 16) & 0xff;
|
|
adress_L = (receive_data >> 8) & 0xff;
|
|
// 第一步:检查是否已存在
|
|
for(j = 0; j <5; j++) {
|
|
if(adress_H_array[j] == adress_H && adress_L_array[j] == adress_L) {
|
|
isok=1;
|
|
break;
|
|
}
|
|
|
|
}
|
|
// 如果不存在,找空位添加
|
|
if(j==5) { // 循环完整结束,说明不存在
|
|
// 寻找空位置(值为0的位置)
|
|
for(i = 0; i <=4; i++) {
|
|
if(adress_H_array[i] == 0 && adress_L_array[i] == 0) {
|
|
adress_H_array[i] = adress_H;
|
|
adress_L_array[i] = adress_L;
|
|
|
|
isok=1;
|
|
break;
|
|
}
|
|
if(i==4){
|
|
for(j = 0; j <5; j++) {
|
|
if(adress_H_array[j] == adress_H && adress_L_array[j] == adress_L) {
|
|
updata=1;
|
|
break;
|
|
}
|
|
}
|
|
if(updata==0){
|
|
addToFixedArray(adress_H_array,adress_H);
|
|
addToFixedArray(adress_L_array,adress_L);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
times_s=300;
|
|
match_success=1;
|
|
|
|
/// EA=0;
|
|
isok=0;
|
|
if(W_PWM==0&&C_PWM==0){
|
|
if(Mode==0){
|
|
W_PWM=100;
|
|
C_PWM=0;
|
|
(void)0;/* transition */
|
|
}else {
|
|
W_PWM=100;
|
|
C_PWM=0;
|
|
(void)0;/* transition */
|
|
}
|
|
}
|
|
if(deviceStatus==POWEROFF){
|
|
startTimer(&timer1,1);
|
|
Start_PWM();
|
|
}
|
|
for (i=0;i<3;i++){
|
|
Start_PWM();
|
|
Delay_ms(times_s);
|
|
Stop_PWM();
|
|
Delay_ms(times_s);
|
|
}
|
|
|
|
Start_PWM();
|
|
startTimer(&timer1,1);
|
|
|
|
|
|
}
|
|
else if(res_data==Brightness_L||res_data==B_Brightness_L||res_data==F_Brightness_100){
|
|
adress_H_array[0] = 0xff;
|
|
adress_L_array[0] = 0xff;
|
|
adress_H_array[1] = 0xff;
|
|
adress_L_array[1] = 0xff;
|
|
adress_H_array[2] = 0xff;
|
|
adress_L_array[2] = 0xff;
|
|
adress_H_array[3] = 0xff;
|
|
adress_L_array[3] = 0xff;
|
|
adress_H_array[4] = 0xff;
|
|
adress_L_array[4] = 0xff;
|
|
match_success = 0;
|
|
times_s=200;
|
|
match_success = 0;
|
|
|
|
if(W_PWM==0&&C_PWM==0){
|
|
W_PWM=100;
|
|
C_PWM=0;
|
|
if(Mode==0){
|
|
(void)0;/* transition */
|
|
}else {
|
|
(void)0;/* transition */
|
|
}
|
|
}
|
|
if(deviceStatus==POWEROFF){
|
|
startTimer(&timer1,1);
|
|
Start_PWM();
|
|
}
|
|
for (i=0;i<3;i++){
|
|
Start_PWM();
|
|
Delay_ms(times_s);
|
|
Stop_PWM();
|
|
Delay_ms(times_s);
|
|
}
|
|
Start_PWM();
|
|
}
|
|
|
|
ssse=1;
|
|
set_TaskComps_timer(2,2000);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void Encoder_key(){
|
|
Encoder_key_NoCode(); //一对一
|
|
}
|
|
|
|
|
|
typedef struct {
|
|
uint32_t KEY_STATE_Click; // 单击
|
|
uint32_t KEY_STATE_LONG_PRESS; // 长按
|
|
} _Key_TypeDef;
|
|
_Key_TypeDef Key_TypeDef={0,0};
|
|
uint32_t KEY_STATE_re=0; // 长按
|
|
|
|
|
|
|
|
//该函数选择那个按键为单击,哪个为长按按键
|
|
static void set_click(){
|
|
if(res_data==Mode_add||res_data==Mode_dow||res_data==F_ON_OFF||res_data==F_Mode_choose||res_data==ON||res_data==OFF){
|
|
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 > 115 && low_level_time < 125)) //判同步码,时间间隔12ms
|
|
//if ((high_level_time > 4 && high_level_time < 9) && ((low_level_time > 17 && low_level_time < 23))) //判同步码,时间间隔12ms
|
|
if ((high_level_time > 1 && high_level_time < 7) && (low_level_time > 115 && low_level_time < 130)) //判同步码,时间间隔12ms
|
|
{
|
|
//进来这里需要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 < 9) && (low_level_time > 8 && low_level_time < 18)) //数据低电平
|
|
if ((high_level_time >= 1 && high_level_time <= 7) && (low_level_time >= 7 && low_level_time <= 16)) //数据低电平
|
|
{
|
|
receive_data_cnt++;//接收24位位数
|
|
receive_temp = receive_temp << 1;
|
|
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 < 18) && (low_level_time > 2 && low_level_time < 9)) //数据高电平
|
|
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;//当前电平状态
|
|
}
|
|
//
|
|
if(time_5s<=40000){
|
|
time_5s++;
|
|
}
|
|
}
|
|
|
|
uint16_t spee_dealy_time=0; //速度缓慢变换
|
|
|
|
void _433_fun(){
|
|
if(Key_TypeDef.KEY_STATE_Click==1||KEY_STATE_re==1) return;
|
|
set_click();
|
|
sss=1;//记忆标志位
|
|
switch(res_data){
|
|
//椭圆
|
|
case ON://单击
|
|
if(deviceStatus==POWEROFF){
|
|
Start_PWM();
|
|
}
|
|
break;
|
|
case OFF:
|
|
if(deviceStatus==POWERON){
|
|
Stop_PWM();
|
|
}
|
|
break;
|
|
case Mode_add:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(choose_mode_falsg!=0){ //遥控只是切换暖光,不跳模式
|
|
choose_mode_bh=0;
|
|
choose_mode_falsg=0;
|
|
startTimer(&timer1,1);
|
|
}
|
|
break;
|
|
case Mode_dow:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(choose_mode_falsg!=1){ //遥控只是切换冷光,不跳模式
|
|
choose_mode_bh=0;
|
|
choose_mode_falsg=1;
|
|
startTimer(&timer1,1);
|
|
}
|
|
break;
|
|
case Brightness_L:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(Mode==mode0){
|
|
if(Brightness <=
|
|
(BRIGHTNESS_MIN_PERCENT + BRIGHTNESS_RF_STEP_PERCENT))
|
|
{
|
|
Brightness = BRIGHTNESS_MIN_PERCENT;
|
|
}
|
|
else
|
|
{
|
|
Brightness -= BRIGHTNESS_RF_STEP_PERCENT;
|
|
}
|
|
}else {
|
|
if(Speed<10)Speed++;
|
|
}
|
|
break;
|
|
case Brightness_H:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(Mode==mode0){
|
|
if(Brightness >=
|
|
(BRIGHTNESS_MAX_PERCENT - BRIGHTNESS_RF_STEP_PERCENT))
|
|
{
|
|
Brightness = BRIGHTNESS_MAX_PERCENT;
|
|
}
|
|
else
|
|
{
|
|
Brightness += BRIGHTNESS_RF_STEP_PERCENT;
|
|
}
|
|
}else{
|
|
if(Speed>1)Speed--;
|
|
}
|
|
break;
|
|
default:
|
|
if(POWEROFF==deviceStatus)break;
|
|
if(res_data>=16&&res_data<=255){
|
|
Mode=mode0;
|
|
choose_mode_bh=0;
|
|
choose_mode_falsg=10;//圆环模式
|
|
driveMode=0;
|
|
temperature((uint8_t)(((uint16_t)(255U - res_data) * 255U + 119U) / 239U));
|
|
startTimer(&timer1,1);
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//3748
|
|
void scan_433(){
|
|
|
|
if(power==0){
|
|
if(rf_flag.rf_receive_flag.receive_finish==1){
|
|
if(match_success==1){
|
|
if((adress_H_array[0]==((receive_data >> 16) & 0xff)&&adress_L_array[0]==((receive_data >> 8) & 0xff))||
|
|
(adress_H_array[1]==((receive_data >> 16) & 0xff)&&adress_L_array[1]==((receive_data >> 8) & 0xff))||
|
|
(adress_H_array[2]==((receive_data >> 16) & 0xff)&&adress_L_array[2]==((receive_data >> 8) & 0xff))||
|
|
(adress_H_array[3]==((receive_data >> 16) & 0xff)&&adress_L_array[3]==((receive_data >> 8) & 0xff))||
|
|
(adress_H_array[4]==((receive_data >> 16) & 0xff)&&adress_L_array[4]==((receive_data >> 8) & 0xff))
|
|
){
|
|
_433_fun();
|
|
}
|
|
}else if(match_success==0){
|
|
_433_fun();
|
|
}
|
|
}
|
|
rf_flag.rf_receive_flag.receive_finish=0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|