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

📄 ir_america_recieve.c

📁 红外对射dfgdhfghfghfhh 矗二直矸直
💻 C
字号:
#include "AT892051.H"
#define uchar unsigned char
#define uint  unsigned int
bit wave_flag = 1;
bit alarm;
bit led_flash_switch = 0;
bit delay_one_flag = 0;
bit one_two_pulse;
bit beam_pulse_switch;

uchar state_pulse;
uchar led_count = 0;
uchar syn_alarm;
uchar syn_timer;
uchar beam_num;
uchar beam_num_tmp;
uint recieve_flag = 0;
uint recieve_flag_tmp;
uchar one_two_timer;
uchar one_two_timer_value = 30;
uchar beam_pulse_counter = 1;

uint  double_alarm_flag[9] = {0x00ff,0x027f,0x033f,0x039f,0x03cf,0x03e7,0x03f3,
	  					      0x03f9,0x03fc};
void timer1(void) interrupt 3
{
	wave_flag = 0;
	alarm = 1;
}
void delay_200us(void)
{
}
void delay_decode(void)
{
	uint i;
	for(i = 0;i<58;i++) {}
}
void recieve_fun(void)
{
 	 //uint i;
 	 //for(i = 0;i<6700;i++) {} // ~33.5ms
	 //PORTD |= BIT1;
	 // 1
	 /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 //P1 = 0xff;
	 //P3 |= 0xfc;
 	 if(P1_6 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	// debug 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	    delay_200us();
		recieve_flag = (recieve_flag <<1);
		recieve_flag |= 0x00;
	 }
	 delay_decode();
	 // 2
	/*PORTD &=~BIT1;
		  delay_200us();
		 PORTD |= BIT1;*/
	 if(P1_7 == 0)
	 {
         P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();
	// 3
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 //P3_5 = 1;
	 if(P3_5 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();
// 4
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_1 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();
// 5
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_0 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();	
// 6
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P3_7 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();		
// 7
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_2 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();		  
// 8
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_3 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();	
// 9
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_4 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();		 	 
// 10
	     /*PORTD &=~BIT1;
		  delay_200us();
		  PORTD |= BIT1;*/
	 if(P1_5 == 0)
	 {
          P3_1 = 0;
		  delay_200us();
		  P3_1 = 1;
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x01;
	 }
	 else
	 {
	      delay_200us();
		  recieve_flag = (recieve_flag <<1);	 
		  recieve_flag |= 0x00;
	 }
		 delay_decode();		 	 
}

void syn_judge(void)
{
 	 //uint i; 
	 uint count_value_tmp;
 	 uint timer1_count_value;
	 //********************************************
state_channel:TR1 = 0;
	 TH1 = 0x00;
	 TL1 = 0x00;
	 TF1 = 0;
	 TR1 = 1;
	 syn_timer = 0;
	 state_pulse = 0;
	 one_two_pulse = 0;
	 one_two_timer = 0;
	 // test start
	 do
	 {
	  syn_timer = 0;
	  TR0 = 0;
	  TH0 = 0x00;
	  TL0 = 0x00;
	  TF0 = 0;
	  TR0 = 1;
      
	  
	// P1_6 = 1;
	 while(!((P1_6 == 1) && (P1_7 == 1))) {}
 	 while(!((P1_6 == 0) || (P1_7 == 0))) 
	 {
		if(TF0 == 1) 
		{
			TF0 = 0;
			syn_timer++;
			if(syn_timer == 2)
			{
				alarm = 1;
				TR0 = 0;
				TF0 = 0;
				goto judge_end;
			}
		}
	 
	 }
	
	// P1_6 = 1;
	  TR0 = 0;
	  TH0 = 0x00;
	  TL0 = 0x00;
	  TF0 = 0;
	  TR0 = 1;
	 while(!((P1_6 == 1) && (P1_7 == 1))) {}  //if syn(PD1) = 0,then wait
	 TR0 = 0; 		   	  	  //timer1 counter stop

	 
     	 
	 //Get timer1 counter value 
	 count_value_tmp = 0x00ff;
	 timer1_count_value = 0x00ff;
	 timer1_count_value &= TL0;
	 count_value_tmp &= TH0;
	 //count_value_tmp<<8;
	 timer1_count_value |= (count_value_tmp<<8);
	 
	 if(((150 < timer1_count_value) && (timer1_count_value < 350)))
	 {
	 	one_two_pulse = 1;
		state_pulse++;
		if(state_pulse == 3)
		{
			goto judge_start;
		}
	 }
	 else
	 {

 		state_pulse = 0;
	 }


	 if(beam_pulse_switch == 1)
	 {
	    if(TF1 == 1) 
		{
			TF1 = 0;
			one_two_timer++;
			if(one_two_timer > one_two_timer_value)
			{
				one_two_timer_value = 5;

				
				if(one_two_pulse == 0)
				{
					beam_pulse_switch = 1;
					alarm = 1;
					TR1 = 0;
					TF1 = 0;
					goto judge_end;
				}
				else
				{
					one_two_timer = 0;
					one_two_pulse = 0;
					beam_pulse_switch = 0;

				}
			}
		 }
	  }




		}
	 while(1);
	 // test end
judge_start:do
	 {
	 
 	  TR0 = 0;
	  TH0 = 0x00;
	  TL0 = 0x00;
	  TF0 = 0;
	  TR0 = 1;
	// P1_6 = 1;
	 while(!((P1_6 == 1) && (P1_7 == 1))) {}
 	 while(!((P1_6 == 0) || (P1_7 == 0))) 
	 {
	 	if(TF0 == 1) 
		{
			TF0 = 0;
			syn_timer++;
			if(syn_timer == 2)
			{
				alarm = 1;
				TR0 = 0;
				TF0 = 0;
				goto judge_end;
			}
		}
	}  //if syn(PD1) = 1,then wait
	// P1_6 = 1;
	  TR0 = 0;
	  TH0 = 0x00;
	  TL0 = 0x00;
	  TF0 = 0;
	  TR0 = 1;
	 while(!((P1_6 == 1) && (P1_7 == 1))) {}  //if syn(PD1) = 0,then wait
	 TR0 = 0; 		   	  	  //timer1 counter stop
    /* if(TF1 == 1) 
	 {
		TF1 = 0;
	 	syn_timer++;
	 	if(syn_timer == 2)
	 	{
	 		alarm = 1;
	 		TR1 = 0;
	 		TF1 = 0;
	 		goto judge_end;
	 	}
	 }*/

	 
	 
	 //Get timer1 counter value 
	 count_value_tmp = 0x00ff;
	 timer1_count_value = 0x00ff;
	 timer1_count_value &= TL0;
	 count_value_tmp &= TH0;
	 //count_value_tmp<<8;
	 timer1_count_value |= (count_value_tmp<<8);
	 
	 if(((2950 < timer1_count_value) && (timer1_count_value < 3150)))
	 {
		beam_pulse_switch = 1;
		delay_one_flag = 0; 
		goto judge_end;
	 }
	 else
	 {
		goto state_channel;
	 }
	
	 }
	 //-----------------------------------
	 while(1); // 650us--750us
	 
 	//
judge_end:TR1 = 0;
		  TR0 = 0; 		   	  	  //timer1 counter stop
}

void main(void)
{
	uint count;
	/*uchar a[2],x=0;
	uint b[1],*p;
	TH0 = 4;
	TL0 = 5;
	TMOD = 0x01;
	//TR0 = 1;		// ENABLE TIMER0 COUNTER
	a[1] = TL0;
	a[0] = TH0;
	p = a;*/
	
	TH0 = 0;
	TL0 = 0;
	TH1 = 0;
	TL1 = 0;
	TMOD = 0x11;
	//EA = 1;
	//ET1 = 1;
	
	beam_num_tmp = (P3 & 0x18);
		if(beam_num_tmp == 0x00)
		 beam_num = 3;
	 else if(beam_num_tmp == 0x08)
	     beam_num = 5;
	 else if(beam_num_tmp == 0x10)
	     beam_num = 7;
	 else if(beam_num_tmp == 0x18)
	     beam_num = 9;	
	while(1)
	 {
	  	syn_judge();	
		//if(wave_flag == 0) goto alarm_place;
		//for(count = 0;count<6720;count++) {} // 33.5ms
	
			TH0 = 0xfc; 	//1ms
	 		TL0 = 0x17;
			TF0 = 0;
	 		TR0 = 1;
	 		while(TF0 == 0) {}
	 		TR0 = 0;
	 		TF0 = 0;

			for(count = 0;count<13;count++) {}
		
	  	recieve_fun();
		recieve_flag_tmp = recieve_flag;
		if(P3_2 == 0)
		{
			for(count = 0;count < beam_num;count++)
			{
			 	if((recieve_flag_tmp | double_alarm_flag[count]) == double_alarm_flag[count]) {alarm = 1;syn_alarm = 3;}
			}
		}
		else
		{
		 	if(beam_num == 3)
			{
		     			if((recieve_flag_tmp & 0x03c0) != 0x03c0) {alarm = 1;syn_alarm = 2;}   // 4
			}
			if(beam_num == 5)
			{
		     			if((recieve_flag_tmp & 0x03f0) != 0x03f0) {alarm = 1;syn_alarm = 2;}   // 6
			}
			if(beam_num == 7)
			{
		     			if((recieve_flag_tmp & 0x03fc) != 0x03fc) {alarm = 1;syn_alarm = 2;}   // 8
			}
			if(beam_num == 9)
			{
		     			if((recieve_flag_tmp & 0x03ff) != 0x03ff) {alarm = 1;syn_alarm = 2;}   // 10
			}
		}
		
	 if(alarm == 1)
	 {
	 	
	    led_flash_switch = 1;
		P3_0 = 0;
		for(count = 0;count < 20;count++)//1s
		{
		  TH0 = 0x3c; 	//50ms
	 	  TL0 = 0xaf;
	 	  TR0 = 1;
	 	  while(TF0 == 0) {}
	 	  TR0 = 0;
	 	  TF0 = 0;
		}

		P3_0 = 1;
		for(count = 0;count < 10;count++)//0.5s
		{
		  TH0 = 0x3c; 	//50ms
	 	  TL0 = 0xaf;
	 	  TR0 = 1;
	 	  while(TF0 == 0) {}
	 	  TR0 = 0;
	 	  TF0 = 0;
		}
	 }
	 
	 
		alarm = 0;
		wave_flag = 1;
		recieve_flag = 0;

		
//	while(1) {}
	}
	
}

⌨️ 快捷键说明

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