找回密码
 立即注册
查看: 671|回复: 1

STC8H红外遥控接收解码(NEC码)例程跑不通

[复制链接]

该用户从未签到

6

主题

16

回帖

148

积分

注册会员

积分
148
发表于 2023-8-1 17:24:37 | 显示全部楼层 |阅读模式
本帖最后由 chenyl 于 2023-8-1 17:35 编辑

以下是根据我自己遥控器IR_in P06口改过了的代码,但是无法跑到获取到值的那一步

/*************  本程序功能说明  **************

本例程基于STC8H8K64U为主控芯片的实验箱9进行编写测试,STC8G、STC8H系列芯片可通用参考.

红外接收程序。适用于市场上用量最大的NEC编码。

应用层查询 B_IR_Press标志为,则已接收到一个键码放在IR_code中, 处理完键码后, 用户程序清除B_IR_Press标志.

数码管左起4位显示用户码, 最右边两位显示数据, 均为十六进制.

下载时, 选择时钟 24MHz (用户可自行修改频率).

******************************************/
#include    "intrins.h"
#include    "IR_Remote_Rx.h"

#define SysTick     10000       // 次/秒, 系统滴答频率, 在4000~16000之间

/***********************************************************/

#define DIS_DOT     0x20
#define DIS_BLACK   0x10
#define DIS_        0x11

/****************************** 自动定义宏 ***********************************/
#define MAIN_Fosc                24000000L        //定义主时钟

#define Timer0_Reload   (65536UL - ((MAIN_Fosc + SysTick/2) / SysTick))     //Timer 0 中断频率

/*****************************************************************************/


/*************  红外接收程序变量声明    **************/
sbit    P_IR_RX = P0^6;         //定义红外接收输入IO口

u8  IR_SampleCnt;       //采样计数
u8  IR_BitCnt;          //编码位数
u8  IR_UserH;           //用户码(地址)高字节
u8  IR_UserL;           //用户码(地址)低字节
u8  IR_data;            //数据原码
u8  IR_DataShit;        //数据移位

bit P_IR_RX_temp;       //Last sample
bit B_IR_Sync;          //已收到同步标志
bit B_IR_Press;         //红外接收标志
u8  IR_code;            //红外键码
u16 UserCode;           //用户码


//*******************************************************************
//*********************** IR Remote Module **************************
//*********************** By  (Coody) 2002-8-24 *********************
//*********************** IR Remote Module **************************
//this programme is used for Receive IR Remote (NEC Code).

//data format: Synchro, AddressH, AddressL, data, /data, (total 32 bit).

//send a frame(85ms), pause 23ms, send synchro of continue 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 rate = 108ms ( pause 23 ms or 96 ms)

/******************** 红外采样时间宏定义, 用户不要随意修改  *******************/

#define IR_SAMPLE_TIME      (1000000UL/SysTick)     //查询时间间隔, us, 红外接收要求在60us~250us之间
#if ((IR_SAMPLE_TIME <= 250) && (IR_SAMPLE_TIME >= 60))
    #define D_IR_sample         IR_SAMPLE_TIME      //定义采样时间,在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_NEC(void)
{
    u8  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)                 //Pre-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){
                        SBUF = 0x22;//自己加的打印信息
                        B_IR_Sync = 0;  //large the Maxim SYNC time, then error
                }
        else if(SampleTime >= D_IR_SYNC_MIN)                    //SYNC
        {
                        SBUF = 0x33;//自己加的打印信息
            if(SampleTime >= D_IR_SYNC_DIVIDE)
            {
                                SBUF = 0x44;//自己加的打印信息
                B_IR_Sync = 1;                  //has received SYNC
                IR_BitCnt = D_IR_BIT_NUMBER;    //Load bit number
            }
        }
        else if(B_IR_Sync)                      //has received SYNC
        {
                        SBUF = 0x55;//自己加的打印信息
            if(SampleTime > D_IR_DATA_MAX){   
                                SBUF = 0x66;                //自己加的打印信息
                                B_IR_Sync=0;    //data samlpe time too large
                        }
            else
            {
                                SBUF = 0x77;//自己加的打印信息
                IR_DataShit >>= 1;                  //data shift right 1 bit
                if(SampleTime >= D_IR_DATA_DIVIDE){  
                                        SBUF = 0x88;//自己加的打印信息
                                        IR_DataShit |= 0x80;    //devide data 0 or 1
                                }
                if(--IR_BitCnt == 0)                //bit number is over?
                {
                                        SBUF = 0x99;//自己加的打印信息
                    B_IR_Sync = 0;                  //Clear SYNC
                    if(~IR_DataShit == IR_data)     //判断数据正反码
                    {
                                                SBUF = 0xaa;//自己加的打印信息
                        UserCode = ((u16)IR_UserH << 8) + IR_UserL;
                        IR_code      = IR_data;
                        B_IR_Press   = 1;           //数据有效
                    }
                }
                else if((IR_BitCnt & 7)== 0)        //one byte receive
                {
                                        SBUF = 0xbb;//自己加的打印信息
                    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
                }
            }
        }
    }
}

我把IR_RX_NEC();放在Timer0中断中跑
/********************** Timer0中断函数 ************************/
void timer0 (void) interrupt 1
{
    IR_RX_NEC();
}

Timer0初始化大概是如下:

    AUXR = 0x80;    //Timer0 set as 1T, 16 bits timer auto-reload,
    TH0 = (u8)(Timer0_Reload / 256);
    TL0 = (u8)(Timer0_Reload % 256);
    ET0 = 1;    //Timer0 interrupt enable
    TR0 = 1;    //Tiner0 run

当遥控器按下时能够检测到电压变低,
大部分都是在我加的打印SBUF = 0x22;或者SBUF = 0x33那;

走不到下一步,麻烦大家帮看一下是什么原因

回复 送花

使用道具 举报

该用户从未签到

552

主题

9492

回帖

1万

积分

管理员

积分
14057
发表于 2023-8-3 07:40:15 | 显示全部楼层
回复 支持 1 反对 0 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2024-5-18 05:09 , Processed in 0.061753 second(s), 36 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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