433和315无线模块的软件编解码遥控程序

更新时间:2023-10-09 10:17:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

433和315无线模块的软件编解码遥控程序

1:单片机软件解码PT2240

2:选用PIC16F877A作为软件解码芯片 3:单片机时钟频率选用外部4MHZ晶振 4:选用外部中断脚作为编码信号脚输入脚

5:可解PT2240芯片(8脚的学习型编码芯片 编码地址位:2的20次方 重复几率100万分之一)

6:功能有:遥控器学习(DEMO上的S9作为学习按纽) 清除记忆(长按DEMO上的S9即可清除遥控器

地址的记忆)

7:学习遥控器数量可以设定(可根据EEPROM的大小 随便设定)

8:输出功能(有三路是 单击遥控器双稳,可以通过PORTC上的LED可以看到结果.还有一路是 双击

遥控器双稳)

9:可选用315MHZ/433MHZ的超再生/超外差接收模块

/******************************************************************************/

/****************************** 遥控器接收程序 ********************************/

/******************************************************************************/

#include

#include #define remote_geshu 10

/******************************************************************************/

union BIT_16 {

int TIMER1_REG; unsigned char REG[2]; }

union BIT_32 {

unsigned long data_temp_long; unsigned char data_temp_byte[4]; }

/******************************************************************************/

static union BIT_16 TIMER1_TEMP;//16位定时器1 static union BIT_32 data_temp;

/******************************************************************************/

static volatile unsigned char rec_status @ 97;

static unsigned char data_cout;//接收的遥控器码位数 static unsigned char data;//接收的4位数据 static unsigned int h_pulse;//高电平宽度 static unsigned int l_pulse;//低电平宽度

static unsigned char remote_cout;//遥控器数量 static unsigned char remote_numb;//遥控器编号

/******************************************************************************/

static unsigned char TIMER15S1;//清除学习码按键长按时间 static unsigned char TIMER15S2;//学习等待时间

static unsigned char TIMER15S3;//遥控器数据缓冲时间 static unsigned char TIMER15S4;//LED显示时间 static unsigned char TIMER15S5;// static unsigned char TIMER15S6;// static unsigned char TIMER15S7;// static unsigned char TIMER15S8;//

/******************************************************************************/

static bit head @ ((unsigned)(&rec_status)*8+(0));//同步头标志位 static bit learn @ ((unsigned)(&rec_status)*8+(1));//学习标志位

static bit recieved @((unsigned)(&rec_status)*8+(2));//接收完成标志位

static bit remote_button_status @((unsigned)(&rec_status)*8+(3));//遥控器按键标志位 static bit first_click_status @((unsigned)(&rec_status)*8+(4));//遥控器按键单击标志位

/******************************************************************************/

/********************************** 数据接收 **********************************/

/******************************************************************************/

unsigned char data_read(void) {

if(h_pulse>l_pulse) {

if((l_pulse>200)&&(l_pulse<1000)) {

if(h_pulse<(l_pulse<<2)) return 1;//数据为1 }

return 2;//无效的数据 }

else if(h_pulse

if((h_pulse>200)&&(h_pulse<1000)) {

if(l_pulse<(h_pulse<<2)) return 0;//数据为0 }

return 2;//无效的数据 } }

/******************************************************************************/

void clr_head(void)//清除寄存器 {

data_cout=0; head=0; }

/******************************************************************************/

#pragma interrupt_level 1

void check_data(void)//检测数据是否正确 {

if(head) {

switch (data_read()) {

case 0:(data_temp.data_temp_long)<<=1;;data_cout++;break; case

1:(data_temp.data_temp_long)<<=1;(data_temp.data_temp_long)++;;data_cout++;break;

default:clr_head();break; }

if(data_cout>23) {

INTE=0; recieved=1; clr_head();// } } }

/******************************************************************************/

#pragma interrupt_level 1 void check_head(void) {

if((!head)&&(!recieved))// {

if ((h_pulse>300)&&(h_pulse<1000)) {

if((l_pulse>h_pulse*27)&&(l_pulse

} //end******************>>

/******************************************************************************/

#pragma interrupt_level 1 void interrupt level_h_l(void) {

/* if(RAIF)//如果是电平中断 { PORTA=PORTA; RAIF=0;//

TIMER1_TEMP.REG[0]=TMR1L;

TIMER1_TEMP.REG[1]=TMR1H; TMR1H=0; TMR1L=0;

if(RA4)//如果是低电平中断

{ l_pulse=TIMER1_TEMP.TIMER1_REG; check_data(); check_head(); }

else//如果是高电平中断 {

h_pulse=TIMER1_TEMP.TIMER1_REG; } }*/ if(INTF) {

INTF=0;

TIMER1_TEMP.REG[0]=TMR1L; TIMER1_TEMP.REG[1]=TMR1H; TMR1H=0; TMR1L=0;

if(INTEDG)//低电平宽度 {

INTEDG=0;

l_pulse=TIMER1_TEMP.TIMER1_REG; check_data();

check_head(); } else

{

INTEDG=1;

h_pulse=TIMER1_TEMP.TIMER1_REG; } }

if(T0IF)// {

T0IF=0;

TIMER15S1++; TIMER15S2++;

TIMER15S3++; TIMER15S4++; TIMER15S5++; TIMER15S6++; TIMER15S7++; TIMER15S8++; } }

/******************************************************************************/

/********************************* 遥控器学习**********************************/

/******************************************************************************/

void clr_learn_reg(void) {

unsigned char n; di();

for (n=1;n==remote_cout*4;n++) {

eeprom_write(n,0); }

eeprom_write(70,0); eeprom_write(71,0); ei(); }

/******************************************************************************/

unsigned char compare_data(unsigned char eep_addr) {

unsigned char n;

union BIT_32 addr_data; for(n=0;n<4;n++) {

addr_data.data_temp_byte[n]=eeprom_read(eep_addr+n); }

if((data_temp.data_temp_long&0x00fffff0)==(addr_data.data_temp_long&0x00fffff0)) {

return 1;//地址匹配返回1 }

本文来源:https://www.bwwdw.com/article/b19f.html

Top