微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > LPC1114 红外遥控解码程序(RC5 )

LPC1114 红外遥控解码程序(RC5 )

时间:11-11 来源:互联网 点击:
  1. /********************************************************************
  2. ;
  3. ;modified LPC1114 sample code
  4. ;
  5. ; RC5 format:
  6. ; | S | F | C | 5 sytem bits | 6 command bits |
  7. ; | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 |
  8. ;
  9. ; -----+ +-+ +-+ +-+ +-+ +-+ +---+ +-+ +---+ +-+ +-+ +-+ +----
  10. ; | | | | | | | | | | | | | | | | | | | | | | | |
  11. ; +-+ +---+ +-+ +-+ +-+ +-+ +-+ +---+ +-+ +-+ +-+ +-+
  12. ;
  13. ********************************************************************/
  14. #include "LPC11xx.h" /* LPC11xx Peripheral Registers */
  15. #define RC5_INPUT_PIN (1< 0x01)
  16. #define MIN_HALF_BIT 640 // 640 us
  17. #define HALF_BIT_TIME 889 // 889 us
  18. #define MAX_HALF_BIT 1140 // 1140 us
  19. #define MIN_FULL_BIT 1340 // 1340 us
  20. #define FULL_BIT_TIME 1778 // 1778 us
  21. #define MAX_FULL_BIT 2220 // 2220 us
  22. unsigned char RC5_System; // Format 1 E/N ts4 s3 s3 s1 s0
  23. unsigned char RC5_Command; // Format 00c5 c4 c3 c2 c1 c0
  24. unsigned char RC5_flag;
  25. static signed int low_time;
  26. static signed int high_time;
  27. static unsigned char half_bit;
  28. static unsigned char sys; // system byte
  29. static unsigned char cmd; // Command byte
  30. void RC5_Init(void)
  31. {
  32. //GPIO setup
  33. //assign input for IR sensor
  34. LPC_GPIO0->DIR &= ~(RC5_INPUT_PIN);
  35. LPC_IOCON->PIO0_1 |= (1<5); //turn on hysteresis
  36. LPC_SYSCON->SYSAHBCLKCTRL |= (1<6);//enable GPIO clock
  37. NVIC_EnableIRQ(EINT0_IRQn);
  38. //timer16 0 setup
  39. LPC_SYSCON->SYSAHBCLKCTRL |= (1<7);
  40. LPC_TMR16B0->PR= 48; // prescaler 48, timer runs at 48 MHz / 48 = 1 MHz
  41. LPC_TMR16B0->MR0 = 12000; //load match register 0
  42. LPC_TMR16B0->MCR = 3; // Interrupt and Reset on MR0
  43. NVIC_EnableIRQ(TIMER_16_0_IRQn);
  44. LPC_GPIO0->IS &= ~(0x1<0x01);//interrupt sense - 0 = edge sensitive
  45. LPC_GPIO0->IBE |= 0x1<0x01; //interrupt both edges - 1 = both edges
  46. LPC_GPIO0->IE |= ( 0x1 < 0x01); //interrupt enable
  47. LPC_TMR16B0->TCR = 1; //Start timer
  48. }
  49. static void RC5_Shift_Bit(char val)
  50. {
  51. if (sys & 0x80)
  52. {
  53. if (cmd & 0x80) // command full ?
  54. {
  55. sys = 0; // yes, ERROR
  56. cmd = 0;
  57. }
  58. else
  59. cmd = (cmd < 1) | val; // shift command
  60. }
  61. else
  62. sys = (sys < 1) | val; // shift system
  63. }
  64. /************************************************************************
  65. ; RC5_Decode (we only take action at a rising edge)
  66. ;
  67. ; Half(prev) Bit Low Time High Time Action New Half Bit
  68. ; -------------------------------------------------------------------
  69. ; 0 0 0 Shift 0 0
  70. ; 0 0 1 Shift 1 1
  71. ; 0 1 0 -ERROR- *
  72. ; 0 1 1 Shift 1,0 0
  73. ; 1 0 0 Shift 1 1
  74. ; 1 0 1 -ERROR- *
  75. ; 1 1 0 Shift 1,0 0
  76. ; 1 1 1 -ERROR- *
  77. ;
  78. ***********************************************************************/
  79. static void RC5_Decode(void)
  80. {
  81. unsigned char action;
  82. action = half_bit < 2;
  83. // Check High Time
  84. if ((high_time > MIN_FULL_BIT) && (high_time < MAX_FULL_BIT))
  85. action = action | 1; // high_time = long
  86. else if (!((high_time > MIN_HALF_BIT) && (high_time < MAX_HALF_BIT)))
  87. {
  88. sys = 0; // RC5 ERROR
  89. cmd = 0;
  90. return;
  91. }
  92. // Check Low Time
  93. if ((low_time > MIN_FULL_BIT) && (low_time < MAX_FULL_BIT))
  94. action = action | 2; // low_time = long
  95. else if (!((low_time > MIN_HALF_BIT) && (low_time < MAX_HALF_BIT)))
  96. {
  97. sys = 0; // RC5 ERROR
  98. cmd = 0;
  99. return;
  100. }
  101. switch (action)
  102. {
  103. case 0:RC5_Shift_Bit(0); // short low, short high, shift 0
  104. break;
  105. case 1:RC5_Shift_Bit(1); // short low, long high, shift 1
  106. half_bit = 1; // new half bit is true
  107. break;
  108. case 2:sys = 0; // long low, short high, ERROR
  109. cmd = 0;
  110. case 3:RC5_Shift_Bit(1); // long low, long high, shift 1,0
  111. RC5_Shift_Bit(0);
  112. break;
  113. case 4:RC5_Shift_Bit(1); // short low, short high, shift 1
  114. break;
  115. case 5:sys =

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top