新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > MSP430(F149)學(xué)習(xí)筆記——紅外接收

MSP430(F149)學(xué)習(xí)筆記——紅外接收

作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
MSP430(F149)做紅外接收比發(fā)送更為簡單,我采用的紅外傳感器是HS0038B,這個(gè)原件在接收到38K紅外時(shí)輸出低電平,否則輸出高電平,因此,我們就可以從這點(diǎn)開始編寫程序了,由于HS0038B的電路圖很多,我這里就不貼出來了,下面是具體的代碼:
  1. #include<msp430x14x.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!=