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

📄 lyj-v1.0.c

📁 AVR下的红外解码程序
💻 C
字号:
//ICC-AVR application builder : 2005-4-14 21:26:27
// Target : 电动晾衣架 DC电机+红外
// Crystal: 8Mhz

/*红外解码的方法 NEC格式 upd6121
1、9Ms的高电平启动头,然后是4.5Ms的低电平,如果2.25Ms时就有高电平,是持续信号,不处理
2、然后以一个高电平和一个低电平为1Bit,高电平时间是0.5625Ms=562us,
   高低电平时间比为1:1时是Bit1,比为1:3时是Bit0
4、共读入4Byte共24bit,第12Byte是CustomCode码和其反码,第34Byte是DataCode和其反码
5、CustomCode正确和DataCode效验正确的话,执行

接收器输出是反相的
100us采样周期,9Ms=90,4.5Ms=45,高电平=5,低电平最多15
*/
#include <eeprom.h>
#include "config.h"
#include <string.h>

#define VERSION 10

#define CT1H 0x94;//CA01=10ms 最小应答间隔
#define CT1L 0x01;//BF34=12ms AF01=15ms 9401=20ms

#define T0N 10
#define LEDON CLR(PORTB,LED);
#define LEDOFF SET(PORTB,LED);

#define CSTOP 1  
#define CUP   2
#define CDOWN 3
#define CLAMP 4
#define CDIS  5

//红外命令按键码
#define IRSTOP 10  
#define IRUP   11
#define IRDOWN 7
#define IRLAMP 2
#define IRDIS  14

bit b20ms,bTimer,bCmdOk,bIrCmdOk;
uchar T0Cnt,iCmd;
uchar iPreBit,iHight,iLow,iIrMode;
uchar iBitCount,iIrData[4];
 
void DelayMs(char);
void DelayUs(uint);

void PortInit(void)
{
 PORTB = 0xFD;
 DDRB  = 0x02;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x7F;
 PORTD = 0x9F;
 DDRD  = 0x60;
  
}

//Watchdog initialize
// prescale: 2048K
void WdtInit(void)
{
 WDR(); 
 WDTCR=(1<<WDTOE)|(1<<WDE);//!!注意:M8的WDT修改要先WDTOE,WDE=1 P42 
 WDTCR=(1<<WDE)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0);//2.2s 
}

void Timer0Init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x64; //set count
 TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler Timer0:10
void Timer0(void)
{
 TCNT0 = 0x64; //reload counter value 20ms
 
 b20ms=1;
 if (--T0Cnt==0)
   {T0Cnt=T0N;
    bTimer=1;
    }
 
 CheckStop();

}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 100uSec
// actual value: 100.000uSec (0.0%)
void Timer1Init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0x9C;
 OCR1AH = 0x00;
 OCR1AL = 0x64;
 OCR1BH = 0x00;
 OCR1BL = 0x64;
 ICR1H  = 0x00;
 ICR1L  = 0x64;
 TCCR1A = 0x00;
 TCCR1B = 0x02; //start Timer
}

#pragma interrupt_handler Timer1:9
void Timer1(void)
{
 //TIMER1 has overflowed
 TCNT1H = 0xFF; //reload counter high value
 TCNT1L = 0x9C; //reload counter low value
 
 CheckIR();//红外解码
}

//call this routine to initialize all peripherals
void IoInit(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 PortInit();
 WdtInit();
 Timer0Init();
 Timer1Init();
 //ADCSR&=~BIT(ADEN);
 
 MCUCR = 0x00;//int0 low level在掉电模式,低电平触发才能唤醒,下跳沿不行
 GICR  = 0x00;//Int Mask,Int0 disable
 TIMSK = 0x05; //timer interrupt Mask: Timer0,1 enable
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}


void WdtOn()
{
  WDR();
  WDTCR=0x0f;

}

