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

📄 lyj.s

📁 /*红外解码的方法 NEC格式 upd6121 1、9Ms的高电平启动头
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module LYJ.c
	.area text(rom, con, rel)
	.dbfile D:\IccAvr\Pei\LYJ\LYJ.c
	.dbfunc e PortInit _PortInit fV
	.even
_PortInit::
	.dbline -1
	.dbline 49
; //ICC-AVR application builder : 2006-2-26 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
; 3、Timer1中断执行100us采样周期,9Ms=90,4.5Ms=45,高电平=5,低电平最多15
; 4、共读入4Byte共24bit,第1、2Byte是CustomCode码和其反码,第3、4Byte是DataCode和其反码
; 5、CustomCode正确和DataCode效验正确的话,执行
; 6、红外接收器输出是反相的
; */
; #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)
; {
	.dbline 50
;  PORTB = 0xFD;
	ldi R24,253
	out 0x18,R24
	.dbline 51
;  DDRB  = 0x02;
	ldi R24,2
	out 0x17,R24
	.dbline 52
;  PORTC = 0x00; //m103 output only
	clr R2
	out 0x15,R2
	.dbline 53
;  DDRC  = 0x7F;
	ldi R24,127
	out 0x14,R24
	.dbline 54
;  PORTD = 0x9F;
	ldi R24,159
	out 0x12,R24
	.dbline 55
;  DDRD  = 0x60;
	ldi R24,96
	out 0x11,R24
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e WdtInit _WdtInit fV
	.even
_WdtInit::
	.dbline -1
	.dbline 62
;   
; }
; 
; //Watchdog initialize
; // prescale: 2048K
; void WdtInit(void)
; {
	.dbline 63
;  WDR(); 
	wdr
	.dbline 64
;  WDTCR=(1<<WDTOE)|(1<<WDE);//!!注意:M8的WDT修改要先WDTOE,WDE=1 P42 
	ldi R24,24
	out 0x21,R24
	.dbline 65
;  WDTCR=(1<<WDE)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0);//2.2s 
	ldi R24,15
	out 0x21,R24
	.dbline -2
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e Timer0Init _Timer0Init fV
	.even
_Timer0Init::
	.dbline -1
	.dbline 69
