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

📄 l050526_rs232.c

📁 串口发卡器程序!hc4060做的模块!rfid
💻 C
字号:
//------------------------------------------------

#include<at892051.h>

#include<stdio.h>

#include<intrins.h>

typedef unsigned char  		u8;

typedef unsigned int 			u16;

typedef unsigned long int u32;
//------------------------------------------------端口设定
sbit pout  	= P3^3;
sbit BMP  	= P3^5;
sbit LED  	= P1^7;
sbit BP   	= P1^6;	
sbit KEY  	= P1^5;
sbit BAT  	= P1^4;
sbit A_DATA = P1^3;
sbit A_CLK  = P1^2;
sbit DATA = P1^0;
sbit CLK  = P1^1;

//------------------------------------------------RAM分配
u8  data xx;
u8 	data 	btime;
u8 	data 	etime;
u8 	data 	ctime;
u8 	data 	gtime;
u8 	data 	rdata[15];
u8 	data 	rdbak[15];
u8 	data 	mdata;
u8 	data 	erord[2];
u8 	data 	xlll;
u8 	data 	noka;
bit comflag;
//------------------------------------------------
void DelayUs(u16 us) 
{
	while(--us);
}
//------------------------------------------------
//定时器1 = 128us
//
void timer1(void) interrupt 3   
{
	if( comflag==0)
{
	++btime; 
	++ctime;
 	TF1=0;}
}
//------------------------------------------------
// TH1 = 0x14=22.1184  0x8A=11.0592 0x80=12.00
// 12/XTAL = XXus 
// 初始化Timer0
void Timer1_rs(void)      
{
 	TMOD = 0x20;
 	TH1  = 0x14;  
 	EA   = 1;
 	ET1  = 1;
	comflag=0;
}
//------------------------------------------------
void init_rs232(void)
{
	SCON = 0x50;
	TMOD = 0x20;
	TH1  = 0xFA;	//FD=B/11920 FA=B9600 XTAL=22.1184
	TR1  = 1; 
	TI   = 1; 
	comflag=1;
}
//------------------------------------------------
void send_char(u8 c)
{
	while(1)if(TI==1)break;
	TI=0;
	SBUF=c;
	while(TI==1);
	DelayUs(250);
}

//------------------------------------------------
// rd_data_clr()函数--清除55位数据缓冲区

void rd_data_clr(void)
{
	u8 c;
	for(c=0;c<10;c++)
	{
		rdata[c]=0;
	}
}

//------------------------------------------------
// 
//
u8 rd_start_bit(void)
{
 	btime=0;
	while(pout==1)
	{
		if( btime>12 && btime<3)
			return(0);  
	}
	while(pout==0)
	{
		if( btime>12 && btime<3)
			return(0); 
	}
	btime=0;
	return(1);
}
//------------------------------------------------
// 
//
u8 rd_next_bit(void)
{
	u8 x;
  while(btime!=3);
  x=pout;
  while(btime!=4);
	btime=0;
	return(x);	     
}

//------------------------------------------------
// 
// 
u8 rd_head_9bit(void)
{
	u8 x = 0, y = 0,z = 0;                
  while( x != 0x09 )                 
  { 
  	z=rd_next_bit();                           
    if( z == 1 )
			x++;                    
    else
		{ 
			x = 0;
			y++; 
			if( y > 9 )
		 		return(0); 
			}                        
	}
  return(1);                          
}

//------------------------------------------------
//
//
u8 chenk_pair(u8 x)
{
	u8 i,temp,char_temp;
 	bit flag_check	=0;
 	bit flag_pair;

 	temp	=x;
 	flag_pair	= temp&0x01;
 	temp	= temp>>1;
 	for ( i=1; i<5; i++)
  {
		char_temp=temp&0x01;
    if ( char_temp==0x01 )
    {
			flag_check=!flag_check;
    }
    temp=temp>>1;
  }
 	if(flag_pair==flag_check)
		return(0);
 	else	
		return(1);
}

//------------------------------------------------
// 
//
u8 rd_data_pair(void)
{
	u8 x;
	for(x=0;x<10;++x)
	{
	if( chenk_pair(rdata[x]) ) 
		return(0); 
	}
  	return(1);
}


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

