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 <PIC.h>

  #include <PIC1687x.h>

  #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<l_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<h_pulse*35))

  {

  head=1;

  }

  }

  }

  }

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

  #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

  }

  return 0;//地址不匹配返回0

  }

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

  unsigned char compare_all_data(void)

  {

  unsigned char n;

  for(n=0;n<remote_cout;n++)//在已经学习好的遥控器地址里比较有没有相同的

  {

  if(compare_data(n*4+1))//如果有相同的遥控器

  {

  return 1;

  }

  }

  return 0;//没有一个地址是相同的

  }

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

  //读遥控器的数量

  void read_remote_cout(void)

  {

  remote_cout=eeprom_read(71);//读出已经学习的遥控器总数量

  if(remote_cout>20)remote_cout=0; //如果EEPROM是空的则为0

  }

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

  void check_learn_pro(void)

  {

  unsigned char n;

  if(learn)

  {

  learn=0;

  remote_numb=eeprom_read(70);//读出现在可以覆盖掉哪个遥控器的编号

  read_remote_cout();////读遥控器的数量

  if(remote_numb>(remote_geshu-1))remote_numb=0;//如果遥控器的编号已经是最大的了 则从小开始

  if((remote_cout==0)||!compare_all_data())//如果还没有遥控器学习或没有相同地址的遥控器学习

  {

  di();

  for(n=0;n<4;n++)

  {

  eeprom_write(remote_numb*4+n+1,data_temp.data_temp_byte[n]);

  }

  remote_numb++;

  if(remote_cout<remote_geshu+1)remote_cout++;//已经学习好的遥控器数量

  eeprom_write(71,remote_cout);//保存已经学习好的遥控器总数量

  eeprom_write(70,remote_numb);//保存已学习的遥控起编号

  ei();

  }

单机接解码(单解码游戏规则)

  }

  }

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

  void check_out_pro(void)

  {

  read_remote_cout();//读遥控器的数量

  if(compare_all_data())

  {

  data=data_temp.data_temp_byte[0]&0x0f;//

  TIMER15S3=0;

  TIMER15S4=0;

  RC4=1;

  }

  }

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

  void decode_init(void)//接收初始化

  {

  OPTION=0x87;

  RBPU=0;

  TMR0=0;

  T0IE=1;//使能定时器0中断

  INTE=1;

  TMR1ON=1;//

  PORTC=0x00;//

  TRISC=0x00;//

  ei();//开放全局中断

  }

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

  void check_remote_recieved(void)//检测有无新的数据

  {

  if(!RB1)//

  {

  if(TIMER15S1>30)//是否长按了3秒

  {

  learn=0;//

  clr_learn_reg();

  RC4=0;

  }

  else

  {

  learn=1;//

  TIMER15S2=0;

  TIMER15S4=0;

  RC4=1;

  }

  }

  else

  {

  TIMER15S1=0;

  if(TIMER15S2>60)

  {

  learn=0;//

  }

  }

  if(TIMER15S3>2)//数据保持时间

  {

  data=0;

  TIMER15S3=0;

  }

  if(TIMER15S4>3)//LED显示时间

  {

  RC4=0;

  }

  if(recieved)

  {

  TIMER15S3=0;

  recieved=0;

  TIMER15S4=0;

  RC4=1;

  check_learn_pro();

  check_out_pro();

  }

  INTE=1;

  }

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

  /********************************* 控制部分程序 *******************************/

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

  void control_init(void)

  {

  ;

  }

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

  /*********************************声音部分程序*********************************/

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

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

  unsigned char delay(unsigned int nus)

  {

  for(;nus>0;nus--)

  {

  if(recieved==1) return 0;

  asm("nop");

  }

  }

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

  unsigned char soud_one_fre(unsigned int cout,unsigned int delay_time)//发音程序

  {

  if(recieved==1) return 0;

  INTE=0;

  for (;cout>0;cout--)

  {

  RC6=!RC6;

  delay(delay_time);

  }

  RC6=0;

  INTE=1;

  }

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

  /*********************************急促的声音***********************************/

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

  unsigned char soud_one_fre0(unsigned int time,unsigned int delay_time)

  {

  for (;time>0;time--)

  {

  if(recieved==1) return 0;

  soud_one_fre(1000,10);

  delay(delay_time);

  }

  }

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

  /*********************************救护的声音***********************************/

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

  unsigned char soud_tow_fre0(unsigned int time)//

  {

  for (;time>0;time--)

  {

  if(recieved==1) return 0;

  soud_one_fre(900,10);

  soud_one_fre(800,30);

  }

  }

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

  /************************************低-高音***********************************/

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

  unsigned char soud_many_fre0(unsigned int time,unsigned int cout)

  {

  for (;time>0;time--)

  {

  unsigned int delay_time;

  for(delay_time=50;delay_time>10;delay_time--)

  {

  if(recieved==1) return 0;

  soud_one_fre(cout,delay_time);

  }

  }

  }

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

  /************************************低-高音1**********************************/

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

  unsigned char soud_many_fre2(unsigned int time,unsigned int cout)

  {

  for (;time>0;time--)

  {

  unsigned int delay_time;

  for(delay_time=30;delay_time>20;delay_time--)

  {

  if(recieved==1) return 0;

  soud_one_fre(cout,delay_time);

  }

  }

  }

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

  /***********************************高—低音************************************/

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

  unsigned char soud_many_fre1(unsigned int time,unsigned int cout)

  {

  for (;time>0;time--)

  {

  unsigned int delay_time;

  for(delay_time=10;delay_time<50;delay_time++)

单机接解码(单解码游戏规则)

  {

  if(recieved==1) return 0;

  soud_one_fre(cout,delay_time);

  }

  }

  }

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

  /***********************************高—低音1***********************************/

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

  unsigned char soud_many_fre3(unsigned int time,unsigned int cout)

  {

  for (;time>0;time--)

  {

  unsigned int delay_time;

  for(delay_time=20;delay_time<30;delay_time++)

  {

  if(recieved==1) return 0;

  soud_one_fre(cout,delay_time);

  }

  }

  }

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

  /**********************************汽车报警声**********************************/

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

  unsigned char soud_low_high_low(unsigned int cout)

  {

  for (;cout>0;cout--)

  {

  if(recieved==1) return 0;

  soud_many_fre0(1,10);

  soud_many_fre2(1,10);

  }

  }

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

  unsigned char remote_check(void)//遥控器按键检测

  {

  if(data)//

  {

  if(remote_button_status)

  {

  return 0;

  }

  else

  {

  remote_button_status=1;

  return data;//遥控器数据有效

  }

  }

  else

  {

  remote_button_status=0;

  return 0;

  }

  }

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

  void check_remote_command(void)

  {

  if(TIMER15S5>10)

  {

  first_click_status=0;//

  }

  switch (remote_check())

  {

  case 0x01:

  {

  RC0=!RC0;

  // soud_one_fre0(1,6000);

  soud_tow_fre0(5);

  delay(10000);

  soud_many_fre0(5,40);

  delay(10000);

  soud_many_fre2(10,100);

  delay(10000);

  soud_many_fre1(5,40);

  delay(10000);

  soud_many_fre3(10,150);

  delay(10000);

  soud_low_high_low(5);

  delay(10000);

  soud_one_fre0(2,10000);

  break;

  }

  case 0x02:RC1=!RC1;break;

  case 0x04:RC2=!RC2;break;

  case 0x08:

  {

  if(first_click_status==1)

  {

  RC3=!RC3;

  first_click_status=0;//

  break;

  }

  else

  {

  first_click_status=1;

  TIMER15S5=0;

  break;

  }

  }

  default:break;

  }

  }

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

  void control_pro(void)

  {

  check_remote_command();

  }

  原文链接:https://www.eeworld.com.cn/mcu/article_2016111731476.html