; }
; 
; void Timer0Init(void)
; {
	.dbline 70
;  TCCR0 = 0x00; //stop
	clr R2
	out 0x33,R2
	.dbline 71
;  TCNT0 = 0x64; //set count
	ldi R24,100
	out 0x32,R24
	.dbline 72
;  TCCR0 = 0x05; //start timer
	ldi R24,5
	out 0x33,R24
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.area vector(rom, abs)
	.org 18
	rjmp _Timer0
	.area text(rom, con, rel)
	.dbfile D:\IccAvr\Pei\LYJ\LYJ.c
	.dbfunc e Timer0 _Timer0 fV
	.even
_Timer0::
	rcall push_lset
	.dbline -1
	.dbline 77
; }
; 
; #pragma interrupt_handler Timer0:10
; void Timer0(void)
; {
	.dbline 78
;  TCNT0 = 0x64; //reload counter value 20ms
	ldi R24,100
	out 0x32,R24
	.dbline 80
;  
;  b20ms=1;
	ldi R24,1
	sts _b20ms,R24
	.dbline 81
;  if (--T0Cnt==0)
	lds R24,_T0Cnt
	subi R24,1
	mov R2,R24
	sts _T0Cnt,R2
	tst R24
	brne L5
	.dbline 82
;    {T0Cnt=T0N;
	.dbline 82
	ldi R24,10
	sts _T0Cnt,R24
	.dbline 83
;     bTimer=1;
	ldi R24,1
	sts _bTimer,R24
	.dbline 84
;     }
L5:
	.dbline 86
;  
;  CheckStop();//判断限位开关
	rcall _CheckStop
	.dbline -2
L4:
	rcall pop_lset
	.dbline 0 ; func end
	reti
	.dbend
	.dbfunc e Timer1Init _Timer1Init fV
	.even
_Timer1Init::
	.dbline -1
	.dbline 94
; 
; }
; //TIMER1 initialize - prescale:8
; // WGM: 0) Normal, TOP=0xFFFF
; // desired value: 100uSec
; // actual value: 100.000uSec (0.0%)
; void Timer1Init(void)
; {
	.dbline 95
;  TCCR1B = 0x00; //stop
	clr R2
	out 0x2e,R2
	.dbline 96
;  TCNT1H = 0xFF; //setup
	ldi R24,255
	out 0x2d,R24
	.dbline 97
;  TCNT1L = 0x9C;
	ldi R24,156
	out 0x2c,R24
	.dbline 98
;  OCR1AH = 0x00;
	out 0x2b,R2
	.dbline 99
;  OCR1AL = 0x64;
	ldi R24,100
	out 0x2a,R24
	.dbline 100
;  OCR1BH = 0x00;
	out 0x29,R2
	.dbline 101
;  OCR1BL = 0x64;
	out 0x28,R24
	.dbline 102
;  ICR1H  = 0x00;
	out 0x27,R2
	.dbline 103
;  ICR1L  = 0x64;
	out 0x26,R24
	.dbline 104
;  TCCR1A = 0x00;
	out 0x2f,R2
	.dbline 105
;  TCCR1B = 0x02; //start Timer
	ldi R24,2
	out 0x2e,R24
	.dbline -2
L7:
	.dbline 0 ; func end
	ret
	.dbend
	.area vector(rom, abs)
	.org 16
	rjmp _Timer1
	.area text(rom, con, rel)
	.dbfile D:\IccAvr\Pei\LYJ\LYJ.c
	.dbfunc e Timer1 _Timer1 fV
	.even
_Timer1::
	rcall push_lset
	.dbline -1
	.dbline 110
; }
; 
; #pragma interrupt_handler Timer1:9
; void Timer1(void)
; {
	.dbline 112
;  //TIMER1 has overflowed
;  TCNT1H = 0xFF; //reload counter high value
	ldi R24,255
	out 0x2d,R24
	.dbline 113
;  TCNT1L = 0x9C; //reload counter low value
	ldi R24,156
	out 0x2c,R24
	.dbline 115
;  
;  CheckIR();//红外解码
	rcall _CheckIR
	.dbline -2
L8:
	rcall pop_lset
	.dbline 0 ; func end
	reti
	.dbend
	.dbfunc e IoInit _IoInit fV
	.even
_IoInit::
	.dbline -1
	.dbline 120
; }
; 
; //call this routine to initialize all peripherals
; void IoInit(void)
; {
	.dbline 122
;  //stop errant interrupts until set up
;  CLI(); //disable all interrupts
	cli
	.dbline 123
;  PortInit();
	rcall _PortInit
	.dbline 124
;  WdtInit();
	rcall _WdtInit
	.dbline 125
;  Timer0Init();
	rcall _Timer0Init
	.dbline 126
;  Timer1Init();
	rcall _Timer1Init
	.dbline 129
;  //ADCSR&=~BIT(ADEN);
;  
;  MCUCR = 0x00;//int0 low level在掉电模式,低电平触发才能唤醒,下跳沿不行
	clr R2
	out 0x35,R2
	.dbline 130
;  GICR  = 0x00;//Int Mask,Int0 disable
	out 0x3b,R2
	.dbline 131
;  TIMSK = 0x05; //timer interrupt Mask: Timer0,1 enable
	ldi R24,5
	out 0x39,R24
	.dbline 132
;  SEI(); //re-enable interrupts
	sei
	.dbline -2
L9:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e WdtOn _WdtOn fV
	.even
_WdtOn::
	.dbline -1
	.dbline 138
;  //all peripherals are now initialized
; }
; 
; 
; void WdtOn()
; {
	.dbline 139
;   WDR();
	wdr
	.dbline 140
;   WDTCR=0x0f;
	ldi R24,15
	out 0x21,R24
	.dbline -2
L10:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e WdtOff _WdtOff fV
	.even
_WdtOff::
	.dbline -1
	.dbline 145
; 
; }
; 
; void WdtOff()
; {
	.dbline 146
;   CLI();//!!必须,WDTOE置位后必须4个时钟内清 WDE,才能真正关断WDT
	cli
	.dbline 147
;   WDR();
	wdr
	.dbline 148
;   WDTCR |=(BIT(WDTOE)|(BIT(WDE)));
	in R24,0x21
	ori R24,24
	out 0x21,R24
	.dbline 150
;   //WDTCR&=~BIT(WDE);//0;//off
;   WDTCR=0x07;
	ldi R24,7
	out 0x21,R24
	.dbline 151
;   SEI();
	sei
	.dbline -2
L11:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e PwrIdle _PwrIdle fV
	.even
_PwrIdle::
	.dbline -1
	.dbline 155
; }
; 
; void PwrIdle()
; {//P30 MCUCR 休眠控制和INT0,1,
	.dbline 156
;   MCUCR&=0x0f;
	in R24,0x35
	andi R24,15
	out 0x35,R24
	.dbline 157
;   MCUCR |=BIT(SE);
	in R24,0x35
	ori R24,128
	out 0x35,R24
	.dbline 158
;   asm("sleep");
	sleep
	.dbline -2
L12:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e PwrDown _PwrDown fV
	.even
_PwrDown::
	.dbline -1
	.dbline 162
; }
; 
; void PwrDown()
; { 
	.dbline 163
;   MCUCR&=0x0f;
	in R24,0x35
	andi R24,15
	out 0x35,R24
	.dbline 164
;   MCUCR |=0x60;//0x60掉电 0x70省电
	in R24,0x35
	ori R24,96
	out 0x35,R24
	.dbline 165
;   asm("sleep");
	sleep
	.dbline -2
L13:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e TestIR _TestIR fV
	.even
_TestIR::
	.dbline -1
	.dbline 171
; }
; 
; //测试红外输出
; //无信号时有小许干扰输出,遥控器保持按键时有持续输出
; void TestIR()
; {
	.dbline 172
; if ((PIND&(1<<IR))!=0)
	sbis 0x10,7
	rjmp L15
	.dbline 173
; 	     {BeepOff();}
	.dbline 173
	rcall _BeepOff
	.dbline 173
	rjmp L16
L15:
	.dbline 175
	.dbline 175
	rcall _BeepOn
	.dbline 175
L16:
	.dbline -2
L14:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e main _main fV
	.even
_main::
	.dbline -1
	.dbline 180
;       else
; 	     {BeepOn();}
; 
; }
; 
; void main(void)
; {
	.dbline 182
; 
;    IoInit();
	rcall _IoInit
	.dbline 183
;    SystemIni();
	rcall _SystemIni
	.dbline 184
;    Beep();
	rcall _Beep
	rjmp L19
L18:
	.dbline 186
;    while (1)
;    {  
	.dbline 187
;       if (bTimer) TimerFunc();
	lds R2,_bTimer
	tst R2
	breq L21
	.dbline 187
	rcall _TimerFunc
L21:
	.dbline 189
; 	  //TestIR();
; 	  if (bIrCmdOk) CheckIrCmd();
	lds R2,_bIrCmdOk
	tst R2
	breq L23
	.dbline 189
	rcall _CheckIrCmd
L23:
	.dbline 190
; 	  if (bCmdOk) ReadCmd();
	lds R2,_bCmdOk
	tst R2
	breq L25
	.dbline 190
	rcall _ReadCmd
L25:
	.dbline 191
	rcall _PwrIdle
	.dbline 192
L19:
	.dbline 185
	rjmp L18
X0:
	.dbline 192
; 	  PwrIdle();
;     };
	.dbline -2
L17:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e ReadCmd _ReadCmd fV
	.even
_ReadCmd::
	rcall push_gset1
	.dbline -1
	.dbline 198
; 
; }
; 
; //命令执行
; void ReadCmd()
; {
	.dbline 199
;     bCmdOk=0;
	clr R2
	sts _bCmdOk,R2
	.dbline 201
; 	//Beep();
; 	switch (iCmd)
	lds R20,_iCmd
	clr R21
	cpi R20,1
	ldi R30,0
	cpc R21,R30
	breq L31
	cpi R20,2
	ldi R30,0
	cpc R21,R30
	breq L32
	cpi R20,3
	ldi R30,0
	cpc R21,R30
	breq L33
	cpi R20,4
	ldi R30,0
	cpc R21,R30
	breq L34
	cpi R20,5
	ldi R30,0
	cpc R21,R30
	breq L37
	rjmp L28
X1:
	.dbline 202
; 	{
L31:
	.dbline 204
; 	   case CSTOP:
; 	       MotorStop();
	rcall _MotorStop
	.dbline 205
; 		   BeepN(2);
	ldi R16,2
	ldi R17,0
	rcall _BeepN
	.dbline 206
; 	       break;
	rjmp L29
L32:
	.dbline 208
; 	   case CUP:
; 	       MotorUp();
	rcall _MotorUp
	.dbline 209
; 		   BeepN(1);
	ldi R16,1
	ldi R17,0
	rcall _BeepN
	.dbline 210
; 	       break;
	rjmp L29
L33:
	.dbline 212
; 	   case CDOWN:
; 	       MotorDown();
	rcall _MotorDown
	.dbline 213
; 		   BeepN(1);
	ldi R16,1
	ldi R17,0
	rcall _BeepN
	.dbline 214
; 		   break;
	rjmp L29
L34:
	.dbline 216
; 	   case CLAMP:
; 	       if ((PORTC&(1<<LAMP))==0)
	sbic 0x15,2
	rjmp L35
	.dbline 217
; 		     { LampOn();
	.dbline 217
	rcall _LampOn
	.dbline 218
; 			   BeepN(1);
	ldi R16,1
	ldi R17,0
	rcall _BeepN
	.dbline 219
; 			 }
	rjmp L29
L35:
	.dbline 221
; 		   else
; 		     { LampOff();
	.dbline 221
	rcall _LampOff
	.dbline 222
; 			   BeepN(2);
	ldi R16,2
	ldi R17,0
	rcall _BeepN
	.dbline 223
; 			 }
	.dbline 224
; 		   break;
	rjmp L29
L37:
	.dbline 226
; 	   case CDIS:
; 	       if ((PORTC&(1<<DIS))==0)
	sbic 0x15,3
	rjmp L38
	.dbline 227
; 		     { DisOn();
	.dbline 227
	rcall _DisOn
	.dbline 228
; 			   BeepN(1);
	ldi R16,1
	ldi R17,0
	rcall _BeepN
	.dbline 229
; 			 }
	rjmp L29
L38:
	.dbline 231
; 		   else
; 		     { DisOff();
	.dbline 231
	rcall _DisOff
	.dbline 232
; 			   BeepN(2);
	ldi R16,2
	ldi R17,0
	rcall _BeepN
	.dbline 233
; 			 }
	.dbline 234
; 		   break;
L28:
L29:
	.dbline -2
L27:
	rcall pop_gset1
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e CheckIR _CheckIR fV
;           iPos -> R16
;        iNowBit -> R18
	.even
_CheckIR::
	.dbline -1
	.dbline 240
; 	}
; }
; 
; //红外解码
; void CheckIR()
; {uchar iNowBit,iPos;
	.dbline 242
	clr R2
	in R24,0x10
	andi R24,128
	cp R2,R24
	brsh L41
	.dbline 242
	clr R18
	rjmp L42
L41:
	.dbline 242
;    
;    if ((PIND&(1<<IR))>0) iNowBit=0;else iNowBit=1;//先反相
	ldi R18,1
L42:
	.dbline 245
;    
;    //把信号持续的处理作为公共处理
;    if ((iPreBit==1)&&(iNowBit==1))//hight++
	lds R24,_iPreBit
	cpi R24,1
	brne L43
	cpi R18,1
	brne L43
	.dbline 246
; 		  { iHight++;
	.dbline 246
	lds R24,_iHight
	subi R24,255    ; addi 1
	sts _iHight,R24
	.dbline 247
; 		    return;
	rjmp L40
L43:
	.dbline 249
; 		  }
;    if ((iPreBit==0)&&(iNowBit==0))//low++
	lds R2,_iPreBit
	tst R2
	brne L45
	tst R18
	brne L45
	.dbline 250
; 		  { iLow++;
	.dbline 250
	lds R24,_iLow
	subi R24,255    ; addi 1
	sts _iLow,R24
	.dbline 251
; 		    return;
	rjmp L40
L45:
	.dbline 254
; 		  }
; 		  
;    switch (iIrMode)
	lds R16,_iIrMode
	clr R17
	cpi R16,0
	cpc R16,R17
	breq L50
X2:
	cpi R16,1
	ldi R30,0
	cpc R17,R30
	breq L57
	cpi R16,2
	ldi R30,0
	cpc R17,R30
	brne X6
	rjmp L65
X6:
	rjmp L47
X3:
	.dbline 255
;    {
L50:
	.dbline 257
;      case 0://等待9ms的高电平
; 	      if ((iPreBit==0)&&(iNowBit==1))//0->1:start
	lds R2,_iPreBit
	tst R2
	brne L51
	cpi R18,1
	brne L51
	.dbline 258
; 		  { iPreBit=1;
	.dbline 258
	ldi R24,1
	sts _iPreBit,R24
	.dbline 259
; 		    iHight=1;
	sts _iHight,R24
	.dbline 260
; 			return;
	rjmp L40
L51:
	.dbline 263
; 		  }
; 		  
; 		  if ((iPreBit==1)&&(iNowBit==0))//1->0
	lds R24,_iPreBit
	cpi R24,1
	breq X7
	rjmp L48
X7:
	tst R18
	breq X8
	rjmp L48
X8:
	.dbline 264
; 		  { iPreBit=0;
	.dbline 264
	clr R2
	sts _iPreBit,R2
	.dbline 265
; 		    if (iHight>85)//9.0ms
	ldi R24,85
	lds R2,_iHight
	cp R24,R2
	brsh L55
	.dbline 266
; 			  { iIrMode=1;
	.dbline 266
	ldi R24,1
	sts _iIrMode,R24
	.dbline 267
; 			    iLow=1;
	sts _iLow,R24
	.dbline 268
; 				iHight=0;
	clr R2
	sts _iHight,R2
	.dbline 270
; 			    //bCmdOk=1;iCmd=10;//Test..
; 			  }
	rjmp L40
L55:
	.dbline 272
; 			else
; 			   {iHight=0;//reset
	.dbline 272
	clr R2
	sts _iHight,R2
	.dbline 273
; 			   }
	.dbline 274
; 			return;
	rjmp L40
X4:
	.dbline 276
; 		  }
; 	      break;
L57:
	.dbline 279
; 		  
; 	 case 1://等待4.5ms的低电平
; 	      if ((iPreBit==0)&&(iNowBit==1))//0->1
	lds R2,_iPreBit
	tst R2
	breq X9
	rjmp L48
X9:
	cpi R18,1
	breq X10
	rjmp L48
X10:
	.dbline 280
; 		  { iPreBit=1;
	.dbline 280
	ldi R24,1
	sts _iPreBit,R24

⌨️ 快捷键说明

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