⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irremote.c

📁 avr attiny2313红外线接受程序
💻 C
字号:
/**************************************************************************
实验:红外线接受程序
单片机:ATTINY2313
遥控器:CHUNGHOP,设置000,
有效按键:UP
PB0: 输出 5秒交替信号
PB3: 输出 随按键高低电平交替
PB2: 输入 红外线信号输入
2008年8月20日
***************************************************************************/


#include <io2313v.h>//和单片机类型相对应的头文件,选择Attiny2313做实验;
#include <macros.h>
#include "chunghop.h"//遥控器编码及参数


#define TIMER0_OVF 7

unsigned char vtg=0;//瞬时电平
unsigned int timer0=0;//计数器 32us
unsigned int ms=0;//ms数
unsigned int sec=0;
//unsigned int hn[40];
//unsigned int ln[40];
unsigned int nn=0;
void main(void)
{//L1
  //OSCCAL=0XCA;//系统时钟校准 
  TCCR0=BIT(CS00);
  TIMSK=BIT(TOIE0);//允许定时器T/C0溢出中断
  SEI();//开中断
  DDRB|=BIT(0);//设置PB3为输出口
  PORTB&=~BIT(0);
  DDRB|=BIT(3);//设置PB3为输出口
  PORTB&=~BIT(3);
  DDRB&=~BIT(2);//设置PB2为输入口
  
  while(1)
  {//L2
    
   
  }//L2
}//L1
//------------------------------------------------------------------------
#pragma interrupt_handler timer0_ovf:TIMER0_OVF//指定中断服务程序的入口
void timer0_ovf(void)
{//L1
  unsigned int temp=0;//缓存变量
  unsigned char vtg=1;//现时刻电平 
  static unsigned char vtgold=1;//前时刻电平
  static unsigned char vtgchange=0;//电平变换标记
  static unsigned char pdepressed=1;//状态标记
  static unsigned char depressed=0;//按下标记
  static unsigned char codein=0;//输入标记
  static unsigned int codenum=1;//识别选位码
  static unsigned int datanum=1;//数据选位码
  static unsigned int customcode=0;//识别码
  static unsigned int datacode=0;//数据码
  static unsigned int data=0;//编码
  static unsigned int high=0;//高电平时间
  static unsigned int low=0;//低电平时间
  static unsigned int timer0=0;//计数器
  unsigned int timecost=0;
  //TCNT0=245;
  vtg=(PINB&0B00000100);//记录PD2电压
  vtg=vtg>>2;
  if(vtg==0 && vtgold==1)  //电压1->0 时,添加标记
  {//L2
    vtgchange=1;
  }//L2
  else
  {//L2
    vtgchange=0;
  }//L2
  vtgold=vtg;
  if(vtg==1)//记录电压持续时间,单位32us
  {//L2
    high++;
    if(high>=5000)  //100ms
    {//L3
	  //low=0;
	  high=5000;
	  codein=0;
	  pdepressed=1;
	  depressed=0;
	  customcode=0;
	  datacode=0;
	  data=0;
	  nn=0;
	  
	}//L3
  }//L2
  if(vtg==0)
  {//L2
    low++;
  }//L2
  if(vtgchange==1) //电压1->0时
  {//L2            //解码

	if(low>=toplow-code32usdiv && low<=toplow+code32usdiv && high >=tophigh-code32usdiv && high <=tophigh+code32usdiv)
	{//L3           //头标记
	  codein=1;
	  customcode=0;
	  datacode=0;
	  data=0;
	  depressed=0;
	  nn=1;
	}//L3    
    if(low>=endlow-code32usdiv && low<=endlow+code32usdiv && high >=endhigh-code32usdiv && high <=endhigh+code32usdiv)
	{//L3           //尾标记
      nn=34;
	}//L3    
    if(low>=bit0low-code32usdiv && low<=bit0low+code32usdiv && high >=bit0high-code32usdiv && high <=bit0high+code32usdiv)
	{//L3           //0
//	  depressed=0;
	  if(codein==1)
	  {//L4
	    if(nn>=2 && nn<=17)
		{//L5
		    codenum=0x0001<<(17-nn);
		    customcode=customcode & ~codenum;
		  if(nn==17)
		  {//L6
		    codenum=1;
		  }//L6
		}//L5 
	    if(nn>=18 && nn<=33)
		{//L5
		    datanum=0x0001<<(33-nn);
			datacode=datacode & ~datanum;
		  if(nn==33)
		  {//L6
			temp=datacode>>8;
			if((temp^datacode & 0x00fe) == 0x00fe)
			{//L7
			  data=temp & 0x00FF;
			}//L7
     	    codein=0;
	        customcode=0;
	        datacode=0;
		    depressed=1;
		    nn=0;
		  }//L6
		}//L5  
		 
	  }//L4
	}//L3    
    if(low>=bit1low-code32usdiv && low<=bit1low+code32usdiv && high >=bit1high-code32usdiv && high <=bit1high+code32usdiv)
	{//L3           //1
	  if(codein==1)
	  {//L4
	    if(nn>=2 && nn<=17)
		{//L5
		    codenum=0x0001<<(17-nn);
			customcode=customcode | codenum;
		  if(nn==17)
		  {//L6
		    codenum=1;
		  }//L6
		}//L5  
	    if(nn>=18 && nn<=33)
		{//L5
		    datanum=0x0001<<(33-nn);
			datacode=datacode | datanum;
		  if(nn==33)
		  {//L6
			temp=datacode>>8;
			if((temp^datacode & 0x00fe) == 0x00fe)
			{//L7
			  data=temp & 0x00FF;
			}//L7
		    codein=0;
	        customcode=0;
	        datacode=0;
		    depressed=1;
		    nn=0;
	    
		  }//L6
		}//L5  
	  }//L4
	}//L3   

//    hn[nn]=high;
//	ln[nn]=low;
	if(nn>=40)
	{//L3
	nn=35;
	}//L3
	nn++;
	
	high=0;
	low=0;   
	

  }//L2
  
  
  if(data==UP && depressed==1 & pdepressed==1)
  {//L2
	PINB=PINB|BIT(3);
	pdepressed=0;
  }//L2
  if(timer0>=32)//主计数器
  
  {//L2
    ms++;
	timer0=0;
  }//L2
  if(ms>=1024)
  {//L2
    sec++;
    ms=0;
  }//L2
  if(sec>=5)
  {//L2
    sec=0;
    PINB=PINB|BIT(0);
  }//L2

  timer0++;
}//L1

//--------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -