原创 基于单片机的红外接收C语言程序

2010-6-8 18:39 3240 5 5 分类: MCU/ 嵌入式

#include <Gendef.h>
#include <Val.h>
#include <stdio.h>
#include<intrins.h>


void ErrRst(void)
{
 EX0 = TRUE;
 ET0 = FALSE;
 TR0 = FALSE;
  
 RecTem = 0;
 RecBitCoun = 0;
 RecBytCoun = 0;
 
 RecStart = FALSE;
 Recend = FALSE;
 DatRecFlag = FALSE;
 Status = RECRET0;
}


void Ex0Int(void) interrupt 0 using 1
{


// P1 = ~Status;


 if(Status == RECRET0){
  
  EX0 = FALSE;
  ET0 = TRUE;
  
  Status = RECRET1;


  TR0 = FALSE;
  TH0 = TimRefTH[Status];
  TL0 = TimRefTL[Status];
  TR0 = TRUE;


 }else if (Status == RECRET4){
 
  Status++;
  RecTem = 0x00;
  RecBitCoun = 0x00;
  RecBytCoun = 0x00;
  RecStart = TRUE;
  Recend = FALSE;
  DatRecFlag = FALSE;


  TR0 = FALSE;
  TH0 = TimRefTH[Status];
  TL0 = TimRefTL[Status];
  TR0 = TRUE; 
  
 }else if(RecStart == TRUE){


  {
   INT16U RecReg;


   RecReg = ((TH0<<8) & 0xFF00) | (TL0 & 0xFF);
   RecTem >>= 1;
   if((RecReg > ZERLOWLMT) && (RecReg < ZERHGHLMT)){    
    RecTem &= 0x7F;
   }else if((RecReg > ONELOWLMT) && (RecReg < ONEHGHLMT)){ 
    RecTem |= 0x80;
   }else{
    ErrRst();


    TI = 0;
    SBUF = 'A';
    while(TI == 0);
    TI = 1;


    TI = 0;
    SBUF = (RecReg >> 8) & 0xFF;
    while(TI == 0);
    TI = 1;
    
    TI = 0;
    SBUF = RecReg & 0xFF;
    while(TI == 0);
    TI = 1;


   }
     
   if(++RecBitCoun >= 8){
    RecBitCoun = 0;
    DatRecBuff[RecBytCoun++] = RecTem;


   }
   if(RecBytCoun >= 4){


//    EX0 = TRUE;
    TR0 = FALSE;
    ET0 = FALSE;
   
    RecTem = 0;
    RecBitCoun = 0;
    RecBytCoun = 0;
    
    RecStart = FALSE;
    Recend = TRUE;
    DatRecFlag = TRUE;
    Status = RECRET0; 


//    P1 = 0x0F;


    {
     
     EX0 = FALSE;
     EA = FALSE;


     IrdaS = 1;
     while(IrdaS == 0);


     
     IrdaS = 1;
     while(IrdaS == 0);


     EX0 = TRUE;
     EA = TRUE;
     
    }
    
   }
  }


  if((Recend == FALSE)&&(Status == RECRET5)){
   TR0 = FALSE;
   TH0 = TimRefTH[Status];
   TL0 = TimRefTL[Status];
   TR0 = TRUE;
  }


 }else{
 
  ErrRst();
 
 }
}


void Tim0Int(void) interrupt 1 using 1
{
 Status++;


// P1 = ~Status;


 if(1 == Status)
 {
//  P1 = ~0xAF;
  while(1);
 }


 if(RecStart == TRUE)
 {
 
  ErrRst();
  
 }else{


  if((Status == RECRET2)||(Status == RECRET3)){
   
   if(IrdaS == 1){


    ErrRst();


   }
  }


  if(Status == RECRET4){


   if(IrdaS == 0){
    ErrRst();


   }else{
    ET0 = TRUE;
    EX0 = TRUE;
   }
  }


  if(Status != RECRET0)
  {
   TR0 = FALSE;
   TH0 = TimRefTH[Status];
   TL0 = TimRefTL[Status];
   TR0 = TRUE;
  }


 }
}


#include <Gendef.h>
#include <Val.h>


void SerCon(void)
{
 SCON   = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr       */
 TMOD  |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload         */
 TH1     = 0xFD;       /* TH1:  reload value for 9600 baud @ 11.0592MHz    */
 TR1     = 1;            /* TR1:  timer 1 run                             */
 TI     = 1;         /* TI:   set TI to send first char of UART      */
}


void TmCon(void)
{
 TMOD |= 0x01;
 TH0 = 0x00;
 TL0 = 0x00;
 ET0 = 1;
// TR0 = 1;
}


void ExCom(void)
{
 IT0 = 1;
 EX0 = 1;
}


void SysInit()
{
 INT8U i;
 
 EA = 0;


 SerCon();
 TmCon();
 ExCom();


 RecTem = 0;
 RecBitCoun = 0;
 RecBytCoun = 0;
 
 RecStart = FALSE;
 Recend = FALSE;
 DatRecFlag = FALSE;
 Status = RECRET0;


 for(i = 0; i < 4; i++)
  DatRecBuff = 0;


 EA = 1;
}


原文地址:




http://gudeng614.blog.163.com/blog/static/818017420101545820700/

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /2 下一条