MSP430 (F149) study notes - infrared reception

Publisher:EtherealGlowLatest update time:2015-08-19 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
It is easier to use MSP430 (F149) to receive infrared than to send it. The infrared sensor I use is HS0038B. This component outputs a low level when receiving 38K infrared, otherwise it outputs a high level. Therefore, we can start writing the program from this point. Since there are many circuit diagrams of HS0038B, I will not post them here. The following is the specific code:
  1. #include   
  2. #include "delay.h"  
  3.   
  4. static unsigned short addr = 0x00;  
  5. static unsigned char ir_rx_buf[256];  
  6. static unsigned char ir_rx_w_offset = 0;  
  7. static unsigned char ir_rx_r_offset = 0;  
  8.   
  9. void ir_tx_open(){  
  10.   P2DIR |= BIT2 | BIT3; //P2.2, P2.3 output  
  11.   P2SEL |= BIT3; //P2.2:IO P2.3:TA0  
  12.   P2SEL &= ~BIT2; //  
  13.   P2OUT &= ~(BIT2 | BIT3);  
  14.     
  15.   //38K->P2.3  
  16.   CCR0 = (int)(26.3*8 + 0.5);  
  17.   CCTL1 = OUTMOD_6;  
  18.   CCR1 = (int)(13.15*8 + 0.5);  
  19.   
  20.   TACTL = TASSEL_2 + MC_1;  
  21. }  
  22.   
  23. void ir_set_addr(unsigned char addr){  
  24.   addr = (unsigned char)(0xff&addr);  
  25. }  
  26.   
  27. static void ir_start(){  
  28.   P2OUT |= BIT2;  
  29.   delay_us(9000);  
  30.     
  31.   P2OUT &= ~BIT2;  
  32.   delay_us(4500);  
  33. }  
  34.   
  35. static void ir_next(){  
  36.   P2OUT |= BIT2;  
  37.   delay_us(9000);  
  38.     
  39.   P2OUT &= ~BIT2;  
  40.   delay_us(2250);  
  41. }  
  42.   
  43. static void ir_send_byte(unsigned char c){  
  44.   unsigned char i;  
  45.     
  46.   for(i = 0; i != 8; ++i){  
  47.     P2OUT |= BIT2;  
  48.     delay_us(560);  
  49.       
  50.     P2OUT &= ~BIT2;  
  51.     if(c&0x01){  
  52.       delay_us(1685);  
  53.     }  
  54.     else{  
  55.       delay_us(565);  
  56.     }  
  57.       
  58.     c >>= 1;  
  59.   }  
  60. }  
  61.   
  62. static void ir_end(){  
  63.   P2OUT |= BIT2;  
  64.   delay_us(300);  
  65.   P2OUT &= ~BIT2;  
  66. }  
  67.   
  68. void ir_put_char(unsigned char c){  
  69.   ir_start();  
  70.   ir_send_byte(addr);  
  71.   ir_send_byte(~addr);  
  72.   ir_send_byte(c);  
  73.   ir_send_byte(~c);  
  74.   ir_end();  
  75. }  
  76.   
  77. void ir_put_string(char* str){  
  78.   if(*str != '')  
  79.     ir_start();  
  80.   else  
  81.     return;  
  82.   while(1){  
  83.     ir_send_byte(addr);  
  84.     ir_send_byte(~addr);  
  85.     ir_send_byte(*str);  
  86.     ir_send_byte(~(*str));  
  87.       
  88.     ++str;  
  89.       
  90.     if(*str != ''){  
  91.       ir_next();  
  92.     }  
  93.     else{  
  94.       ir_end();  
  95.     }  
  96.   }  
  97. }  
  98.   
  99. void ir_put_array(unsigned char* array, unsigned int length){  
  100.   unsigned int i;  
  101.     
  102.   ir_start();  
  103.     
  104.   for(i = 0; i != length; ++i){  
  105.     ir_send_byte(addr);  
  106.     ir_send_byte(~addr);  
  107.     ir_send_byte(array[i]);  
  108.     ir_send_byte(array[i]);  
  109.       
  110.     if(i < length-1){  
  111.       ir_next();  
  112.     }  
  113.     else{  
  114.       ir_end();  
  115.     }  
  116.   }  
  117. }  
  118.   
  119. void ir_tx_close(){  
  120.   P2SEL &- ~BIT3;  
  121.   P2DIR |= BIT3;  
  122.   P2OUT &= ~BIT3;  
  123.   TACTL = TACLR;  
  124. }  
  125.   
  126. void ir_rx_open(){  
  127.   P2SEL &= ~BIT0; //I/O  
  128.   P2DIR &= ~BIT0; //Input  
  129.   P2IES |= BIT0;  //High->Low  
  130.   P2IE  |= BIT0;  //Enable interrupt  
  131. }  
  132.   
  133. void ir_rx_close(){  
  134.   P2IE  &= ~BIT0;  //Disable interrupt  
  135. }  
  136.   
  137. unsigned char ir_get(){  
  138.   while(ir_rx_r_offset == ir_rx_w_offset);  
  139.   return ir_rx_buf[ir_rx_r_offset++];  
  140. }  
  141.   
  142. #pragma vector=PORT2_VECTOR  
  143. __interrupt void port2_handler(){  
  144.   static unsigned char ir_code[4];  
  145.   unsigned char i, j;  
  146.   unsigned short time;  
  147.     
  148.   if(P2IFG&BIT0){  
  149.     P2IE &= ~BIT0;  
  150.     P2IFG &= ~BIT0;  
  151.     _UNITE();  
  152.       
  153.     time_start(9500);  
  154.     while(!time_out() && !(P2IN&BIT0));  
  155.     time = time_end();  
  156.     if(!time_out() && time > 8500){  
  157.       time_start(5000);  
  158.       while(!time_out() && (P2IN&BIT0));  
  159.       time = time_end();  
  160.       if(!time_out() && time > 4000){  
  161.         for(i = 0; i != 4; ++i){  
  162.           for(j = 0; j != 8; ++j){  
  163.             while(!(P2IN&BIT0));//Wait for high level  
  164.             time_start(2000); //timing high level  
  165.             while(!time_out() && (P2IN&BIT0));  
  166.             time = time_end();  
  167.             ir_code[i] >>= 1;  
  168.             if(!time_out() && time > 1500){  
  169.               //uart_put(0x03);  
  170.               ir_code[i] |= 0x80;  
  171.             }  
  172.             else{  
  173.               //uart_put(0x04);  
  174.               ir_code[i] &= 0x7f;  
  175.             }  
  176.           }  
  177.         }  
  178.           
  179.         if(ir_code[0] == addr && (0xff == ir_code[0] + ir_code[1])){  
  180.           if(0xff == ir_code[2] + ir_code[3]){  
  181.             ir_rx_buf[ir_rx_w_offset++] = ir_code[2];  
  182.           }  
  183.         }  
  184.       }  
  185.     }  
  186.       
  187.     P2IE |= BIT0;;  
  188.   }  
  189. }  
Contains the organized infrared sending and receiving, put here as a record. If you have any questions, please leave a message for discussion.
Keywords:MSP430 Reference address:MSP430 (F149) study notes - infrared reception

Previous article:12864 LCD circular display of Chinese characters on three screens
Next article:MSP430 (f149) study notes - infrared remote control transmitter

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号