搜索
bottom↓
回复: 5

基于2313的6122编码解码程序(原创)

[复制链接]

出0入0汤圆

发表于 2006-11-3 19:42:02 | 显示全部楼层 |阅读模式
/*------------------------------------------------------------------------------

- Device           : 2313

- Crystal       : 8.0Mhz

- Descrip          : 6122解码程序

- Revision         : Ver1.00

- Author           : 崔庆伟

- Date             : 23/03/2006

- Modify           :

------------------------------------------------------------------------------*/

#include <io2313v.h>

#include <macros.h>



//      起始位        1            0                     重复码

// +-------------+    +--------+     +------+     ........ +-------------+  +---

// |             |    |        |     |      |     ........ |             | |

//_|             |____|        |_____|      |_____........_|             |__|

// |<----irHpulse---->|<--ir1Pulse-->|<-ir0Pulse->|        |<---irRpulse--->|         

         

#define irHPulse         9000 + 4500                        // 引导码 us   

#define irRPulse         9000 + 2250                        // 重复码 us   

#define ir1Pulse         1680 + 560                        // 1   us   

#define ir0Pulse         565  + 560                        // 0   us         

#define irdelta         50                                            // 误差  us



unsigned char OK_flag;                                        // 接收完成标志

unsigned char irdata[4];                                // 接收编码缓存

unsigned char irbits;                                   // 接收有效编码位数

unsigned char ircode;                                   // 有效指令



void uart_putc        (unsigned char c);                      // 简单串口发送程序



void port_init(void)

{

        PORTB = 0x00;

        DDRB  = 0xFF;

        PORTD = 0x20;

        DDRD  = 0x00;

}



//Watchdog initialize

// prescale: 2048K

void watchdog_init(void)

{

        WDR(); //this prevents a timout on enabling

        WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget WDR

}



//UART0 initialize

// desired baud rate: 4800

// actual: baud rate:4760 (0.8%)

void uart0_init(void)

{

        UCR  = 0x00; //disable while setting baud rate

        UBRR = 0x67; //set baud rate

        UCR  = 0x18; //enable

}



void uart_putc(unsigned char c)

{

          while( !(USR & (1<<UDRE)) );

        UDR=c;

}



//TIMER1 initialize - prescale:1

// desired value: 1uSec

// actual value:  1.000uSec (0.0%)

void timer1_init(void)

{

        TCCR1B = 0x00; //stop timer

        TCNT1H = 0x00; //set count value

        TCNT1L = 0x00;

        OCR1H  = 0x00; //set compare value

        OCR1L  = 0x01;

        TCCR1A = 0x00;

        TCCR1B = 0x82; //start Timer

}

// 使能噪音抑制,下降沿触发中断

#pragma interrupt_handler timer1_capt_isr:4

void timer1_capt_isr(void)

