keilc51红外遥控解码程序

来源:本站
导读:目前正在解读《keilc51红外遥控解码程序》的相关信息,《keilc51红外遥控解码程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《keilc51红外遥控解码程序》的详细说明。

本keil c51程序适用uPC1621/uPC1622及兼容的红外遥控器芯片,占用外部中断0和定时器1,以中断方式解码,节省系统资源,以查询方式检测遥控信号是否有效.

解码思路:
红外线经一体化接受头解码放到后送到单片机的外部中断0,单片机设置外部中断下降沿触发,T0和T1为16位定时器,T0在系统启动后定时5ms.T1在外部中断0启动后开始定时,初值为0,每次在INT0中断后先读T1计数值,并重设初值为0,而且判断T1的计数值,

代码
  1. //Fosc=11.0592MHz
  2. //statesforandvariablesIRdataprocessing;
  3. typedefenum{
  4. IR_idle,
  5. IR_waitstart,
  6. IR_getaddr,
  7. IR_getaddrinv,
  8. IR_getdata,
  9. IR_getdatainv
  10. }_IRstate;
  11. _IRstateIRstate=IR_idle;
  12. unsignedcharIRaddr=0xff;
  13. unsignedchar_IRaddr=0xff;
  14. unsignedcharIRdata=0xff;
  15. unsignedchar_IRdata=0xff;
  16. unsignedcharIR_repeat=0;
  17. unsignedcharIR_ready=0;
  18. unsignedcharIR_poweron=0;
  19. //bitir_done=0;
  20. //timeconstants
  21. unsignedintIRtimer=0;//IRtimeout
  22. //cpu初始化
  23. voidcpu_init(void)
  24. {
  25. TMOD=0X11;//T0andT1十六位定时
  26. TH0=0xee;//fosc=11.0592M,timer=5ms
  27. TL0=0x00;
  28. TR0=1;//runtimer0;
  29. TF0=0;
  30. ET0=1;//enabletmr0overflowinterrupt
  31. IT0=1;//int0edgesensitive
  32. EX0=1;//enable"int0"
  33. EA=1;//globalinteruptenable
  34. }
  35. //T0中断
  36. voidtmrint()interrupt1
  37. {
  38. TH0=0xee;
  39. TL0=0x00;
  40. if(IRtimer)//IR接收超时
  41. --IRtimer;//
  42. else
  43. {
  44. IRstate=IR_idle;
  45. //IR_poweron=0;
  46. }
  47. }
  48. //Fosc=11.0592MHz
  49. #definemsec_12p50x2d00
  50. #definemsec_150x3600
  51. #definemsec_90x2066
  52. //#definemsec_90x1066
  53. #definemsec_2p50x900
  54. #definemsec_0p90x33d
  55. #definemsec_1p680x610
  56. //voidIRint()interrupt0(void)
  57. //WhentheIRreceivepingoeslowandinterruptisgenerated
  58. //IRiscollectedbystartingtimer2inthefirstfallingedgeofthepin
  59. //thenoneveryotherfallingedge,thetimervalueissavedandthetimerrestarted.
  60. //thecapturedtimeisthenusedtogettheIRdata
  61. //a"startofdata"is13.5Msec,a"1"is2.25Msec,a"0"is1.12msecanda"repeat"is11.25msec.
  62. //thecounterincrementsat1.085Usec
  63. //Iallowafairlylargetolerancetotimejitterbuttherearenofalsetriggersseen.
  64. voidIRint()interrupt0
  65. {
  66. staticunsignedcharbits;
  67. unsignedshorttime;
  68. switch(IRstate)
  69. {
  70. caseIR_idle:
  71. TL1=0;
  72. TH1=0;
  73. TR1=1;
  74. IRstate=IR_waitstart;
  75. IRtimer=26;
  76. break;
  77. caseIR_waitstart://P2_4=!P2_4;
  78. TR1=0;
  79. time=TH1;
  80. time=(time<<8)+TL1;;
  81. TL1=0;
  82. TH1=0;
  83. TR1=1;
  84. if((time>msec_12p5)&&(time<msec_15))//greaterthan12.5Msec&lessthan15msec=startcode
  85. {
  86. IRaddr=0;
  87. _IRaddr=0;
  88. IRdata=0;
  89. _IRdata=0;
  90. bits=1;
  91. IRstate=IR_getaddr;
  92. }
  93. elseif((time>msec_9)&&(time<msec_12p5))//lessthan12.5Msecandgreaterthan9msec=Repeatcode
  94. {
  95. IR_repeat=2;
  96. IRstate=IR_idle;
  97. }
  98. else
  99. {//toshort,baddatajustgotoidle
  100. IRstate=IR_idle;
  101. }
  102. break;
  103. caseIR_getaddr://P2_4=!P2_4;
  104. TR1=0;
  105. time=TH1;
  106. time=(time<<8)+TL1;;
  107. TL1=0;
  108. TH1=0;
  109. TR1=1;
  110. if((time>msec_2p5)||(time<msec_0p9))//if>2.5msecorshorterthan.9Msecbaddata,gotoidle
  111. {
  112. IRstate=IR_idle;
  113. break;
  114. }
  115. if(time>msec_1p68)//greaterthan1.68Msecisa1
  116. {
  117. IRaddr|=bits;
  118. }
  119. bits=bits<<1;
  120. if(!bits)
  121. {
  122. IRstate=IR_getaddrinv;
  123. bits=1;
  124. }
  125. break;
  126. caseIR_getaddrinv://P2_4=!P2_4;
  127. TR1=0;
  128. time=TH1;
  129. time=(time<<8)+TL1;;
  130. TL1=0;
  131. TH1=0;
  132. TR1=1;
  133. if((time>msec_2p5)||(time<msec_0p9))//if>2.5msecorshorterthan.9Msecbaddata,gotoidle
  134. {
  135. IRstate=IR_idle;
  136. break;
  137. }
  138. if(time>msec_1p68)//greaterthan1.68Msecisa1
  139. {
  140. _IRaddr|=bits;
  141. }
  142. bits=bits<<1;
  143. if(!bits)
  144. {
  145. IRstate=IR_getdata;;
  146. bits=1;
  147. }
  148. break;
  149. caseIR_getdata:
  150. TR1=0;
  151. time=TH1;
  152. time=(time<<8)+TL1;;
  153. TL1=0;
  154. TH1=0;
  155. TR1=1;
  156. if((time>msec_2p5)||(time<msec_0p9))//if>2.5msecorshorterthan.9Msecbaddata,gotoidle
  157. {
  158. IRstate=IR_idle;
  159. break;
  160. }
  161. if(time>msec_1p68)//greaterthan1.68Msecisa1
  162. {
  163. IRdata|=bits;
  164. }
  165. bits=bits<<1;
  166. if(!bits)
  167. {
  168. IRstate=IR_getdatainv;
  169. bits=1;
  170. }
  171. break;
  172. caseIR_getdatainv:
  173. TR1=0;
  174. time=TH1;
  175. time=(time<<8)+TL1;;
  176. TL1=0;
  177. TH1=0;
  178. TR1=1;
  179. if((time>msec_2p5)||(time<msec_0p9))//if>2.5msecorshorterthan.9Msecbaddata,gotoidle
  180. {
  181. IRstate=IR_idle;
  182. break;
  183. }
  184. if(time>msec_1p68)//greaterthan1.68Msecisa1
  185. {
  186. _IRdata|=bits;
  187. }
  188. bits=bits<<1;
  189. if(!bits)//wehaveitall,nowwemakesureitisaNECcodefromtheCHSIRtransmitter
  190. {//makesureaddress,~addressarecorrect,data,~dataarecorrectandaddressis0.
  191. IR_ready=((IRaddr^_IRaddr)==0xff)&&((IRdata^_IRdata)==0xff)&&(IRaddr==0);
  192. if(IR_ready)
  193. {
  194. IRstate=IR_idle;
  195. }
  196. }
  197. break;
  198. default:
  199. IRstate=IR_idle;
  200. break;
  201. }
  202. }
  203. voidmain(void)
  204. {
  205. cpu_init();
  206. while(1)
  207. {
  208. if(IR_ready)
  209. {
  210. IR_ready=0;
  211. switch(IRdata)
  212. {
  213. case0x45://1
  214. //yourcode
  215. break;
  216. case0x44://3
  217. //yourcode
  218. break;
  219. case0x43://4
  220. //yourcode
  221. break;
  222. case0x08://prev
  223. //yourcode
  224. break;
  225. case0x5a://next
  226. //yourcode
  227. break;
  228. default:
  229. break;
  230. &n bsp;}
  231. }
  232. }
  233. }

提醒:《keilc51红外遥控解码程序》最后刷新时间 2024-03-14 01:18:32,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《keilc51红外遥控解码程序》该内容的真实性请自行鉴别。