找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

大神幫忙改一下C51單片機(jī)編碼收發(fā)程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:440585 發(fā)表于 2019-12-13 10:30 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式

求助高手幫忙改一下程序  本人太菜了  改不出來 (功能要求:當(dāng)K1按下,單片機(jī)U1的程序通過編碼發(fā)送到U2,當(dāng)U2接收到編碼 LED亮,K1斷開LED滅。)現(xiàn)在是K1按下LED亮  松開K1按鍵LED不滅燈 。下面程序是齊全的不用下載可以的。 紅外發(fā)射接收.rar (39.21 KB, 下載次數(shù): 6)

單片機(jī)源程序如下:

/*紅外發(fā)射編碼程序*/
#include<reg52.h>
sbit ir=P1^3;
sbit k1=P2^1;

unsigned int count, set_count;  
bit irflag,keyflag;   
unsigned char irsys[]={0x00,0xff};
unsigned char irdata,ircode;
void delay(unsigned int a)
{
        unsigned char i;
        while(--a!=0)
        for(i=300;i>0;i--);
}
void keyscan()
{
        if(k1==0)
        {
                delay(10);
                if(k1==0)
                {
                        keyflag=1;
                         irdata=0x01;
                        
                 }
        }
        }



void ir_sendbyte()    //紅外發(fā)送一個字節(jié)數(shù)據(jù)
{
        unsigned char i;
        for(i=0;i<8;i++)    //發(fā)送8位數(shù)據(jù)
        {
           set_count=43; //發(fā)送編碼中的0.56ms高電平
           irflag=1;      
           count=0;     
           TR0=1;     
           while(count<set_count);
           TR0=0;
           if(ircode&0x01) set_count=130; //判斷紅外編碼最低位,若為1則1.69ms的低電平
           else set_count=43;    //為0則0.565ms的低電平
           irflag=0;
           count=0;
           TR0=1;
           while(count<set_count);
           TR0=0;
           ircode=ircode>>1;  
        }
}
void ir_send()
{
        set_count=346; //發(fā)送編碼中的引導(dǎo)碼(4.5ms高電平+4.5ms低電平)
        irflag=1;   
        count=0;   
        TR0=1;
        while(count<set_count);
        TR0=0;
        set_count=346; //發(fā)送編碼中的4.5ms低電平
        irflag=0;
        count=0;
        TR0=1;
        while(count<set_count);
        TR0=0;
        ircode=irsys[0];  
        ir_sendbyte();
        ircode=irsys[1];
        ir_sendbyte();
        ircode=irdata;   //發(fā)送8位數(shù)據(jù)碼
        ir_sendbyte();
        ircode=~irdata; //發(fā)送8位數(shù)據(jù)反碼
        ir_sendbyte();
        set_count=43; //發(fā)送編碼中的0.56ms高電平
        irflag=1;
        count=0;
        TR0=1;
        while(count<set_count);
        TR0=0;
        irflag=0;
        delay(23);    //延時23ms(編碼中的23ms低電平)
        set_count=346; //發(fā)送編碼中的引導(dǎo)碼(4.5ms高電平+4.5ms低電平)
        irflag=1;
        count=0;
        TR0=1;
        while(count<set_count);
        TR0=0;
        set_count=346;
        irflag=0;
        count=0;
        TR0=1;
        while(count<set_count);
        TR0=0;
        set_count=43;   
        irflag=1;
        count=0;
        TR0=1;
        while(count<set_count);
        TR0=0;
        irflag=0;
        delay(23);  
}

void timer0_init()   
{
        EA=1;
        TMOD=0x02;     //定時0 8位自動重裝模式
        ET0=1;
        TH0=0xe6;   //定時13us,38K紅外矩形波,晶振24M
        TL0=0xe6;   
}
void main()
{
        timer0_init();
        count=0;     
        ir=0;
        irflag=0;     
        while(1)
        {
                keyscan();   
           if(keyflag)  
           {
                    delay(10);
                    ir_send();   
                    delay(500);
                    keyflag=0;   
                        delay(100);
                   }
        }
}

void timer0() interrupt 1  
{
        count++;
        if(irflag==1)
         ir=~ir; //有發(fā)射標(biāo)志,則發(fā)射38khz的矩形波
        else
         ir=0;     
}

/*紅外編碼接收程序*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
uchar irtime;
uchar startflag;
uchar irdata[33];
uchar bitnum;
uchar irreceok;
uchar ircode[4];
uchar irprosok;
void display();
void display1();
void timer0init()
{
  TMOD=0x02;
  TH0=0x00;
  TL0=0x00;
  ET0=1;
  EA=1;
  TR0=1;

}

void int0init()
{
  IT0=1;
  EX0=1;
  EA=1;
}

void irpros()   
{
  uchar k,i,j;
  uchar value;
  k=1;
  for(j=0;j<4;j++)
  {
   for(i=0;i<8;i++)
   {
    value=value>>1;  
    if(irdata[k]>6)   
    {
     value=value | 0x80;
    }
    k++;
        if(k>33)
        k=1;
   }
   ircode[j]=value;
  }
}

void main()
{
led1=0;
  timer0init();
  int0init();
  while(1)
  {
   if(irreceok)
   {
    irpros();
   }
   display();
  }



}
void display()
{                                                                                          
                switch(ircode[2])
                        {
                                        case 0x02:led1=1;
                                        break;

                     }

        
        
}

void timer0 () interrupt 1
{
  irtime++;                                                                    
}

void int0 () interrupt 0
{
  if(startflag)
  {
   if(irtime>32) //檢測引導(dǎo)碼
   {
     bitnum=0;
   }
   irdata[bitnum]=irtime;
   irtime=0;
   bitnum++;
   if(bitnum==33)
   {
    bitnum=0;
    irreceok=1;
        startflag=0;

  }

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

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表