微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > MSP430(F149)学习笔记——红外接收

MSP430(F149)学习笔记——红外接收

时间:11-28 来源:互联网 点击:
MSP430(F149)做红外接收比发送更为简单,我采用的红外传感器是HS0038B,这个原件在接收到38K红外时输出低电平,否则输出高电平,因此,我们就可以从这点开始编写程序了,由于HS0038B的电路图很多,我这里就不贴出来了,下面是具体的代码:

  1. #includemsp430x14x.h>
  2. #include"delay.h"
  3. staticunsignedshortaddr=0x00;
  4. staticunsignedcharir_rx_buf[256];
  5. staticunsignedcharir_rx_w_offset=0;
  6. staticunsignedcharir_rx_r_offset=0;
  7. voidir_tx_open(){
  8. P2DIR|=BIT2|BIT3;//P2.2,P2.3输出
  9. P2SEL|=BIT3;//P2.2:IOP2.3:TA0
  10. P2SEL&=~BIT2;//
  11. P2OUT&=~(BIT2|BIT3);
  12. //38K->P2.3
  13. CCR0=(int)(26.3*8+0.5);
  14. CCTL1=OUTMOD_6;
  15. CCR1=(int)(13.15*8+0.5);
  16. TACTL=TASSEL_2+MC_1;
  17. }
  18. voidir_set_addr(unsignedcharaddr){
  19. addr=(unsignedchar)(0xff&addr);
  20. }
  21. staticvoidir_start(){
  22. P2OUT|=BIT2;
  23. delay_us(9000);
  24. P2OUT&=~BIT2;
  25. delay_us(4500);
  26. }
  27. staticvoidir_next(){
  28. P2OUT|=BIT2;
  29. delay_us(9000);
  30. P2OUT&=~BIT2;
  31. delay_us(2250);
  32. }
  33. staticvoidir_send_byte(unsignedcharc){
  34. unsignedchari;
  35. for(i=0;i!=8;++i){
  36. P2OUT|=BIT2;
  37. delay_us(560);
  38. P2OUT&=~BIT2;
  39. if(c&0x01){
  40. delay_us(1685);
  41. }
  42. else{
  43. delay_us(565);
  44. }
  45. c>>=1;
  46. }
  47. }
  48. staticvoidir_end(){
  49. P2OUT|=BIT2;
  50. delay_us(300);
  51. P2OUT&=~BIT2;
  52. }
  53. voidir_put_char(unsignedcharc){
  54. ir_start();
  55. ir_send_byte(addr);
  56. ir_send_byte(~addr);
  57. ir_send_byte(c);
  58. ir_send_byte(~c);
  59. ir_end();
  60. }
  61. voidir_put_string(char*str){
  62. if(*str!=)
  63. ir_start();
  64. else
  65. return;
  66. while(1){
  67. ir_send_byte(addr);
  68. ir_send_byte(~addr);
  69. ir_send_byte(*str);
  70. ir_send_byte(~(*str));
  71. ++str;
  72. if(*str!=){
  73. ir_next();
  74. }
  75. else{
  76. ir_end();
  77. }
  78. }
  79. }
  80. voidir_put_array(unsignedchar*array,unsignedintlength){
  81. unsignedinti;
  82. ir_start();
  83. for(i=0;i!=length;++i){
  84. ir_send_byte(addr);
  85. ir_send_byte(~addr);
  86. ir_send_byte(array[i]);
  87. ir_send_byte(array[i]);
  88. if(i
  89. ir_next();
  90. }
  91. else{
  92. ir_end();
  93. }
  94. }
  95. }
  96. voidir_tx_close(){
  97. P2SEL&-~BIT3;
  98. P2DIR|=BIT3;
  99. P2OUT&=~BIT3;
  100. TACTL=TACLR;
  101. }
  102. voidir_rx_open(){
  103. P2SEL&=~BIT0;//I/O
  104. P2DIR&=~BIT0;//Input
  105. P2IES|=BIT0;//High->Low
  106. P2IE|=BIT0;//Enableinterrupt
  107. }
  108. voidir_rx_close(){
  109. P2IE&=~BIT0;//Disableinterrupt
  110. }
  111. unsignedcharir_get(){
  112. while(ir_rx_r_offset==ir_rx_w_offset);
  113. returnir_rx_buf[ir_rx_r_offset++];
  114. }
  115. #pragmavector=PORT2_VECTOR
  116. __interruptvoidport2_handler(){
  117. staticunsignedcharir_code[4];
  118. unsignedchari,j;
  119. unsignedshorttime;
  120. if(P2IFG&BIT0){
  121. P2IE&=~BIT0;
  122. P2IFG&=~BIT0;
  123. _EINT();
  124. time_start(9500);
  125. while(!time_out()&&!(P2IN&BIT0));
  126. time=time_end();
  127. if(!time_out()&&time>8500){
  128. time_start(5000);
  129. while(!time_out()&&(P2IN&BIT0));
  130. time=time_end();
  131. if(!time_out()&&time>4000){
  132. for(i=0;i!=4;++i){
  133. for(j=0;j!=8;++j){
  134. while(!(P2IN&BIT0));//等待高电平
  135. time_start(2000);//计时高电平
  136. while(!time_out()&&(P2IN&BIT0));
  137. time=time_end();
  138. ir_code[i]>>=1;
  139. if(!time_out()&&time>1500){
  140. //uart_put(0x03);
  141. ir_code[i]|=0x80;
  142. }
  143. else{
  144. //uart_put(0x04);
  145. ir_code[i]&=0x7f;
  146. }
  147. }
  148. }
  149. if(ir_code[0]==addr&&(0xff==ir_code[0]+ir_code[1])){
  150. if(0xff==ir_code[2]+ir_code[3]){
  151. ir_rx_buf[ir_rx_w_offset++]=ir_code[2];
  152. }
  153. }
  154. }
  155. }
  156. P2IE|=BIT0;;
  157. }
  158. }

包含整理后的红外发送和接收,放在这里做个记录,有什么疑问欢迎留言讨论。

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

网站地图

Top