{

        //timer 1 input capture event, read (int)value in ICR1 using;

        // value=ICR1L;   //Read low byte first (important)

        // value|=(int)ICR1H << 8; //Read high byte and shift into top byte

       

        unsigned char i,bit;

        unsigned int  times; // us

       

        times  = ICR1L;            

            times |= (unsigned int)(ICR1H << 8);

       

        TCNT1H = 0x00;                                                      // 清捕捉计数

            TCNT1L = 0x00;  



        if((times>(ir0Pulse-irdelta))&&(times<(ir0Pulse+irdelta)))         // "0"信号

                bit = 0;

        else if((times>(ir1Pulse-irdelta))&&(times<(ir1Pulse+irdelta))) // "1"信号

                bit = 1;

        else if((times>(irRPulse-irdelta))&&(times<(irRPulse+irdelta))) // repeat信号

        {

                OK_flag = 1;;

                return;                                                 // 重复,返回

        }

        else if((times>(irHPulse-irdelta))&&(times<(irHPulse+irdelta))) // header信号

        {

                irbits   = 0;

                return;                                                 // 返回,等待下次开始接收

        }

        else                                                                

                return;                                                        // 干扰信号

       

        irbits++;

       

        /*算法1:简洁、便占MCU空间大

        if ( irbits<=32 )

        {

                if(irbits%8)                                                 // irbits%8 = 1~7                       

                {

                        irdata[irbits/8] |= bit;

                        irdata[irbits/8]<<= 1;

                }

                else

                        irdata[irbits%8] |= bit;

                if(irbits==32)                             

                {

                        /* 校验 *

                        if ((irdata[0]&irdata[1]==0)&&(irdata[2]&irdata[3]==0))

                        {

                                return;                                        // 无效编码

                        }

                        else

                        {

                                /* 遥控发射先发高位,数据格式转换 *

                                bit = irdata[2];

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

                                {

                                        if (bit&0x01)

                                                ircode |= 0x01;

                                        else

                                                ircode &=~0x01;

                                        bit    >>= 1;

                                        ircode <<= 1;

                                }

                                if (bit&0x01)

                                        ircode |= 0x01;

                                else

                                        ircode &=~0x01;

                                OK_flag = 1;

                        }

                }

        }

        */

       

        /*算法2:冗长、但占MCU空间小*/

        if(irbits<8)                                                 // 用户码

        {

                irdata[0] |= bit;

                irdata[0]<<= 1;

        }

        else if(irbits==8)

                irdata[0] |= bit;

        else if(irbits<16)                                         // 用户反码

        {

                irdata[1] |= bit;

                irdata[1] <<= 1;

        }

        else if(irbits==16)                           

                irdata[1] |= bit;

        else if(irbits<24)                                          // 数据码

        {

                irdata[2] |= bit;

                irdata[2] <<= 1;

        }

        else if(irbits==24)                             

                irdata[2] |= bit;

        else if(irbits<32)                                          // 数据反码

        {

                irdata[3] |= bit;

                irdata[3] <<= 1;

        }

        else if(irbits==32)                             

        {

                irdata[3] |= bit;

                /* 校验 */

                if ((irdata[0]&irdata[1]==0)&&(irdata[2]&irdata[3]==0))

                {

                        return;                                        // 无效编码

                }

                else

                {

                        /* 遥控发射先发高位,数据格式转换 */

                        bit = irdata[2];

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

                        {

                                if (bit&0x01)

                                        ircode |= 0x01;

                                else

                                        ircode &=~0x01;

                                bit    >>= 1;

                                ircode <<= 1;

                        }

                        if (bit&0x01)

                                ircode |= 0x01;

                        else

                                ircode &=~0x01;

                        OK_flag = 1;

                }

               

        }

}



//call this routine to initialize all peripherals

void init_devices(void)

{

        //stop errant interrupts until set up

        CLI(); //disable all interrupts

        port_init();

        watchdog_init();

        timer1_init();

        uart0_init();



        MCUCR = 0x00;

        GIMSK = 0x00;

        TIMSK = 0x08;

        SEI(); //re-enable interrupts

        //all peripherals are now initialized

}



void main(void)

{

        init_devices();

       

        OK_flag = 0;

        irbits  = 0;

       

        while (1)

        {

                WDR();

                if (OK_flag)

                {

                        uart_putc(ircode);        // 通过串口将键值送出

                        OK_flag=0;

                }

        }

}

/*-------------------------------------end-----------------------------------*/

                                 
-----此内容被qingwei_cui于2006-11-03,19:43:01编辑过

出0入0汤圆

 楼主| 发表于 2006-11-3 19:44:48 | 显示全部楼层
遥控器资料:

点击此处打开armok01134285.pdf

出0入0汤圆

发表于 2006-11-3 21:04:45 | 显示全部楼层
谢谢楼主共享,收藏先!

出0入0汤圆

发表于 2010-4-20 21:36:43 | 显示全部楼层
多谢~~

出0入0汤圆

发表于 2010-8-20 20:15:47 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-1-19 10:34:07 | 显示全部楼层
mark!~
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-3 05:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表