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

📄 epc.c

📁 用C8051F126实现的RFIDEPC CLASS1B协议程序
💻 C
📖 第 1 页 / 共 3 页
字号:
//项目名称:读出器MCU源程序
//软件名称:RFS_1830.c
//软件版本:V2.00
//作	者:张华,陈启槐
//编写日期:2003-12-24
//修改日期:

// 修改记录:
// 2003-12-24:
//     完成对余志良编写的程序进行全面优化,并初步定稿

#include <C8051F120.h>	// Register definition file.
#include <absacc.h>
#include <RFS_1830.h>
#include  "math.h"
#include <intrins.h>

#define false 0
#define true  1

#define channel_numble 	63      // 跳频频道数
#define radom_numble	21      // 跳频初始值个数

#define uchar unsigned char
//FPGA文件长度69972个字节,每一页64个字节69972=64*1093+20
#define FPGA_DATA_PAGE 1093

//define eeprom_unit address
#define eeprom_baudl   0x00		//波特率
#define hard_versionl  0x01		//版本号
#define out_powerl     0x07		//输出功率
#define current_freql  0x09		//当前频率
#define ip_addressl    0x0b		//IP地址
#define shield_wordl   0x0f		//屏蔽字
#define produce_datal  0x14		//生产日期
#define leave_factoryl 0x17		//出厂日期
#define list_codel     0x1b		//产品序列号
#define card_typel     0x20  	//支持卡类型
#define cur_card_typel 0x21		//当前卡类型
#define tag1l 0x22				//第一种标签
#define tag1h 0x00
#define tag2l 0x24
#define tag2h 0x00
#define tag3l 0x26
#define tag3h 0x00
#define mac_code 		0x33	//厂商授权码起始地址
#define mac_checkout	0x37	//厂商授权码校验
#define user_code 		0x38	//用户授权码起始地址
#define user_checkout	0x40	//用户授权码校验
#define config_1820		0x41
#define config_checkout 0x49
#define cur_Ref_Volt    0x50		//当前DAC电压

// 错误返回码
#define error_ok		        0
#define error_no_antenna		1
#define error_no_tag			2
#define error_invalid_tag		3
#define error_power_low	        4
#define error_write_protect	    5

#define error_erase_failure     6
#define error_write_failure	    7
#define error_read_failure	    8

#define error_eeprom            0x10    //读写EEPROM出错
#define error_mac_code          0x11    // 厂商未对读写器授权
#define error_uac_code          0x12    // 用户未对读写器授权

#define error_unknow_command    0x1e    // 未知命令
#define error_other		        0x1f    // 未知错误

// 新定义变量
unsigned char xdata protect_status=0;
unsigned char xdata present_tags=0;
unsigned char xdata global_temp=0;
unsigned long xdata k,m=0;

//the address of sfr in FPGA
#define cmd 			XBYTE[0x2001]
#define scmd 			XBYTE[0x2000]
#define cmd_len 		XBYTE[0x200f]
#define lastf_len 		XBYTE[0x2010]
#define echo_reg 		XBYTE[0x2011]
#define response_addr		XBYTE[0x200e]
#define continue_response1 	XBYTE[0x2008]
#define continue_response2 	XBYTE[0x2009]
#define continue_response3 	XBYTE[0x200a]
#define continue_response4 	XBYTE[0x200b]
#define continue_response5 	XBYTE[0x200c]
#define continue_response6 	XBYTE[0x200d]

#define crc_result 	XBYTE[0x5000]
#define crc_lenth 	XBYTE[0x5001]
#define crc_in1		XBYTE[0x5002]
#define crc_in2 	XBYTE[0x5003]
#define crc_in3 	XBYTE[0x5004]
#define crc_in4 	XBYTE[0x5005]

// EEPROM 控制引脚
sbit scl=P0^7;
sbit sda=P0^6;

sbit	sel_txian0	=P1^0;
sbit	sel_txian1	=P1^1;
//sbit	testpin		=P1 ^ 2;

sbit pll_clk	= P1 ^ 4;
sbit pll_sda	= P1 ^ 5;
sbit pll_det	= P1 ^ 6;
sbit pll_en		= P1 ^ 7;

sbit	send_en		=P2^0;
sbit 	send_start	=P2^1;
sbit 	overcheck	=P2^2;
sbit beep		= P2^3;       // 蜂鸣器
sbit data1		= P2^4;       // wiegand口
sbit data0		= P2^5; 
sbit	comtx		=P2^6;

/*FPGA CONFIGURATE PIN*/
sbit	FPGA_RESET	=P2 ^ 7;
sbit   	FPGA_PROG	=P3 ^ 0;
sbit   	FPGA_DONE	=P3 ^ 1;
sbit   	FPGA_CCLK	=P3 ^ 2;
sbit   	FPGA_DIN	=P3 ^ 3;
sbit	rs485ctl	=P3 ^ 4;
sbit	da_swr0		=P3^5;
sbit	da_swr1		=P3^6;
sbit	power_on	=P3^7;