void WdtOff()
{
  CLI();//!!必须,WDTOE置位后必须4个时钟内清 WDE,才能真正关断WDT
  WDR();
  WDTCR |=(BIT(WDTOE)|(BIT(WDE)));
  //WDTCR&=~BIT(WDE);//0;//off
  WDTCR=0x07;
  SEI();
}

void PwrIdle()
{//P30 MCUCR 休眠控制和INT0,1,
  MCUCR&=0x0f;
  MCUCR |=BIT(SE);
  asm("sleep");
}

void PwrDown()
{ 
  MCUCR&=0x0f;
  MCUCR |=0x60;//0x60掉电 0x70省电
  asm("sleep");
}

//测试红外输出
//无信号时有小许干扰输出,遥控器保持按键时有持续输出
void TestIR()
{
if ((PIND&(1<<IR))!=0)
	     {BeepOff();}
      else
	     {BeepOn();}

}

void main(void)
{

   IoInit();
   SystemIni();
   Beep();
   while (1)
   {  
      if (bTimer) TimerFunc();
	  //TestIR();
	  if (bIrCmdOk) CheckIrCmd();
	  if (bCmdOk) ReadCmd();
	 
    };

}

void ReadCmd()
{
    bCmdOk=0;
	//Beep();
	switch (iCmd)
	{
	   case CSTOP:
	       MotorStop();
		   BeepN(2);
	       break;
	   case CUP:
	       MotorUp();
		   BeepN(1);
	       break;
	   case CDOWN:
	       MotorDown();
		   BeepN(1);
		   break;
	   case CLAMP:
	       if ((PORTC&(1<<LAMP))==0)
		     { LampOn();
			   BeepN(1);
			 }
		   else
		     { LampOff();
			   BeepN(2);
			 }
		   break;
	   case CDIS:
	       if ((PORTC&(1<<DIS))==0)
		     { DisOn();
			   BeepN(1);
			 }
		   else
		     { DisOff();
			   BeepN(2);
			 }
		   break;
	}
}

//红外解码
void CheckIR()
{uchar iNowBit,iPos;
   
   if ((PIND&(1<<IR))>0) iNowBit=0;else iNowBit=1;//先反相
   
   //把信号持续的处理作为公共处理
   if ((iPreBit==1)&&(iNowBit==1))//hight++
		  { iHight++;
		    return;
		  }
   if ((iPreBit==0)&&(iNowBit==0))//low++
		  { iLow++;
		    return;
		  }
		  
   switch (iIrMode)
   {
     case 0://等待9ms的高电平
	      if ((iPreBit==0)&&(iNowBit==1))//0->1:start
		  { iPreBit=1;
		    iHight=1;
			return;
		  }
		  
		  if ((iPreBit==1)&&(iNowBit==0))//1->0
		  { iPreBit=0;
		    if (iHight>85)//9.0ms
			  { iIrMode=1;
			    iLow=1;
				iHight=0;
			    //bCmdOk=1;iCmd=10;//Test..
			  }
			else
			   {iHight=0;//reset
			   }
			return;
		  }
	      break;
		  
	 case 1://等待4.5ms的低电平
	      if ((iPreBit==0)&&(iNowBit==1))//0->1
		  { iPreBit=1;
		    if (iLow>40)//4.5ms
			  {  iIrMode=2;
			     iBitCount=0;
				 iIrData[0]=0;iIrData[1]=0;iIrData[2]=0;iIrData[3]=0;
				 iHight=1;
				 //bCmdOk=1;iCmd=10;//Test..
			  }
			else
			  { iIrMode=0;
			    //确实只发送一次,后面的是按键保持信号,无按键码信息
			  }
		    return;
		  }
	      
	      break;
		  
	 case 2://接收用户码和按键码,共32Bit
	      
	      if ((iPreBit==1)&&(iNowBit==0))//1->0
		  { iPreBit=0;
		    iLow=1;
		  }
		  if ((iPreBit==0)&&(iNowBit==1))//0->1
		  { iPreBit=1;
		    //已经接收1Bit,根据占空比判断01
			if (iLow>(iHight+iHight)) iNowBit=0;else iNowBit=1;

			//Bit0在前,右移位
			iPos=iBitCount/8;
			iIrData[iPos]=iIrData[iPos]>>1;
			if (iNowBit==1) iIrData[iPos]|=0x80;
			
			iBitCount++;
			if (iBitCount>=32)
			{ bIrCmdOk=1;
			  iIrMode=0;
			  //bCmdOk=1;iCmd=10;//Test..
			}
			
			iHight=1;//must!
		  }
	 	  break;
     default:
	 	  iIrMode=0;
		  break;
   }
}

