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

📄 main.c

📁 2051控制无线收发器,通过串口无线收发数据.
💻 C
字号:
/*--------------------------------------------------------
         DATE:
         AUTHOR:
         VERSION:  V1.0
---------------------------------------------------------*/
#include<reg52_r.h>
#include <intrins.h>

#define NOP  _nop_( ); _nop_( ); _nop_( ); _nop_( )

#define uchar unsigned char
#define uint  unsigned int
#define ulong unsigned long

#define SW_TEMP   		P33
#define C_POWER	  		P37

#define TEST_PIN    	P34

#define P_TXD    		P31

#define INBUF_LEN 4   //数据长度

#define ID_H    0x80
#define ID_L	0x01


uchar code output_D[10]={0x30,0xF3,0x29,0x23,0xE2,0x26,0x24,0x73,0x20,0x22};

//uchar code cmd_long_key[1]={0xA3};
//uchar code cmd_shot_key[1]={0xA2};
//uchar code cmd_vshot_key[1]={0xA1};

uchar code cmd_long_key[7] ={0x02,0x05,ID_H,ID_L,0xA3,0x00,0x03};                    	//???????????
uchar code cmd_shot_key[7] ={0x02,0x05,ID_H,ID_L,0xA2,0x00,0x03};						//???????????
uchar code cmd_vshot_key[7]={0x02,0x05,ID_H,ID_L,0xA1,0x00,0x03};						//???????????

uchar idata i,k;
bit   bdata f_working,f_100ms,f_40ms;
uchar idata work_mode;

uchar receive_buf[16],rcv_data[16];

uchar idata checksum,count3;
bit   bdata read_flag=0;

uchar idata key_count,cmd_index;
bit   bdata f_new_key,f_new_receive,f_new_data,f_receiving_0,f_0x10;

uchar idata  chr;


ulong idata  power_time;


uchar idata u_count;


uchar idata receive_time;

uchar idata dis_vol,dis_work_mode;
bit   bdata f_dis_work_mode;

void delay(uint n)
{
uint i;
for(i=0;i<n;i++)
{NOP;

}

}


void timer0(void) interrupt 1
{
   TH0=0x63;              //T0 40MS
   TL0=0xC1;

   f_40ms=1;

   if(receive_time)
   		receive_time--;

}

//向串口发送一个字符
void send_char_com(unsigned char ch)
{
    SBUF=ch;
    while(TI==0);
    TI=0;
}

//向串口发送一个字符串,strlen为该字符串长度
void send_string_com(unsigned char *str,unsigned int strlen)
{

    unsigned int k=0;
	P_TXD=1;

	delay(4000);
	
    do
    {
        send_char_com(*(str + k));
        k++;
    } while(k < strlen);

	P_TXD=0;
}


//串口接收中断函数
void serial () interrupt 4 using 3
{	uint temp,i;
    if(RI)
    {
        RI = 0;
		temp = SBUF;
		if((temp==0x02)&&(f_0x10==0))
		{	receive_buf[0]=0x02;
			u_count=1;
        	f_receiving_0=1;
			receive_time=60;
			return;
		}

    	if((temp==0x03)&&(f_0x10==0)&&(f_receiving_0==1))
    	{	receive_buf[u_count]=0x03;
     		f_receiving_0=0;
			f_new_receive=1;
			u_count++;
			for(i=0;i<u_count;i++)
			{	
				rcv_data[i]=receive_buf[i];
	    	}	
	    	u_count=0;
			return;
		}
		
		if(((temp==0x10)||(temp==0x02)||(temp==0x03))&&(f_receiving_0==1)&&(f_0x10==1))
		{	receive_buf[u_count]=temp;
			u_count++;
			f_0x10=0;
			return;
		}	

		if((temp==0x10)&&(f_receiving_0==1)&&(f_0x10==0))
		{	f_0x10=1;
			return;
		}						

		if((f_receiving_0==1)&&(u_count<16))
		{	receive_buf[u_count++]=temp;
			f_0x10=0;
		}	    
		else
			if(u_count==16)
			{ 	u_count=0;
				f_0x10=0;
				f_receiving_0=0;
			}
	}

}

void init_timer0(void)
{
   IP=0x00;
   TMOD |=0x01;            //T0 MODE 2,SOFT TIMER  0.5MS
   TH0=0x63;               //T0 40MS
   TL0=0xC1;
   TF0=0;
   TR0=1;
   ET0=1;           	   //开T0中断
}