unsigned char bdata com=0;
unsigned char bdata tempn=0;
sbit tempn_0=tempn^0;
sbit tempn_1=tempn^1;
sbit tempn_2=tempn^2;
sbit tempn_3=tempn^3;
sbit tempn_4=tempn^4;
sbit tempn_5=tempn^5;
sbit tempn_6=tempn^6;
sbit tempn_7=tempn^7;

sbit com_0=com^0;
sbit com_1=com^1;
sbit com_2=com^2;
sbit com_3=com^3;
sbit com_4=com^4;
sbit com_5=com^5;
sbit com_6=com^6;
sbit com_7=com^7;

unsigned char count_25us=0;         // 25us循环计数器
unsigned char count_100us=0;        // 100us循环计数器
unsigned char count_1ms=0;
unsigned char count_10ms=0;
unsigned char count_300ms=0;

unsigned char ok_300ms=0;
unsigned char ok_30s=0;

//unsigned int delay_100us;			//每100us减一计数
unsigned char delay_100us;			//每100us减一计数
unsigned char delay_1ms;			//延时计数

//接收部分使用变量
unsigned char xdata commun_flag=0;	 //进入通信标志

unsigned char xdata uart_selec=0;    // UART0-RS232口,UART0-RS485口
unsigned char xdata down_ok=0;       // MCU升级程序下载完毕

unsigned char xdata interval_times=20;
unsigned char xdata receiveflag=1;
unsigned char xdata sendflag=0;

unsigned char xdata frame_1byte_ok=0;
unsigned char xdata frame_head_ok=0;
unsigned char xdata check_sum_val=0;
unsigned char xdata length=0;

unsigned char xdata offset=0;
unsigned char xdata combuf[256]={0};

//发送部分使用变量
unsigned char xdata sendcount=0;	//发送的起始数
unsigned char xdata sendbuf[255]={0};

//flash 操作使用变量
unsigned int  xdata count_addr=0;	//flash 地址计
unsigned char xdata flashwritebuf[100];
unsigned char xdata flashreadbuf[100];

//eeprom 操作使用变量
unsigned char xdata eeprom_buf[16]={0};
unsigned char xdata common_buf[16]={0};
unsigned char xdata config_buf[8]={0};

unsigned char xdata eeprom_flag=0;		//写EEPROM操作延时20ms
unsigned char xdata eeprom_delay;	


//读标签使用变量
unsigned char xdata autocon=1;          // 是否跳频工作, 1-跳频, 0-定频
unsigned char xdata response=0;
unsigned char xdata getdataflag=0;
unsigned char xdata response_flag=0;

unsigned char xdata id_code[240]={0};
unsigned char xdata id_count=0;
unsigned char xdata id_get=0;

unsigned char xdata data_code[240]={0};	
unsigned char xdata data_get=0;

unsigned char xdata tagwritebuf[8]={0};
unsigned char xdata read_write[4]={0};
unsigned char xdata start_addr_w=0;

unsigned char xdata tag_buf[16]={0};  // 0~3为UAC码, 10~13为MAC码
unsigned char xdata tagdata_buf[6]={0};

unsigned char xdata get_fre=0;
unsigned char xdata channel_site=0;
unsigned char xdata radom_site=0;
unsigned char xdata pll_b[channel_numble]=
{141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,141,
 142,142,142,142,142,142,142,142,142,142,142,142,142,142,142,142,
 143,143,143,143,143,143,143,143,143,143,143,143,143,143,143,143,
 144,144,144,144,144,144,144,144,144,144,144,144,144,144,144};
unsigned char xdata pll_a[channel_numble]=
{2,  6,  10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62,
 2,  6,  10, 13, 18, 22, 26, 30, 33, 38, 42, 46, 50, 53, 58, 62,
 2,  6,  10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62,
 2,  6,  10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58};
unsigned char xdata channel_table[channel_numble]=
{ 7,34,11,50,23,61,38,21,48, 4,29, 9,54,24,63,41,
 18,43, 2,33,10,56,27,60,36,16,47, 3,35,13,53,22,
 58,40,17,49, 6,32, 8,51,26,59,42,20,46, 1,30,12,
 52,28,62,39,15,44, 5,31,14,55,25,57,37,19,45};
unsigned char xdata radom_table[radom_numble]=
{ 1, 4, 7,10,13,16,19,22,24,28,31,34,37,40,43,46,49,52,55,58,61};


void flash_erase(unsigned char bn_sel,unsigned int addr);
void flash_writebyte(unsigned char bn,unsigned int addr,unsigned char data_w);
void flash_writenbyte(unsigned char bn,unsigned int addr,unsigned char data_number);
void flash_readnbyte(unsigned char bn,unsigned int addr,unsigned char data_number);

void iic_eeprom_read_adress(unsigned char eeprom_address_low,unsigned char eeprom_write_data);
void iic_block_write(unsigned char eeprom_address_low,unsigned char eeprom_write_data);

