久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2463|回復(fù): 5
打印 上一主題 下一主題
收起左側(cè)

這個(gè)單片機(jī)紅外解碼程序?yàn)槭裁床慌袛嘤脩舸a?只要數(shù)據(jù)碼對(duì)就能正常

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:206067 發(fā)表于 2020-1-29 01:03 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
從論壇看到的一位朋友發(fā)的紅外解碼程序
請(qǐng)問(wèn)大家這個(gè)程序?yàn)槭裁床慌袛嘤脩舸a  只要數(shù)據(jù)碼對(duì)就能正常  
紅色字體的用戶碼 我的這個(gè)遙控器實(shí)際用戶碼是01FE


單片機(jī)源程序如下:
/*************        用戶系統(tǒng)配置        **************/
#define MAIN_Fosc                12000000L        //定義主時(shí)鐘, 模擬串口和紅外接收會(huì)自動(dòng)適應(yīng)。5~36MHZ
#define D_TIMER0                125                        //選擇定時(shí)器時(shí)間, us, 紅外接收要求在60us~250us之間
#define        User_code                0xB649                //定義紅外接收用戶碼

/*************        以下宏定義用戶請(qǐng)勿修改        **************/
#include        "reg51.H"
#define        uchar        unsigned char
#define uint        unsigned int   
#define freq_base                        (MAIN_Fosc / 1200)
#define Timer0_Reload                (65536 - (D_TIMER0 * freq_base / 10000))
/*************        本地常量聲明        **************/         
/*************        本地變量聲明        **************/



sbit        P_IR_RX = P3^2;                //定義紅外接收輸入端口

sbit        userLed1=P3^0;
sbit        userLed2=P3^1;
sbit        userLed3=P3^3;
sbit         userLed4=P3^4;
sbit         userLed5=P3^5;                    



bit                P_IR_RX_temp;                //Last sample
bit                B_IR_Sync;                        //已收到同步標(biāo)志
uchar        IR_SampleCnt;                //采樣計(jì)數(shù)
uchar        IR_BitCnt;                        //編碼位數(shù)
uchar        IR_UserH;                        //用戶碼(地址)高字節(jié)
uchar        IR_UserL;                        //用戶碼(地址)低字節(jié)
uchar        IR_data;                        //數(shù)據(jù)原碼
uchar        IR_DataShit;                //數(shù)據(jù)反碼

bit                B_IrUserErr;                //User code error flag
bit                B_IR_Press;                        //Key press flag,include repeat key.
uchar        IR_code;                        //IR code        紅外鍵碼
unsigned int Count1ms=0;        //一毫秒計(jì)數(shù)器         
unsigned char   T1RH=0xFC;        //定時(shí)器時(shí)長(zhǎng)        高位值
unsigned char   T1RL=0x67;        //定時(shí)器時(shí)長(zhǎng)        低位值

/*************        本地函數(shù)聲明        **************/
void        Tx1Send(uchar dat);
uchar        HEX2ASCII(uchar dat);
void        InitTimer(void);
void        PrintString(unsigned char code *puts);
/*************  外部函數(shù)和變量聲明 *****************/
/* 配置并啟動(dòng)T0,ms-T0定時(shí)時(shí)間 */
void ConfigTimer1(unsigned int ms)
{
    unsigned long tmp;  //臨時(shí)變量                  
//    tmp = 11059200 / 12;      //定時(shí)器計(jì)數(shù)頻率
        tmp = MAIN_Fosc / 12;      //定時(shí)器計(jì)數(shù)頻率
    tmp = (tmp * ms) / 1000;  //計(jì)算所需的計(jì)數(shù)值
    tmp = 65536 - tmp;        //計(jì)算定時(shí)器重載值
    tmp = tmp + 18;           //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
    T1RH = (unsigned char)(tmp>>8);  //定時(shí)器重載值拆分為高低字節(jié)
    T1RL = (unsigned char)tmp;
    TMOD &= 0x0F;   //清零T0的控制位
    TMOD |= 0x10;   //配置T0為模式1
    TH1= T1RH;     //加載T0重載值
    TL1 = T1RL;
    ET1 = 1;        //使能T0中斷
    TR1 = 1;        //啟動(dòng)T0
}
/* T0中斷服務(wù)函數(shù) */
void InterruptTimer1() interrupt 3
{
        static unsigned char ti5=0;  
    TH1 = T1RH;  //重新加載重載值
    TL1 = T1RL;
        ti5++;          //計(jì)數(shù)標(biāo)志自加1
        if(ti5==20)          //判斷是否到1s
        {
                ti5=0;             //將靜態(tài)變量清0
        //        FlashLed=~FlashLed;       //LED位求反
        }
}
/********************* 主函數(shù) *************************/
void main(void)
{
        userLed1=0;
        userLed2=0;
        userLed3=0;
        userLed4=0;
        userLed5=0;
         
        EA=1;        //允許總中斷
        ConfigTimer1(25);        
        InitTimer();                //初始化Timer           
        while(1)
        {
                if(B_IR_Press)                //有IR鍵按下
                {
                        switch(IR_code)
                        {
                                case 0x01:
                                        userLed1=~userLed1;
                                        break;
                                case 0x00:
                                        userLed2=~userLed2;
                                        break;
                                case 0x04:
                                        userLed3=~userLed3;
                                        break;
                                case 0x08:
                                        userLed4=~userLed4;
                                        break;
                                case 0x43:
                                        userLed5=~userLed5;
                                        break;
                        }
                        B_IR_Press = 0;                //清除IR鍵按下標(biāo)志
                }
        }
}   


