main.c in wg26c.rar


Wiegand 26 serial communication protocol to the agreement, can Wiegand input 26...Original Link
    Sponsored links

			
			#include <reg52.h>
#include <string.h>
#include <intrins.h>
/////////////////////////////////////////////////////////////////////
//89C52特殊寄存器定义
/////////////////////////////////////////////////////////////////////
sfr      WDT_CONTR  =    0xe1;
sfr16    RCAP2LH    =    0xCA;
sfr16    T2LH       =    0xCC;
sfr      AUXR       =    0X8E;
/////////////////////////////////////////////////////////////////////
//端口定义
/////////////////////////////////////////////////////////////////////
sbit	led		=	P0^0;
sbit	wgdata1 =	P1^2;
sbit	wgdata_1  =	P1^3;
sbit	wgdata_0  =	P1^4;
sbit	b_xiepoid = P1^5;
sbit	b_scom  =	P1^6;
sbit	b_wgcode  =	P1^7;
sbit	b_rs485 = P3^2;
bit 	b_rewg	;
bit 	b_sendscom;
bit 	b_norec;

unsigned char idata txwgdata;
unsigned char idata wgnumber;
unsigned char idata SendBuf[15];
unsigned char idata Sendnum;  
unsigned char idata scomdata;
unsigned long bdata wgdata26; 
    
/////////////////////////////////////////////////////////////////////
//延时常数及波特率定义
/////////////////////////////////////////////////////////////////////
#define  OSC_FREQ        11059200L

#define  RCAP2_50us      65536L - OSC_FREQ/240417L
#define  RCAP2_1ms       65536L - OSC_FREQ/12021L
#define  RCAP2_10ms      65536L - OSC_FREQ/1200L

#define  BOUND19200      256 - 3
#define	 BOUND9600		 256 - 6
/////////////////////////////////////////////////////////////////////
//函数原型
/////////////////////////////////////////////////////////////////////
void InitializeSystem();                                                                                                            
void DelayMs(unsigned int j);                                      
void Delay_50us(unsigned char j);        
unsigned char getascii(unsigned char input);
                                                
///++++++++++++++

void main( )
{    
    InitializeSystem( );
    while(1)
	{
		WDT_CONTR = 0x3f;
		if(b_rewg)
		{
			DelayMs(30);
			if(wgnumber != 0)
			{
				wgdata26 = 0;
				wgnumber = 0;
				b_rewg = 0;
			 }
		 }	

		if(b_sendscom)	
		{
			b_sendscom = 0;
			if(b_scom)	
			{
				b_sendscom = 0;
				TI = 1;
			}
		
			else
			{
				b_norec = 0;
				_nop_();
				_nop_();
				_nop_();
				if(!b_norec)
				{
					b_rs485 = 1;			
					TI = 1;
				}
			}
		}
	}
}//main


void ex_int (void) interrupt 2 using 2
{
	
	//unsigned char chkdata = 0,n;
	if(wgnumber == 0)
	{
		b_rewg = 1;
	}
		if(wgdata1==0)	wgdata26 |= 0x00000001;
		else			wgdata26 &= 0xFFFFFFFE;
		wgdata26 <<= 1;
		wgnumber = wgnumber+1;
		
	if(wgnumber > 20)	
	{
		
		SendBuf[0] = wgdata26 >> 1;			
		SendBuf[1] = wgdata26 >> 9;			
		SendBuf[2] = wgdata26 >> 17;
		
		Sendnum = 3;	
		
		wgdata26 = 0;
		wgnumber = 0;
		b_rewg = 0;
		b_sendscom = 1;
	}
}
		
//////////////////////////////////////////////////////////////////////
///系统初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem()
{
    TMOD &= 0x0F;
    TMOD |= 0x21;
	TCON |=	0x04;	
    PCON |= 0x80;
    SCON  = 0x50;
    P0 = P1 = P2 = P3 = 0xFF;
	TH1 = TL1 = BOUND9600; 
    TR1=1;
    IE |= 0x94;
    //AUXR = 0x02;
	wgdata26  = 0;
	wgnumber = 0;
	wgdata_0 = 0;
	wgdata_1 = 0;
	b_rewg = 0;
	b_rs485 = 0;
	b_norec = 0;
	b_sendscom = 0;
	WDT_CONTR = 0x3f;
}

/////////////////////////////////////////////////////////////////////
//串行中断,接收上位机数据
/////////////////////////////////////////////////////////////////////
void seri_int (void) interrupt 4 using 3
{
	unsigned char i;
	if (RI)       		
	{
		RI = 0;
		b_norec = 1;
	}  
	if (TI)   	    	
	{	
			TI = 0;
        	for (i=0; i<Sendnum; i++)
        	{    
     	   		TI   = 0;
     	   		SBUF = SendBuf[i];                
           		while (!TI);
        	} 
			b_rs485 = 0;
   	    	TI = 0;
			ES = 1;	
	}
}

/////////////////////////////////////////////////////////////////////
//用T2做延时子程序
/////////////////////////////////////////////////////////////////////
void DelayMs(unsigned int _MS)
{
    RCAP2LH = RCAP2_1ms;
    T2LH    = RCAP2_1ms;

    ET2     = 0; 	                                   // Disable timer2 interrupt
    T2CON   = 0x04;                                        // 16-bit auto-reload, clear TF2, start timer
    TR2     = 1;
    while (_MS--)
    {   while (!TF2);TF2 = 0;  }
    TR2 = 0;
}
/*
void Delay_50us(unsigned char _50us)
{
    RCAP2LH = RCAP2_50us;
    T2LH    = RCAP2_50us;

    ET2     = 0; 	                                   // Disable timer2 interrupt
    T2CON   = 0x04;	                                   // 16-bit auto-reload, clear TF2, start timer
    while (_50us--)
    {   while (!TF2);TF2 = 0;  }
    TR2 = 0;
}
*/

			click here to download the whole source code package.

			
			


Project Files

    Sponsored links
NameSizeDate
 wg26.plg152.00 B12-04-09 23:48
 wg26.Uv22.15 kB12-04-09 23:48
 wg26_Opt.Bak1.42 kB21-01-08 16:49
 wg26_Uv2.Bak2.01 kB21-01-08 16:49
 main.c4.72 kB21-01-08 15:09
 main.LST10.14 kB21-01-08 15:09
 main.OBJ9.70 kB21-01-08 15:09
 STARTUP.A515.00 kB16-04-02 14:32
 STARTUP.LST11.39 kB21-01-08 15:04
 STARTUP.OBJ749.00 B21-01-08 15:04
 wg269.20 kB21-01-08 15:09
 wg26.hex1.30 kB21-01-08 15:09
 wg26.lnp56.00 B21-01-08 15:09
 wg26.M5110.83 kB21-01-08 15:09
 wg26.Opt1.44 kB12-04-09 23:48
...

Related Items

    Sponsored links