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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2489|回復: 3
打印 上一主題 下一主題
收起左側

51做的紅外遙控發(fā)射代碼有問題,求助!

[復制鏈接]
跳轉到指定樓層
樓主
ID:93430 發(fā)表于 2015-10-24 14:51 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
大家好,
     從網上DOWN了程序,優(yōu)化了一下,主要想調整NEC編碼的占空比(不是38K載波的占空比),但是實際測試輸出LEAD波形時并不按設計的走(地址碼與數據碼都未發(fā)現有問題),如,695次調用TIMER0中斷,輸出延時是對的,但是900次調用中斷后就不對了,感覺哪里有東西打斷了進程?也嘗試在不同的地方加中斷、計數使能,但是總有問題。
     我用的是STC12C5202AD單片機,用KEIL 5編譯,已經將芯片導入到KEIL里,使用外部12M晶振。
     代碼如下:

------------------------自定義頭文件----------------------------
#ifndef _C51_ZY_H_
#define _C51_ZY_H_

void UartInit();
void delay_us(int i);
void IR_transfer(char iraddr1,char iraddr2,char p_irdat);
void timer0_int_13us(void);
void delay_100ms(int k) ;
#endif

---------------主程序-----------------------------------------
     #include <reg51.h>
#include <C51_ZY.h>
sbit P1_1=P1^1;     //  LED
char  iraddr1=0x44;    //the first byte of the hex address
char  iraddr2=0x9b;     //the second byte of the hex address
char  p_irdata=0x12;    //IR DATA
void main(void)  
{
  P1_1=0;
        AUXR |= 0x80;         //SET TIMER0 1T MODE
  P1M1 &= 0xF7;
  P1M0 |= 0x08;        //SET P1_3 PUSH-PULL MODE
        //UartInit();
       
        IR_transfer(iraddr1,iraddr2,p_irdata);

        //delay_us(10);       
       
}   


----------------------TIMER0初始化-----------------------------
#include <reg51.h>
#include <C51_ZY.h>                                           
void  timer0_int_13us(void)
{                    


        AUXR |= 0x80;                //定時器時鐘1T模式
        TMOD &= 0xF0;                //設置定時器模式
        TMOD |= 0x01;                //設置定時器模式
        ET0 = 1;
        TL0 = 0x64;                //設置定時初值
        TH0 = 0xFF;                //設置定時初值
        TF0 = 0;                //清除TF0標志
        TR0 = 1;                //定時器0開始計時

}

-----------------------延時子程序--------------------------
void delay_100ms(int k)
{     
  int i,j;   
  for(k;k>0;k--)
{  
  for(i=0;i<150;i++)   
     {
                        for(j=0;j<400;j++);
     }
        }                 
                 
}

--------------------發(fā)射程序---------------------------
#define uchar unsigned char     //just for convenience
#define uint  unsigned int
#define ulong unsigned long  
#include <reg51.h>             //the standard head file  
#include <C51_ZY.h>
static bit OP;                 //the turn on or off of the IR led
int count;     //delay count
int endcount;   //terminate count
unsigned char flag;     //the flag of IR emission
sbit P1_3=P1^3;      // IR pin

