基于单片机的红外通信软件设计

时间:2022-09-03 23:33:51

   随着短距离的通信技术的发展,红外通信已经日渐远离人们的眼球,但是在红外遥控领域应用还是比较广的。

   概念:就是将基带二进制信号通过载波信号调制成脉冲信号,通过红外发射管发射出去。载波信号的频率一般为38khz(35-42都行)。这二进制信号,通过一定的协议编码出来的,常见的是NEC协议。而红外管发射的红外线波长为0.76-1.5nm。

    需要注意的地方:NEC协议与红外通信是无关的,它只是表示了这串数据的格式,而红外通信只是将这串数据原原本本地发射出去。

    NEC协议的格式:引导码  用户码 用户码  键值码  键值反码

                          引导码:由9千us的低电平与4千5us的高电平组成;

                           后面的码值:1由1680us高电平与560us低电平组成;

                                            0由560us高电平与560us低电平组成。

                            但凡是NEC协议的编码的遥控器,用用户码用来区别的,这样不同的遥控器就不会混淆。

    38KHZ产生方法:1 有455khz的晶振分12频得出 2 由时基电路产生

    红外发射电路:基于单片机的红外通信软件设计

 

   

    红外解码c语言程序:主要通过定时器获得高低电平时间。

                            int ir_get_low()

                                               {

                                                    TL0=0;

                                                    TH0=0;

                                                    TR0=1;

                                                     while(!IR_PIN&&(TH0&0X80)==0);    //IR_PIN为数据输入口,若为高则可以跳出循环

                                                        TR0=0;

                                                       return(TH0*256+TL0);

                                                   }

                            将!IR_PIN中的!去掉就可以变成取高电平的时间了!

                             解码程序:

                                           main()

                                           {

                                             unsigned int temp;

                                             char i,j;

                                             while(1)

                                            {

                                             start:  while(IR_PIN);          //引导码先是低电平才有效

                                                       temp=   ir_get_low();

                                                       if((temp<7833)||(temp>8755))

                                                          goto start;

                                                        temp=ir_get_high();

                                                       if((temp<3686)||(temp>4608))

                                                            goto start;   

                                                         for(i=0;i<4;i++)

                                                           {   for(i=0;i<8;i++)

                                                                    {

                                                                         temp=   ir_get_low();

                                                                        if((temp<7833)||(temp>8755))

                                                                        goto start;

                                                                        temp=ir_get_high();

                                                                       if((temp<3686)||(temp>4608))

                                                                       goto start;  

                                                                        IR_BUF[i]>>=1;                       //数据从低位开始传的

                                                                                    if(temp>1032)

                                                                                          IR_BUF[i]=0x80;

                                                                       }

                                                                    }                                 

                                              }

 

 

 

                                                  }