u8 rd_data_lpair1(u8 x)
{
	u8 	ltemp,i;
	bit	lflag=0;

	for(i=0;i<11;)
	{
		ltemp=rdata[i];
		ltemp=ltemp>>x;
		ltemp=ltemp&0x01;
		if(i==10)
		{
			if(ltemp==lflag)
				return(0);
			else
				return(1);  
		}
		i++;
		if(ltemp==0x01)
			lflag=~lflag;			
	}
}

//------------------------------------------------
u8 rd_data_lpair()
{
if (  rd_data_lpair1(1) ) return(0);
if (  rd_data_lpair1(2) ) return(0);
if (  rd_data_lpair1(3) ) return(0);
if (  rd_data_lpair1(4) ) return(0);
return(1);
}

//------------------------------------------------
//
//
u8 rd_data_read()
{
	u8 i = 55,j = 0,k = 0;

  rd_data_clr();
  Timer1_rs();
  TR1=1;
  if ( rd_start_bit() )
	{
		if ( rd_head_9bit() )
	 	{
	  	while ( i-- )
	  	{ 
	   		mdata = rd_next_bit();
	  		rdata[j] = rdata[j]|mdata;
	   		++k;
	   		if ( k == 5 )
				{ 
				++j; 
				k = 0; 
				}
	   		rdata[j]<<=1;
			}

			if ( rd_data_pair() )
			{				
	 			if ( rd_data_lpair() )
	 			{                          	
	  			TR1 = 0;
	  			return(1);
	 			}
	   	}	
	  }	
	}
TR1 = 0;
return(0);
}
//------------------------------------------------
//
//
void rd_data_conver(void)
{ 
	unsigned long div,dd,id1,id2,id3,id4;
	u8 i;
		
	for(i=0;i<10;++i)
	{
	rdata[i]=rdata[i]>>1;
	}
	rdata[2]=rdata[2]<<4;
	rdata[4]=rdata[4]<<4;
	rdata[6]=rdata[6]<<4;
	rdata[8]=rdata[8]<<4;
	id1=rdata[2]|rdata[3];
	id1=0;
	id2=rdata[4]|rdata[5];
	id3=rdata[6]|rdata[7];
	id4=rdata[8]|rdata[9];
	dd=id1<<24|id2<<16|id3<<8|id4;
	div=1000000000;
	init_rs232();
	send_char('[');
	for (i=0;i<10;i++) {
		rdata[i]=dd/div;
		rdata[i]=rdata[i]+0x30;
		send_char(rdata[i]);
		dd%=div;
		div/=10;
	}
	send_char(']');
	TR1=0;
}
//------------------------------------------------
//------------------------------------------------
// MAIN    
//
void main(void)
{
	CLK=1;
	DATA=1;
	BMP=1;
	pout=1;
	KEY=1;
	BAT=0;
	erord[0]=0;
	erord[1]=0;	
	xlll=0;
	BMP=0;
	BP=0;
	LED=0;
	ctime=0;
	noka=0;
	BP=1;
	DelayUs(50000);
	DelayUs(50000);
	BP=0;
	DelayUs(10000);
	DelayUs(10000);
	BP=1;
	DelayUs(10000);
	DelayUs(10000);
	BP=0;
	DelayUs(10000);
	DelayUs(10000);
	BP=1;
	DelayUs(10000);
	DelayUs(10000);
	BP=0;	
	DelayUs(10000);
	DelayUs(10000);
	BP=1;
	DelayUs(10000);
	DelayUs(10000);
	BP=0;	
while(1)
{	

  if ( rd_data_read() )
	{
		gtime=0;
		erord[xlll]=rdata[2]+rdata[3]
		+rdata[4] +rdata[5] +rdata[6]
		+rdata[7] +rdata[8] +rdata[9];
		++xlll;
		if(xlll==2)
    {
			gtime=0;
	   	xlll=0;
      if(erord[0]==erord[1])
	    {
				if(erord[1]!=0)
				{
					if(noka==1)
					{
	     			rd_data_conver();
						BP=1;
						LED=1;
						DelayUs(8000);
						DelayUs(8000);
						BP=0;
						LED=0;
						DelayUs(5000);
						DelayUs(5000);
						BP=1;
						LED=1;
						DelayUs(10000);
						DelayUs(10000);
						BP=0;	
						LED=0;
						noka=0;
						gtime=0;
						}
				}
			}
	  }
	}
	if(ctime >=100)	{	++gtime; ctime=0;	}
	if(gtime >=26 )	{	noka=1;	 gtime=0; }	

	}
	
}
					
//------------------------------------------------
//------------------------------------------------

⌨️ 快捷键说明

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