STM8_无线315&433通用解码
单片机源程序如下:
- /*
- *********************************************************************************************************
- *
- * 模块名称 : EV1527遥控解码
- *
- 上升沿触发 触发中断后延时一段时间后检测引脚电平从而判断0 1
- 采集上升沿触发后第6 10 14 个振荡周期时的引脚电平 RF98
- PT2262 连续触发是每个周期的间隔在15MS左右
- EV1527 宽电平时间 500US 窄 250us 同步码 5.7MS
- *********************************************************************************************************
- */
- #include "EV1527.h"
- #include "led.h"
- #include "tim1.h"
- u8 EV1527_Status=0; //接收处理状态
- u8 EV1527_Receive_Count=0; //接收数据位计数
- Receive_Data EV1527_Receive_Data[48]; //24位的脉冲
- u8 EV1527_receive_ok=0; //接收完成标志
- //u8 EV1527_gather_count=3; //每个码位采集3次
- //u8 EV1527_code[3]; //用来暂时存储采集3次得到的值
- //u8 EV1527_repeat; //重复触发计数
- u8 EV1527_check_level_bit; //外部中断触发(上升沿)标志。有外部中断触发后则开始检测引脚电平是否有变化。
- u16 Interval_tim_high=0; //上升沿触发后高电平时间
- u16 Interval_tim_low=0; //上升沿触发后低电平时间
- u8 EV1527_pin_state_buf; //引脚定义缓存
- u16 EV1527_time_out_buf; //定时器超时时间缓存
- u16 TongBuMa_low=0;
- u16 a_count=0;
- bool LearningModel=FALSE; //学习模式
- bool INT_bit=FALSE;
- // 定时器2配置函数
- void EV1527_TIM1_Config(void)
- {
- TIM1_TimeBaseInit(8,TIM1_COUNTERMODE_UP,50000,0); //8分频,定时50MS,1uS的分辨率
- TIM1_ARRPreloadConfig(ENABLE);
- TIM1_ITConfig(TIM1_IT_UPDATE , ENABLE);
- TIM1_Cmd(ENABLE);//DISABLE
- }
- /*
- 初始化
- */
- void EV1527_Init()
- {
- /* 输入模式 */
- GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_PU_IT); //配置浮空输入。
- /* 将GPIOD端口设置为下降沿触发中断-*/
- EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);//下降沿触发。
- GPIO_Init(LED_PORTD, (LED_2|LED_3), GPIO_MODE_OUT_PP_HIGH_FAST );
- EV1527_TIM1_Config();
- }
- /*
- ********************************************************************************
- 定时器溢出中断内处理
- ********************************************************************************
- */
- void EV1527_TIM_IT_Updata_Handdle(void)
- {
- TIM1_SetCounter(0);
- EV1527_Status=0;
- EV1527_Receive_Count=0;
- //EV1527_repeat=0;
- //EV1527_Receive_Data=0;
- Interval_tim_low=0;
- EV1527_time_out_buf=0;
- // TIM1_Cmd(DISABLE);
- TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
-
- }
- /*
- 外部中断触发函数
- 开始接收地址码跟数据码
- */
- void EV1527_ChuLi(void)
- {
- //for(EV1527_Receive_Count=0;EV1527_Receive_Count<48;EV1527_Receive_Count++)
- EV1527_Receive_Count=0;
- while(EV1527_Receive_Count<48)
- {
- while((EV1527_port->IDR &EV1527_pin)&&(EV1527_Status==1)); //如果是高电平就等待
- Interval_tim_high=TIM1_GetCounter(); //记录高电平时间
- TIM1_SetCounter(0);
- EV1527_Receive_Data[EV1527_Receive_Count].Width=Interval_tim_high;
- EV1527_Receive_Data[EV1527_Receive_Count].Level=1; //保存到数组中
- EV1527_Receive_Count++;
- while(!(EV1527_port->IDR &EV1527_pin)&&(EV1527_Status==1)); //如果是低电平就等待
- Interval_tim_low=TIM1_GetCounter(); //记录高电平时间
- TIM1_SetCounter(0);
- EV1527_Receive_Data[EV1527_Receive_Count].Width=Interval_tim_low;
- EV1527_Receive_Data[EV1527_Receive_Count].Level=0; //保存到数组中
- EV1527_Receive_Count++;
- if(EV1527_Status==0)
- {
- EV1527_Receive_Count=49;//超时退出
- }
- }
- if(EV1527_Status==0)
- {
- EV1527_receive_ok=0;
- EV1527_Receive_Count=0;
- GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_PU_IT);//配置上啦输入。
- EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);//下降沿触发。
- TIM1_SetCounter(0);
- }
- else
- {
- EV1527_receive_ok=1;
- }
- }
- void EV1527_Receive_Handle(void)
- {
- if(INT_bit==TRUE)
- {
- INT_bit=FALSE;
- /* 开启定时器 0 记录低电平持续的时间,并检测是否为同步码 */
- a_count=0;
- TongBuMa_low=0;
- EV1527_Status=1;
- while(!(EV1527_port->IDR &EV1527_pin)&&(EV1527_Status==1)); //如果是低电平就等待
- if(EV1527_Status==TRUE) TongBuMa_low=TIM1_GetCounter(); //记录低电平时间
- TIM1_SetCounter(0); //从新计时。
- /* 检测是否为同步码*/
- if((TongBuMa_low>=5000)&&(TongBuMa_low<=38000)) //检测前一个低电平时间,是否符合同步码标
- //是同步码 关闭外部中断,并开始接收地址码跟数据码
- {
- EV1527_ChuLi();
- }
- else //不是,则重新打开中断,继续检测同步码
- {
- EV1527_Status=0;
- EV1527_Receive_Count=0;
- GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_PU_IT); //配置上啦输入。
- EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);//下降沿触发。
- TIM1_SetCounter(0); //定时器计数值清零
- }
- }
-
- }
- /*
- 数据处理
- */
- u8 EV1527_Process()
- {
- u8 EV1527_Data=0;
-
- if(EV1527_receive_ok>=1)
- {
- u8 i=0;
- u8 beishu=0;
- u8 EV1527_DataTemp[24]={0};
- u8 EV1527_addTemp_high=0;
- u8 EV1527_addTemp_low=0;
- // High_Low Level_0,Level_1; //电平
- u16 Width_0,Width_1; //宽度
- while(i<48)
- {
- // Level_0= EV1527_Receive_Data[i].Level;
- Width_0=EV1527_Receive_Data[i].Width;
- i++;
- // Level_1= EV1527_Receive_Data[i].Level;
- Width_1=EV1527_Receive_Data[i].Width;
- beishu=(Width_0>Width_1)?(Width_0/Width_1):(Width_1/Width_0);
- if(beishu>=2)//如果两个宽度的倍数大于等于2说明是有效电平
- {
- EV1527_DataTemp[i/2]= (Width_0>Width_1)?1:0;
- }
- else
- {
- i=50;
- }
- i++;
- // if(i>=46)
- // {
- // Level_0= EV1527_Receive_Data[i].Level;
- // }
- }
- if(i<49)
- {
- //解析地址
- for(i=0;i<20;i++)
- {
- if(i<10)//解析高位 EV1527_address_high
- {
- if(EV1527_DataTemp[i]==1) // 置一
- {
- EV1527_addTemp_high<<=1;
- EV1527_addTemp_high=EV1527_addTemp_high|0x01;
- }
- else if(EV1527_DataTemp[i]==0) //置零
- {
- EV1527_addTemp_high<<=1;
- }
- }
- else //解析低位
- {
- if(EV1527_DataTemp[i]==1) // 置一
- {
- EV1527_addTemp_low<<=1;
- EV1527_addTemp_low=EV1527_addTemp_low|0x01;
- }
- else if(EV1527_DataTemp[i]==0) //置零
- {
- EV1527_addTemp_low<<=1;
- }
- }
- }
- //解析数据
- for(i=20;i<24;i++)
- {
- if(EV1527_DataTemp[i]==1) // 置一
- {
- EV1527_Data<<=1;
- EV1527_Data=EV1527_Data|0x01;
- }
- else if(EV1527_DataTemp[i]==0) //置零
- {
- EV1527_Data<<=1;
- }
- }
- }
- EV1527_receive_ok=0;
- EV1527_Status=0;
- EV1527_Receive_Count=0;
- GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_PU_IT);//配置上啦输入。
- EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);//下降沿触发。
- TIM1_SetCounter(0);
- }
- return EV1527_Data;
- }
- /*
- 解码处理后的执行函数
- */
- void EV1527_Action()
- {
- EV1527_Receive_Handle();
- u8 EV1527_buf=0;
- EV1527_buf=EV1527_Process();
- if(EV1527_buf)
- {
- GPIO_WriteReverse(GPIOD, GPIO_PIN_2);
- for(u8 i=0;i<EV1527_buf;i++)
- {
- LED_Operation(GPIO_PIN_3,ON );
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- LED_Operation(GPIO_PIN_3,OFF );
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
-
- }
- //GPIO_WriteReverse(GPIOD, GPIO_PIN_3);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- Delay(6000);
- }
- }
复制代码
所有资料51hei提供下载:
STM8解码EV1527.7z
(397.25 KB, 下载次数: 68)
|