chenyl 发表于 2023-8-1 17:24:37

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

本帖最后由 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口

u8IR_SampleCnt;       //采样计数
u8IR_BitCnt;          //编码位数
u8IR_UserH;         //用户码(地址)高字节
u8IR_UserL;         //用户码(地址)低字节
u8IR_data;            //数据原码
u8IR_DataShit;      //数据移位

bit P_IR_RX_temp;       //Last sample
bit B_IR_Sync;          //已收到同步标志
bit B_IR_Press;         //红外接收标志
u8IR_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)
{
    u8SampleTime;
    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那;

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

神农鼎 发表于 2023-8-3 07:40:15



【新提醒】红外遥控发送/接收的演示程序,STC8H/STC32实验箱演示程序有 - LCD12864/LCD1602/红外遥控 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)

页: [1]
查看完整版本: STC8H红外遥控接收解码(NEC码)例程跑不通