extern char  iraddr1;    //the first byte of the hex address
extern char  iraddr2;     //the second byte of the hex address
extern char  p_irdata;    //IR DATA
int test_mode;//0 mean constant period,1 mean constant duty cycle   
int logic_high;
int logic0_low;
int logic1_low;
int lead_high;
int lead_low;
void   SendIRdata();
void IR_transfer(char iraddr1,char iraddr2,char p_irdata);
//void   delay_us(int i);
//void timer0_int_13us(void);
void IR_transfer(char iraddr1,char iraddr2,char p_irdata)
{
                 
            count=0;   
                        flag=0;   
                        OP=0;   
                        P1_3=0;     
                  EA = 1;      // permit cpu interrupt           
          
            timer0_int_13us();
          
                        //TMOD = 0x11; //set timer0&1 to the HEX mode of 1   
                        //ET0 = 1;     //permit timer0 interrupt     
                        //TH0 = 0xFF;     
                        //TL0 = 0x7d;  //set timer0 have a interrup per 13us(38k/2)   
      //TR0 = 1;     //start count     
           
       
        test_mode=0;     //0 mean constant period,1 mean constant duty cycle
        logic_high=43;   //43=0%;34=-20%,52=+20%;21=-50%,65=+50%
        lead_high=903;   //625(-10%),556(-20%),486(-30%),417(-40%),347(-50%)
                         //695(0%)
                         //764(+10%),834(+20%),903(+30%),973(+40%),1000(+45%)
        if(test_mode==0)
        {
                lead_low=350;//1045 is standard,
                logic1_low=173-logic_high; //173 is standard,
                logic0_low=87-logic_high;        //87 is standard,
        }
        else
        {
                lead_low=lead_high/2+1;    //+1 為了補償中斷子程序中取反操作與賦值操作的時間差
                logic0_low=logic_high+1;
                logic1_low=logic_high*3+1;
        }
       
do{   
         //ET0=0;
         //TR0=1;
         //EA=1;
   SendIRdata();
         //TR0=0;
         //ET0=0;
         //EA=0;
         delay_100ms(5);
  }   
while(1);
}
void SendIRdata()
{  
       
        int i;   
        char irdata;
                       //---------------send the starting code of 9ms---------------------
        endcount=lead_high;  //690 is standard,
  flag=1;  
  count=0;   
  //ET0=1;TR0=1;
  do{}while(count<endcount);  
                      //---------------send the ending code of 4.5ms--------------------
        //ET0=0;TR0=0;
        endcount=lead_low;  //
  flag=0;  
  count=0;   
        //ET0=1;TR0=1;
  do{}while(count<endcount);      
  //ET0=0;TR0=0;
               //-----------------send the first 8 bit of the address----------------
        irdata=iraddr1;  
        for(i=0;i<8;i++)
        {                           
               //---------------first,send 0.56ms ir wave(38k),voltage high-----------
                //ET0=1;TR0=1;
                endcount=logic_high;   
                flag=1;  
    count=0;   
               
    do{}while(count<endcount);                           
                          //----------------stop send ir wave,voltage low-----------------   
          if(irdata-(irdata/2)*2)  //judge the last binary bit is 1 or 0
                {
                        endcount=logic1_low;          //1 mean wide low level  
                }  
                else   
                {
                        endcount=logic0_low;          //0 mean narrow low level
                }   
    flag=0;  
    count=0;  
    do{}while(count<endcount);  
    irdata=irdata>>1;
  }  
              //-------------send the last 8 bit of the HEX address-----------------  
    irdata=iraddr2;  
    for(i=0;i<8;i++)
    {   
      endcount=logic_high;  
      flag=1;  
      count=0;   
      do{}while(count<endcount);  
                  if(irdata-(irdata/2)*2)
                          {endcount=logic1_low;}  
      else   
        {endcount=logic0_low;}   
      flag=0;  
      count=0;   
      do{}while(count<endcount);  
      irdata=irdata>>1;
    }  
             //------------------send 8 bit data---------------------  
    irdata=p_irdata;  
    for(i=0;i<8;i++)
    {  
      endcount=logic_high;  
      flag=1;  
      count=0;   
      do{}while(count<endcount);   
      if(irdata-(irdata/2)*2)  
        {endcount=logic1_low;}  
      else   
        {endcount=logic0_low;}  
      flag=0;  
      count=0;   
      do{}while(count<endcount);  
      irdata=irdata>>1;
     }  
             //---------------------send the 8 bit complement data----------------------   
     irdata=~p_irdata;  
     for(i=0;i<8;i++)   
      {  
        endcount=logic_high;  
        flag=1;  
        count=0;  
        do{}while(count<endcount);   
        if(irdata-(irdata/2)*2)
          {endcount=logic1_low;}
        else   
          {endcount=logic0_low;}
        flag=0;
        count=0;  
        do{}while(count<endcount);   
        irdata=irdata>>1;  
      }  
     endcount=logic_high;
     flag=1;  
     count=0;   
     do{}while(count<endcount);
     flag=0;
                P1_3=0;
                 //ET0 = 0;       
}  



//***********************************************************************************                                          
         //timer0 execute interrupt
void timeint(void) interrupt 1  using 1
{   
        //ET0=0;
       
        TH0=0xFF;   
        TL0=0x7E;  
        TR0=0;
        count++;   
       
        if (flag==1)  
         {
                 OP=~OP;  
         }
        else   
         {OP = 0;}
          P1_3 = OP;
         //ET0=1;
         TR0=1;
}   


-------------------------------



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

使用道具 舉報

沙發(fā)
ID:93441 發(fā)表于 2015-10-24 16:35 | 只看該作者
賺積分走人
回復

使用道具 舉報

板凳
ID:79544 發(fā)表于 2015-10-25 13:48 | 只看該作者
弄個紅外發(fā)射程序不用這么復雜吧,論壇很多發(fā)射程序好好找找。
回復

使用道具 舉報

地板
ID:93430 發(fā)表于 2015-10-26 08:21 | 只看該作者
@騰飛的龍  主要是為了測試產品接收IR信號的兼容性,所以需要設置不同的占空比,看能支持到多少。。
回復

使用道具 舉報

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

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本精品视频在线 | 国产精品日韩在线 | 蜜桃传媒一区二区 | 97国产精品视频人人做人人爱 | 精品久久久网站 | 中国大陆高清aⅴ毛片 | 欧美成人免费在线视频 | 日本在线中文 | 天天操夜夜操 | 欧美a在线 | 亚洲视频在线一区 | 欧美xxxx在线 | www国产精品 | 欧美日韩国产一区二区三区 | 日韩中文字幕 | 黄色欧美 | 91av精品 | 亚洲精选一区二区 | 国产一区二区在线免费观看 | 欧美视频免费在线观看 | 黄色毛片在线看 | 亚洲一区中文 | 精品亚洲一区二区三区 | 美女国产精品 | 亚洲逼院 | 亚洲精品乱码 | 久久久www成人免费精品 | 亚洲成人久久久 | 欧美国产精品一区二区 | 综合久久久久久久 | 91视频a| 国产福利在线 | 国偷自产av一区二区三区 | 亭亭五月激情 | 91人人爽 | 日韩欧美国产精品一区 | 亚洲国产情侣 | 国产精品免费一区二区 | 久久久久免费 | 99久久免费精品 | 精品少妇一区二区三区在线播放 |