void init_serialcomm(void)
{
    SCON  = 0x50;       	//SCON: serail mode 1, 8-bit UART, enable ucvr
    TMOD |= 0x20;       	//TMOD: timer 1, mode 2, 8-bit reload
    PCON |= 0x80;       	//SMOD=1;
//    TH1   = 0xD0;       	//Baud:2400  fosc=11.0592MHz    
    TH1   = 0xE0;       	//Baud:2400  fosc=11.0592MHz
    IE   |= 0x90;       	//Enable Serial Interrupt
    TR1   = 1;          	// timer 1 run
   // TI=1;
}




//----------------------MAIN---------------------------
void Main(void)
{


uchar j;

uchar voltage;


   init_timer0();  		//初始化定时器0
   init_serialcomm();  		//初始化串口
   EA=1;

   P1=0xFF;
   C_POWER = 0;

   power_time=0;

   P_TXD=0;

   while(1)
   {
   	if(f_40ms==1)

	{   f_40ms=0;
	    i++;
		if((SW_TEMP==1)&&(key_count>0))
			f_new_key=1;


		if((SW_TEMP==0)&&(key_count<250))
		{	key_count++;
			P1=0xFF;
		}
		
		TEST_PIN =!TEST_PIN;

		power_time++;	
	}

	if(f_new_key==1)
	{	f_new_key=0;

		power_time=0;
	
		if(key_count<=2)
			key_count=0;
		
		if((key_count>2)&&(key_count<20))                  //		if((key_count>2)&&(key_count<25))
		{	key_count=0;
			cmd_index = 1;
		}
		
		if((key_count>=38)&&(key_count<75))					//		if((key_count>=50)&&(key_count<100))
		{	key_count=0;
			cmd_index = 2;		
		}
		
		if(key_count>=75)									//		if(key_count>=125)
		{	key_count=0;
			cmd_index = 3;
		}
		
	}

	if(cmd_index)
	{	switch(cmd_index)
		{
			case 1:   send_string_com(cmd_vshot_key, 7);
					  P1=0xFF;
					  break;
			case 2:   send_string_com(cmd_shot_key, 7);
					  P1=0xFF;
					  break;
			case 3:   send_string_com(cmd_long_key, 7);
					  P1=0xFF;
					  break;
			default:  break;
		}
		cmd_index=0;
	}


	if(f_new_receive==1)
	{	f_new_receive=0;

		f_new_data = 1;

//		if(rcv_data[(rcv_data[1]+2)]!=0x03)
//		{	f_new_data = 0;
//		}

//		if(rcv_data[(rcv_data[1]+2)]!=Get_CheckSum(rcv_data))
//		{	f_newc_data = 0;
//		}
//		if((rcv_data[2]!=eq_sn[0])||(rcv_data[3]!=eq_sn[1])||(rcv_data[4]!=eq_sn[2])||(rcv_data[5]!=eq_sn[3]))
		if((rcv_data[1]!=ID_H)||(rcv_data[2]!=ID_L))                             //???????????
		{	f_new_data = 0;
		}
	}



	if(f_new_data==1)
	{	f_new_data=0;
		chr=(rcv_data[3]>>4)-0x02;
		if(chr==0x00)
		{//	P1=0xE0;
			power_time=0;
			if((rcv_data[3]&0x0F)<0x0A)
			{	dis_vol=rcv_data[3]&0x0F;
				P1=output_D[dis_vol];
				dis_work_mode=0xE0;
				f_dis_work_mode=1;
			}
			else
				P1=0xE0;	

		}
		
		if(chr==0x01)
		{//	P1=0xD0;
			power_time=0;
			if((rcv_data[3]&0x0F)<0x0A)			
			{	dis_vol=rcv_data[3]&0x0F;
				P1=output_D[dis_vol];
				dis_work_mode=0xD0;			
				f_dis_work_mode=1;					
			}
			else
				P1=0xD0;			
		
		}
		if(chr==0x02)
		{//	P1=0xBC;
			power_time=0;
			if((rcv_data[3]&0x0F)<0x0A)				
			{	dis_vol=rcv_data[3]&0x0F;
				P1=output_D[dis_vol];
				dis_work_mode=0xBC;			
				f_dis_work_mode=1;				
			}
			else
				P1=0xBC;			
			
		}
		if((rcv_data[3]&0xF0)==0x80)
		{	//P1=0x26;
			power_time=490;
		}
		
		if((rcv_data[3]&0xF0)==0x90)
		{	P1=0x30;
			power_time=0;
		}	
			
	}

	if(power_time>500)
		C_POWER=1;
	
	if(receive_time==0)
		{ 	u_count=0;
			f_0x10=0;
			f_receiving_0=0;
		}	
					  
	if((power_time>100)&&(f_dis_work_mode==1))
		{	f_dis_work_mode=0;
			P1=dis_work_mode;
		}
		

   }

}

⌨️ 快捷键说明

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