原创 avr单片机 红外遥控解码实验 实验(七)

2008-5-12 12:53 5486 9 9 分类: MCU/ 嵌入式

点击下载


这个实验了解红外解码,avr外部中断(捕获)的使用.详情见附件


平台:FDRAVR开发板


主程序代码:


#include <mega16.h>
#include"led.h"
#include"delay.h"
#define FRE 2
// Timer 1 overflow interrupt service routine 



unsigned char keycode="0";
unsigned int key_temp=0;
unsigned char key_flag=0;   //解码成功
bit nosignal="0";     //无信号标志位
bit key_start=0;
bit dat_start=0;
bit key_end=0;
unsigned char counter="0";



interrupt [TIM1_OVF] void timer1_ovf_isr(void)

  if(!nosignal)
  {
   key_start=0;
   dat_start=0;
   counter="0";
   key_temp=0;
   key_end=0;
   TCNT1H=0x00;
   TCNT1L=0x00;
   nosignal="1";
   }
}


// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{


         unsigned char joint="0",A,B;
          A= TCNT1L ;
          B="TCNT1H"; 
   if(key_start)  //开始位
   { 
                         nosignal="0";
    if(!dat_start)
    {
     if(TCNT1H)
     {
     counter++;
     
     }
      else
     {
     joint=1;
     
     }
     if(counter==26) //越过前导码
     {
     dat_start=1;
     counter=0;
  
     }        //数据开始 
       }
  else
  {
            //数据开始接收
        
   if(TCNT1H&&TCNT1H<24)
   { 
       key_temp>>=1;
    if(TCNT1H>2)
    key_temp+=32768;
    counter++;
    if(counter==16)
    {
    keycode=key_temp&0xff;
    key_temp&=0xff00;
    key_temp>>=8;
    key_temp=~(unsigned char)(key_temp);
    if(key_temp==keycode)key_flag=1; 
    key_temp=0; 
                                  //检测 到按键
   
    }
   }
   else
   {
   
    joint="1";
   }
  
  }      
   }
   else
   {
   if(TCNT1H>10&&TCNT1H<31)
   {
    if(!key_end)
  {
                key_start=1;     //清数据开始标志
     counter=0;
     dat_start=0;    //请开始标志          
     key_temp=0;   
  }
  else
  {
   key_start=0;
   dat_start=0;
   key_temp=0;
   counter="0";
   key_end=0;
  
  }
 
    }
  
  }
      
  if(!joint)
  {
   TCNT1H=0;
   TCNT1L=0;


   } 
 
  /*if(B>1)
   {  
 counter++;
        
     
   }*/
   TCNT1H=0;
   TCNT1L=0;


}


// Declare your global variables here


void main(void)
{
// Declare your local variables here
unsigned char temp="0";
 


// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500.000 kHz
// Mode: Normal top="FFFFh"
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: On
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x82;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
    


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x24;


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;


// Global enable interrupts
#asm("sei")


while (1)
      {
      if(key_flag){key_flag=0;temp=keycode;}   
      showcounter(temp );


      };
}

PARTNER CONTENT

文章评论0条评论)

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