新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > LPC1114 紅外遙控解碼程序(RC5 )

LPC1114 紅外遙控解碼程序(RC5 )

作者: 時(shí)間:2016-11-11 來(lái)源:網(wǎng)絡(luò) 收藏
  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 = 0; // short low, long high, ERROR
  116. cmd = 0;
  117. break;
  118. case 6:RC5_Shift_Bit(1); // long low, short high, shift 1,0
  119. RC5_Shift_Bit(0);
  120. half_bit = 0; // new half bit is false
  121. break;
  122. case 7:sys = 0; // long low, long high, ERROR
  123. cmd = 0;
  124. default: break; // invalid
  125. }
  126. }
  127. void PIOINT0_IRQHandler(void)
  128. {
  129. uint32_t regVal;
  130. LPC_TMR16B0->TCR = 0; //disable timer
  131. regVal =LPC_GPIO0->MIS & (0x1<<0x01);//get interrupt status
  132. if ( regVal )
  133. {
  134. if( (LPC_GPIO0->DATA & RC5_INPUT_PIN) == RC5_INPUT_PIN) // check rising or falling edge
  135. {
  136. if (sys == 0) // First pulse ?
  137. {
  138. low_time= HALF_BIT_TIME; // assume short low time
  139. high_time = HALF_BIT_TIME; // assume short high time
  140. half_bit= 1; // assume half bit is true
  141. cmd = 0x02; // = 00000010, prepare command byte
  142. }
  143. else
  144. low_time = LPC_TMR16B0->TC; // rising, so capture low time
  145. RC5_Decode();
  146. }
  147. else
  148. high_time = LPC_TMR16B0->TC; // falling, so capture high time
  149. }
  150. LPC_TMR16B0->TCR = 1; //enable timer
  151. LPC_TMR16B0->TCR = 2; //reset timer
  152. LPC_GPIO0->IC |= ( 0x1 << 0x01);//clear interrupt
  153. LPC_TMR16B0->TCR = 1; //enable timer
  154. }
  155. void TIMER16_0_IRQHandler(void)
  156. {
  157. if ( LPC_TMR16B0->IR & 0x1 )
  158. {
  159. //timeout has occured.
  160. if (cmd & 0x80) // command full ?
  161. {
  162. RC5_Command = cmd & 0x7F; // OK ! Save command byte
  163. RC5_System = sys; // save system byte
  164. RC5_flag = 1; // set event to application
  165. }
  166. sys = 0;
  167. cmd = 0;
  168. }
  169. LPC_TMR16B0->IR = 1; // clear interrupt flag
  170. }


關(guān)鍵詞: LPC1114紅外遙控解碼程

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