//对红外命令判断操作
void CheckIrCmd()
{uchar i;
   bIrCmdOk=0;
   if (iIrData[0]!=(~iIrData[1]))
   { return;//custom码就不对
   }
   if (iIrData[2]!=(~iIrData[3])) return;//按键码不对
   
   i=iIrData[2]&0x0f;
   //BeepN(i&0x0f);
   switch (i)
   { case IRSTOP:
        bCmdOk=1;iCmd=CSTOP;
        break;
	 case IRUP:
	    bCmdOk=1;iCmd=CUP;
		break;
	 case IRDOWN:
	    bCmdOk=1;iCmd=CDOWN;
		break;
	 case IRLAMP:
	    bCmdOk=1;iCmd=CLAMP;
		break;
	 case IRDIS:
	    bCmdOk=1;iCmd=CDIS;
	    break;
   }
}

void CheckStop()
{
   if (((PIND&(1<<UPX))==0) || ((PIND&(1<<DWNX))==0)) 
       { MotorStop();
	     BeepN(2);
		 }
	 
}

void TimerFunc()
{
   bTimer=0;
   CPL(PORTB,LED);
   //CPL(PORTC,BEEP);
   //CPL(PORTC,LAMP);
   //CPL(PORTD,DOWN);
   WDR();//clear WDT
}

void MotorUp()
{
   CLR(PORTD,UP);
   SET(PORTD,DOWN);
}

void MotorDown()
{
   SET(PORTD,UP);
   CLR(PORTD,DOWN);
}

void MotorStop()
{
   CLR(PORTD,UP);
   CLR(PORTD,DOWN);
}

void LampOn()
{
  SET(PORTC,LAMP);
}

void LampOff()
{
  CLR(PORTC,LAMP);
}

void LampOnOff()
{
   CPL(PORTC,LAMP);
}

void DisOn()
{
  SET(PORTC,DIS);
}

void DisOff()
{
  CLR(PORTC,DIS);
}

void DisOnOff()
{
  CPL(PORTC,DIS);
}

void BeepOn()
{
  SET(PORTC,BEEP);
}

void BeepOff()
{
   CLR(PORTC,BEEP);
}

void Beep()
{
   SET(PORTC,BEEP);
   DelayMs(100);
   CLR(PORTC,BEEP);
}

void BeepN(uchar n)
{uchar i;
   for (i=0;i<n;i++)
   { Beep();
     WDR();
     DelayMs(100);
   }
 
}

void ReadSN()
{  char Buf[8],i;
   EEPROMReadBytes( 0x20, Buf,8 );
}

void WriteSN()
{char Buf[8],i;

  EEPROMWriteBytes(0x20, Buf, 8);
   
}


void SystemIni(void)
{
  ReadSN();
  T0Cnt=T0N;
}


void DelayMs(char ms)
{char i;
for (i=0;i<ms;i++)
  {DelayUs(1000);
  }
  return;
}

void DelayUs(uint us)
{uint i;
  
  for (i=0;i<us;i++)
  {NOP();NOP();NOP();NOP();
  }
}

⌨️ 快捷键说明

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