//*******************************************************************
//*********************** IR Remote Module **************************
//*********************** IR Remote Module **************************
//this programme is used for Receive IR Remote (HT6121).                        
//data format: Synchro,AddressH,AddressL,data,/data, (total 32 bit).
//send a frame(85ms), pause 23ms, send synchro of another frame, pause 94ms         
//data rate: 108ms/Frame   
//Synchro:low=9ms,high=4.5/2.25ms,low=0.5626ms
//Bit0:high=0.5626ms,low=0.5626ms
//Bit1:high=1.6879ms,low=0.5626ms
//frame space = 23 ms or 96 ms                  
/******************** 紅外采樣時(shí)間宏定義, 用戶不要隨意修改        *******************/  
#if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
        #define        D_IR_sample                        D_TIMER0                //定義采樣時(shí)間,在60us~250us之間
#endif        
#define D_IR_SYNC_MAX                (15000/D_IR_sample)        //SYNC max time
#define D_IR_SYNC_MIN                (9700 /D_IR_sample)        //SYNC min time
#define D_IR_SYNC_DIVIDE        (12375/D_IR_sample)        //decide data 0 or 1
#define D_IR_DATA_MAX                (3000 /D_IR_sample)        //data max time
#define D_IR_DATA_MIN                (600  /D_IR_sample)        //data min time
#define D_IR_DATA_DIVIDE        (1687 /D_IR_sample)        //decide data 0 or 1
#define D_IR_BIT_NUMBER                32                                        //bit number

//*******************************************************************************************
//**************************** IR RECEIVE MODULE ********************************************
void IR_RX_HT6121(void)
{
        uchar        SampleTime;         
        IR_SampleCnt++;                                                        //Sample + 1   
        F0 = P_IR_RX_temp;                                                //Save Last sample status
        P_IR_RX_temp = P_IR_RX;                                        //Read current status
        if(F0 && !P_IR_RX_temp)                                        //Last sample is high,and current sample is low, so is fall edge
        {
                SampleTime = IR_SampleCnt;                        //get the sample time
                IR_SampleCnt = 0;                                        //Clear the sample counter
                if(SampleTime > D_IR_SYNC_MAX)                B_IR_Sync = 0;        //large the Maxim SYNC time, then error
                else if(SampleTime >= D_IR_SYNC_MIN)                                        //SYNC
                {
                        if(SampleTime >= D_IR_SYNC_DIVIDE)
                        {
                                B_IR_Sync = 1;                                        //has received SYNC
                                IR_BitCnt = D_IR_BIT_NUMBER;        //Load bit number
                        }
                }
                else if(B_IR_Sync)                                                //has received SYNC
                {
                        if(SampleTime > D_IR_DATA_MAX)        
                                B_IR_Sync=0;        //data samlpe time to large
                        else
                        {
                                IR_DataShit >>= 1;                                        //data shift right 1 bit
                                if(SampleTime >= D_IR_DATA_DIVIDE)        IR_DataShit |= 0x80;        //devide data 0 or 1
                                if(--IR_BitCnt == 0)                                //bit number is over?
                                {
                                        B_IR_Sync = 0;                                        //Clear SYNC
                                        if(~IR_DataShit == IR_data)                //判斷數(shù)據(jù)正反碼
                                        {
                                                if((IR_UserH == (User_code / 256)) &&
                                                        IR_UserL == (User_code % 256))
                                                                B_IrUserErr = 0;        //User code is righe
                                                else        B_IrUserErr = 1;        //user code is wrong
                                                        
                                                IR_code      = IR_data;
                                                B_IR_Press   = 1;                        //數(shù)據(jù)有效
                                        }
                                }
                                else if((IR_BitCnt & 7)== 0)                //one byte receive
                                {
                                        IR_UserL = IR_UserH;                        //Save the User code high byte
                                        IR_UserH = IR_data;                                //Save the User code low byte
                                        IR_data  = IR_DataShit;                        //Save the IR data byte
                                }
                        }
                }
        }
}


/**************** Timer初始化函數(shù) ******************************/
void InitTimer(void)
{
        TMOD = 0;                //for STC15Fxxx系列        Timer0 as 16bit reload timer.
        TH0 = Timer0_Reload / 256;
        TL0 = Timer0_Reload % 256;
        ET0 = 1;
        TR0 = 1;
        EA  = 1;
}

/********************** Timer0中斷函數(shù)************************/
void timer0 (void) interrupt 1
{
        IR_RX_HT6121();
}  

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:235200 發(fā)表于 2020-1-29 02:01 | 只看該作者
初步看了一下是利用 D_IR_SYNC_MAX所定義的值的大小跳過(guò)了用戶碼的那段時(shí)間
回復(fù)

使用道具 舉報(bào)

板凳
ID:206067 發(fā)表于 2020-1-29 16:54 | 只看該作者
csmyldl 發(fā)表于 2020-1-29 02:01
初步看了一下是利用 D_IR_SYNC_MAX所定義的值的大小跳過(guò)了用戶碼的那段時(shí)間

//#define D_IR_SYNC_MAX                (12375/D_IR_sample)        //SYNC max time  //對(duì)錯(cuò)用戶碼都能遙控
//#define D_IR_SYNC_MAX                (12374/D_IR_sample)        //SYNC max time  //對(duì)錯(cuò)用戶碼都不行

改小不行 改大一直都是都能識(shí)別
回復(fù)

使用道具 舉報(bào)

地板
ID:235200 發(fā)表于 2020-1-29 18:57 | 只看該作者
仔細(xì)看了一下,是對(duì)用戶碼進(jìn)行了判斷,用戶碼正確才會(huì)有發(fā)光二極管指示
程序中這一句是對(duì)用戶碼進(jìn)行判斷
if((IR_UserH == (User_code / 256)) && IR_UserL == (User_code % 256))
          B_IrUserErr = 0;        //User code is righe
            else        B_IrUserErr = 1;        //user code is wrong
回復(fù)

使用道具 舉報(bào)

5#
ID:206067 發(fā)表于 2020-2-1 08:42 | 只看該作者
csmyldl 發(fā)表于 2020-1-29 18:57
仔細(xì)看了一下,是對(duì)用戶碼進(jìn)行了判斷,用戶碼正確才會(huì)有發(fā)光二極管指示
程序中這一句是對(duì)用戶碼進(jìn)行判斷
if ...

程序的用戶碼并不正確 他對(duì)數(shù)據(jù)碼的判斷也是準(zhǔn)確的  好像沒(méi)有實(shí)際檢測(cè)用戶碼
回復(fù)

使用道具 舉報(bào)

6#
ID:224003 發(fā)表于 2020-2-17 16:17 | 只看該作者

if((IR_UserH == (User_code / 256)) && IR_UserL == (User_code % 256))
          B_IrUserErr = 0;        //User code is righe
            else        B_IrUserErr = 1;        //user code is wrong

這是就是對(duì)用戶碼進(jìn)行判斷的, B_IrUserErr就是檢測(cè)結(jié)果。
但此代碼main中主while中,沒(méi)有利用這個(gè)判斷結(jié)果而已。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 天天综合久久 | 久久久这里只有17精品 | 一区二区三区四区在线播放 | 亚洲黄色av | 精品国产乱码久久久久久蜜臀 | 日韩在线视频播放 | 超碰激情 | 久久久婷 | 亚洲精选一区 | 久久性色 | 天天射天天干 | 久在线 | 中文字幕国产日韩 | 欧美一区二区三区在线观看 | 男人亚洲天堂 | 欧美午夜视频 | 免费的色网站 | 欧美三级视频在线观看 | 国外成人在线视频网站 | 久色 | 中文字幕精品一区 | 秋霞在线一区二区 | 成人精品鲁一区一区二区 | 亚洲黄色国产 | 欧美一级片在线播放 | 日韩一级黄色毛片 | 99亚洲精品 | 成人在线亚洲 | 黄色精品 | 国产高清在线精品 | 亚洲精品乱码久久久久久久久 | 一级日批片 | 阿v视频在线观看 | 色综合久久天天综合网 | 免费在线播放黄色 | 午夜精品久久久久久久久久久久久 | 超碰97干| 久久香蕉精品视频 | 国产精品免费观看视频 | 国产一区二区三区久久久久久久久 | av免费网站在线观看 |