• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

【求助】红外遥控程序,为什么我没办法输入

艾笠调查团 2016-10-23 浏览量:812
协议是NEC红外遥控协议,用户码是00 FF,这里我取了第三个数据码,第四个是反码就不管了图片是遥控器对应的数据码
想要通过遥控器来控制数码管实现加减算法
但是输入的数总是出错


1 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 艾笠调查团 发表于 2016-10-24 20:01
    嗯...確實如您所說,我又改了改,可是這程序有很大問題不能計算十位以上的算法,我這種算法不行,您有一 ...

    嗨,您另外發一篇詳細描述一下吧! 這題問答就先結囉,記得選最佳答案:)
    • 发布于 2016-10-23
    • 举报
    • 评论 0
    • 0
    • 0

其他答案 数量:4
  • 忘了发程序了,程序如下
    #include
    typedef unsigned int uint;
    typedef unsigned char uchar;
    sbit du=P2^6;
    sbit we=P2^7;
    uchar code table[]={

    0x3F, //"0"
    0x06, //"1"
    0x5B, //"2"
    0x4F, //"3"
    0x66, //"4"
    0x6D, //"5"
    0x7D, //"6"
    0x07, //"7"
    0x7F, //"8"
    0x6F, //"9"
    0x77, //"A"
    0x7C, //"B"
    0x39, //"C"
    0x5E, //"D"
    0x79, //"E"
    0x71, //"F"
    0x76, //"H"
    0x38, //"L"
    0x37, //"n"
    0x3E, //"u"
    0x73, //"P"
    0x5C, //"o"
    0x40, //"-"
    0x00, //熄灭
    0x01 //自定义
    };
    uchar IRtime,numer,numer1,n;
    uchar IRcord[4];
    uchar IRdata[33];
    bit IRpro_ok,IR_ok;

    void delay(uint z)
    {
    uint a;
    for(z=z;z>0;z--)
    for(a=110;a>0;a--);

    }
    void init()
    {
    TMOD=0x22;
    TH0=TL0=0;
    TH1=TH1=0xfd;
    IT0=1; //外部中断0下降沿触发方式
    EX0=1; //外部中断允许
    ET0=1;
    IT0=1; //定时器0中断打开
    SM0=0;
    SM1=1; //01模式,方式1——10位异步模式
    TR1=1;
    TR0=1;
    EA=1;
    }

    void IRcordpro()
    {
    uchar i,j,k,cord,value;
    P1=0xff;
    k=1;
    for(i=0;i<4;i++)
    {
    for(j=0;j<8;j++)
    {
    value=value>>1;
    cord=IRdata[k];
    if(cord>5)
    value=value|0x80;
    k++;
    }
    IRcord=value;
    value=0;
    }
    IRpro_ok=1;
    }

    void mix()
    {
    switch(IRcord[2])
    {
    case 0x16:numer=0;
    break;
    case 0x0c:numer=1;
    break;
    case 0x18:numer=2;
    break;
    case 0x5e:numer=3;
    break;
    case 0x08:numer=4;
    break;
    case 0x1c:numer=5;
    break;
    case 0x5a:numer=6;
    break;
    case 0x42:numer=7;
    break;
    case 0x52:numer=8;
    break;
    case 0x4a:numer=9;
    break;
    default:numer=numer;
    }
    }

    void display()
    {
    P0=0xff;
    we=1;
    P0=0xf7;
    we=0;
    du=1;
    P0=table[numer%10];
    du=0;
    delay(5);

    P0=0xff;
    we=1;
    P0=0xfb;
    we=0;
    du=1;
    P0=table[numer%100/10];
    du=0;
    delay(5);

    P0=0xff;
    we=1;
    P0=0xfd;
    we=0;
    du=1;
    P0=table[numer/100];
    du=0;
    delay(5);

    }

    void aas() //add and subtruct
    {
    uchar num,flag;
    if(IRcord[2]==0x45)
    {
    num=0;
    numer=0;
    numer1=0;
    n=0;
    }
    if(n==1)
    {
    numer=numer;
    num=numer;
    }
    if(n==2)
    {
    num=num*10;
    numer=numer+num;
    num=numer;
    }
    if(n==3)
    {
    num=num*10;
    numer=num+numer;
    }
    if(n>3)
    {
    n=3;
    numer=numer;
    }
    if((IRcord[2]==0x07)|(IRcord[2]==0x15)|(IRcord[2]==0x09))
    {
    if(IRcord[2]==0x07)
    {
    numer1=numer;
    numer=0;
    n=0;
    flag=1;
    }
    if(IRcord[2]==0x15)
    {
    numer1=numer;
    numer=0;
    n=0;
    flag=2;
    }
    if(IRcord[2]==0x09)
    {
    if(flag==1)
    {
    numer=numer1-numer;
    n=0;
    }
    if(flag==2)
    {
    numer=numer1+numer;
    n=0;
    }
    else
    {
    numer=numer;
    n=0;
    }
    }
    }

    }

    void main()
    {
    uchar i;
    init();
    while(1)
    {
    if(IR_ok)
    {
    IRcordpro();
    IR_ok=0;
    if(IRpro_ok)
    {
    mix();
    for(i=0;i<4;i++)
    {
    SBUF=IRcord;
    while(!TI);
    TI=0;
    }
    IRpro_ok=0;
    n++;
    aas();
    }
    }
    display();

    }
    }

    void int0() interrupt 0
    {
    static startflag;
    static i;
    if(startflag)
    {
    if((IRtime>=32)&&(IRtime<52)) //如果是引导码就将i置0,从头开始记录数值
    i=0;

    IRdata=IRtime;
    IRtime=0;
    i++;
    if(i==33)
    {
    i=0;
    IR_ok=1; //主函数进入解码部分的开关
    }


    }
    else
    {
    startflag=1;
    IRtime=0;
    }

    }

    void time0() interrupt 1
    {
    IRtime++;
    }

    • 发布于2016-10-23
    • 举报
    • 评论 0
    • 1
    • 0

  • mix()函数中的switch case 语句 最后的default 怎么没有break。。。
    • 发布于2016-10-23
    • 举报
    • 评论 0
    • 0
    • 0

  • 本帖最后由 MOP 于 2016-10-24 16:59 编辑

    這一樣是C語言的基本觀念,看您的ass()就知道問題了

    void aas()                   //add and subtruct
    {
            uchar num,flag;
            if(IRcord[2]==0x45)
            {
                    num=0;
                    numer=0;
                    numer1=0;
                    n=0;
            }
            if(n==1)
            {
                    numer=numer;
                    num=numer;
            }
            if(n==2)
            {
                    num=num*10;
                    numer=numer+num;
                    num=numer;               
            }                                                         
            if(n==3)
            {
                    num=num*10;
                    numer=num+numer;
            }
            if(n>3)
            {
                    n=3;
                    numer=numer;
            }
    但每次call ass()函式的時候,num變數總是被重新初始化,這是因為他是單純local variable,所以您的值加總會永遠是錯的,同理flag變數也是

    Sol:
    static uchar num,flag;
    宣告成static時,每次call ass()都不會再將num和flag初始化,會保留在memory中,即static local variable,當您主程式main()離開了才會釋放該memory

    希望有幫助到您:)
    • 发布于2016-10-24
    • 举报
    • 评论 0
    • 0
    • 0

  • MOP 发表于 2016-10-24 16:54
    這一樣是C語言的基本觀念,看您的ass()就知道問題了

    但每次call ass()函式的時候,num變數總是被重新初始化, ...

    嗯...確實如您所說,我又改了改,可是這程序有很大問題不能計算十位以上的算法,我這種算法不行,您有一些關於這些加減法的例子可以分享一下嗎?:)
    • 发布于2016-10-24
    • 举报
    • 评论 0
    • 0
    • 0

相关问题

问题达人换一批

【求助】红外遥控程序,为什么我没办法输入