void send_cmd(uchar cmd_data,uchar scmd_data,uchar cmd_len_data,uchar lastf_len_data);
unsigned char  fast_id_process(unsigned char multi_card);
unsigned char  check_tag_present(void);
unsigned char  read_data_process(unsigned char tag_addr,uchar tag_num);
unsigned char  write_data_process(uchar tag_addr);
unsigned char  write_protect_process(uchar tag_addr);
unsigned char  read_protect_process(uchar tag_addr);

unsigned char  epc_reg=0;


void treat_report(void);    //PC机报文处理

void pll_con(uchar numble);
void con_pll(unsigned long frequence,unsigned char total_bit);


void dtime( void )
{
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
   _nop_();
}



// ==================================================
void dog(void)
{
	WDTCN = 0xA5; 
}

void delay(int x_100us)
{
	delay_100us = x_100us;
	while(delay_100us != 0) {;}
	dog();
}

time0() interrupt 1
{
	TL0=0xf5;
	TH0=0xff;   // 每25us中断一次

	count_25us++;
	if (count_25us<4)
		return;
	count_25us=0;    // 满100us,重新计数

	delay_100us--;

	count_100us++;
	if(count_100us<10)
		return;
	count_100us=0;   // 满1ms,重新计数 

	// 1
	delay_1ms--;

	// 2
	if(offset>1)    
	{    // 20ms以内必须收到一个字符
		interval_times--;
		if(interval_times==0)
			offset=0;
	}

	// 3
	if(eeprom_flag==1)
	{
		eeprom_delay--;
		if(eeprom_delay<=0)
					eeprom_flag=0;
	}

	count_1ms++;
	if(count_1ms<10)
		return;
	count_1ms=0;      // 满10ms,重新计数

	//  通信结束后500ms,熄灭LED
	if(commun_flag>0)	
	{
		commun_flag--;
		if(commun_flag==0)	
			comtx=0;
	}

	count_10ms++;
	if(count_10ms<30)
		return;
	count_10ms=0;     //满300ms,重新计数

	ok_300ms=1;

	count_300ms++;
	if(count_300ms<100)
		return;
	count_300ms=0;    //满30s,重新计数

	ok_30s=1;
}

/**********外部中断0服务程序*************/
exint0() interrupt 0
{
	unsigned char xdata i=0;

	switch(getdataflag)
	{
	case 1:
		response_flag=1;
		response = response_addr & 0x01;	
		break;
	case 2:
		response_flag=1;
		for(i=0;i<6;i++)
		   tagdata_buf[i]=XBYTE[0x2008+i];
		break;
	case 3:
		response_flag=1;
		response = response_addr;
		break;
	}
}

void endall(void)
{
		offset=0;
        frame_1byte_ok=0;
        frame_head_ok=0;
		dog();
}

/*************串口0中断服务程序***************/
serial() interrupt 4 
{
	bit  check_bit=0;

	uart_selec=0;

	comtx=1;
	commun_flag=50;
	
	SFRPAGE = 0x00;
    TI0=0;
   	RI0=0;

	dog();	

	if(receiveflag)
	{
		com=SBUF0;
		if(frame_head_ok)
		{
			interval_times=20;	
			offset++;
			combuf[offset]=com;
			check_sum_val += com;
			length--;
            if(length==0)
            {
                if(check_sum_val==0)
                     treat_report();
                endall();
			}
		}
		else if(frame_1byte_ok)
		{	
			if(com != 0x40)
			{
				offset=1;
				combuf[1]=com;
	            length=com;
				check_sum_val += com;

				check_bit=0;
				check_bit=check_bit^com_0;
				check_bit=check_bit^com_1;
				check_bit=check_bit^com_2;
				check_bit=check_bit^com_3;
				check_bit=check_bit^com_4;
				check_bit=check_bit^com_5;
				check_bit=check_bit^com_6;
				check_bit=check_bit^com_7;
				if(check_bit)
				{	
					length = length & 0x7f;
					frame_head_ok=1;               
				}
				else
					endall();
			}
		}
		else if(offset==0)
        {
			if(com==0x40)
			{	
				combuf[0]=com;
				check_sum_val=com;
				frame_1byte_ok=1;
			}
		}
	}
	else if(sendflag)
	{
		if(sendcount<((sendbuf[1]&0x7f)+2))
		{
			SBUF0 = sendbuf[sendcount];
			sendcount++;
		}
		else
		{
			sendflag=0;
			receiveflag=1;
		}
	}
}

/*************串口1中断服务程序***************/
serial1() interrupt 20
{	
	bit  check_bit=0;

	uart_selec=1;

	comtx=1;
	commun_flag=50;

	SFRPAGE = 0x01;
    TI1=0;
   	RI1=0;
	SFRPAGE = 0x00;

	dog();	

	if(receiveflag)
	{		
		SFRPAGE = 0x01;
		com=SBUF1;
		SFRPAGE = 0x00;
		
		if(frame_head_ok)
		{
			interval_times=20;	
			offset++;

⌨️ 快捷键说明

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