Using MSP430 (f149) for infrared reception is simpler than sending. The infrared sensor I use is hs0038b. The original output is low when receiving a 38 K infrared. Otherwise, the output is high, we can start to writeProgramBecause there are many circuit diagrams of hs0038b, I will not post them here. The following is a specific example.Code:
# Include <export x14x. h> # include "delay. H "static unsigned short ADDR = 0x00; static unsigned char ir_rx_buf [256]; static unsigned char ir_rx_w_offset = 0; static unsigned char ir_rx_r_offset = 0; void ir_tx_open () {p2dir | = bit2 | bit3; // P2.2, p2.3 output p2sel | = bit3; // P2.2: Io p2.3: ta0 p2sel & = ~ Bit2; // p2out & = ~ (Bit2 | bit3); // 38 K-> p2.3 ccr0 = (INT) (26.3*8 + 0.5); cctl1 = outmod_6; CCR1 = (INT) (13.15*8 + 0.5); tactl = tassel_2 + mc_1;} void ir_set_addr (unsigned char ADDR) {ADDR = (unsigned char) (0xff & ADDR );} static void ir_start () {p2out | = bit2; delay_us (9000); p2out & = ~ Bit2; delay_us (4500);} static void ir_next () {p2out | = bit2; delay_us (9000); p2out & = ~ Bit2; delay_us (2250);} static void ir_send_byte (unsigned char c) {unsigned char I; for (I = 0; I! = 8; ++ I) {p2out | = bit2; delay_us (560); p2out & = ~ Bit2; If (C & 0x01) {delay_us (1685) ;}else {delay_us (565) ;}c >>= 1 ;}} static void ir_end () {p2out | = bit2; delay_us (300); p2out & = ~ Bit2;} void ir_put_char (unsigned char c) {ir_start (); ir_send_byte (ADDR); ir_send_byte (~ ADDR); ir_send_byte (c); ir_send_byte (~ C); ir_end ();} void ir_put_string (char * Str) {If (* Str! = '\ 0') ir_start (); else return; while (1) {ir_send_byte (ADDR); ir_send_byte (~ ADDR); ir_send_byte (* Str); ir_send_byte (~ (* Str); ++ STR; If (* Str! = '\ 0') {ir_next () ;}else {ir_end () ;}} void ir_put_array (unsigned char * array, unsigned int length) {unsigned int I; ir_start (); for (I = 0; I! = Length; ++ I) {ir_send_byte (ADDR); ir_send_byte (~ ADDR); ir_send_byte (array [I]); ir_send_byte (array [I]); if (I <length-1) {ir_next ();} else {ir_end () ;}} void ir_tx_close () {p2sel &-~ Bit3; p2dir | = bit3; p2out & = ~ Bit3; tactl = taclr;} void ir_rx_open () {p2sel & = ~ Bit0; // I/O p2dir & = ~ Bit0; // input p2ies | = bit0; // high-> low p2ie | = bit0; // enable interrupt} void ir_rx_close () {p2ie & = ~ Bit0; // disable interrupt} unsigned char ir_get () {While (ir_rx_r_offset = ir_rx_w_offset); Return ir_rx_buf [ir_rx_r_offset ++];} # pragma vector = port2_vector _ interrupt void port2_handler () {static unsigned char ir_code [4]; unsigned char I, j; unsigned short time; If (p2ifg & bit0) {p2ie & = ~ Bit0; p2ifg & = ~ Bit0; _ Eint (); time_start (9500); While (! Time_out ()&&! (P2in & bit0); time = time_end (); If (! Time_out () & time> 8500) {time_start (5000); While (! Time_out () & (p2in & bit0); time = time_end (); If (! Time_out () & time> 4000) {for (I = 0; I! = 4; ++ I) {for (j = 0; J! = 8; ++ J) {While (! (P2in & bit0); // wait for the High Level time_start (2000); // time high level while (! Time_out () & (p2in & bit0); time = time_end (); ir_code [I] >>=1; If (! Time_out () & time> 1500) {// uart_put (0x03); ir_code [I] | = 0x80 ;} else {// uart_put (0x04); ir_code [I] & = 0x7f ;}}} if (ir_code [0] = ADDR & (0xff = ir_code [0] + ir_code [1]) {If (0xff = ir_code [2] + ir_code [3]) {ir_rx_buf [ir_rx_w_offset ++] = ir_code [2] ;}}} p2ie | = bit0 ;;}}
Include the sorted infrared transmission and receipt. Here we will make a record. If you have any questions, please leave a message for discussion.