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

📄 try.c

📁 crc
💻 C
字号:

#include "oaks_sfr.h"

#include "rs232.h"
#include "interrupt.h"
#include "DataTrans.h"
#include "test.h"
#include "lcd.h"
#include "rs/rs.h"

#include <stdlib.h>


void main(void);
void Rx();
void Tx();

extern unsigned char g_Button1;
extern g_TimerCount;
extern g_PrvType;

const char OPENING1[]=		"-CRC & RS In ES-";
const char OPENING2[]=		"    Welcome!    ";

const char CHOOSE1[]=		" Choose T or R: ";
const char CHOOSET[]=		"      TRANS     ";
const char CHOOSER[]=		"      RECV      ";


const char CODINGTYPE1[]=	"Coding Method:  ";
const char CODINGTYPE2[][]={"  CRC16 Coding  ",
							"  CRC32 Coding  ",
							"   RS (128,4)   ",
							"   RS (128,8)   ",
							"   RS (128,10)  "};
const char CODINGLEN1[]=	"Data Length:    ";
const char DATALENGTH[][]={	" 128 Byte Frame ",
							" 256 Byte Frame ",
							" 512 Byte Frame ",
							"1024 Byte Frame "};
const char ERRORCHANCE[]=	"Error Chance:   ";
const char SENDCOUNT1[]=	"Send Times:     ";

const char BEGIN1[]	   =	" PRESS TO START ";
const char BEGIN2[]	   =	" PRESS TO WAIT  ";

const char SEND1[]	=		"Sending Frame:  ";
const char RECV1[]  =		"Receiving Frame:";

const char RESEND1[]=		"ERROR! RESENDING";
const char RERECV1[]=		"  ERROR! RETRY  ";

const char FINISH1[]=		"     FINISH!    ";

const char ERRCOUNT1[]=		"Error Amount:   ";

const char TIMECOUNT1[]=	"Time Amount:    ";


#define RECV		'R'
#define TRANS		'T'


void main()
{
	int x;
	unsigned char RT;		
//	unsigned char 
	
	
	p7 = 0xff;				/* output "High" to Port P0 			*/
	pd7 = 0xff;				/* output "High" to Port P0 direction register 	*/
	pd3 = 0x00;

	
	
	Rs232Init();
	Int0Init();
	Int1Init();
	_asm( "\tFSET	I");
	LcdInit();

	DispStr(OPENING1,16,1);
	DispStr(OPENING2,16,2);
	WaitButton0();

	DispStr(CHOOSE1,16,1);
	g_Button1=0;
	p7=0xff;
	while(!g_Button1)
	{
		if(RT==RECV)
		{
			RT=TRANS;
			DispStr(CHOOSET,16,2);
		}
		else
		{
			RT=RECV;
			DispStr(CHOOSER,16,2);
		}
		WaitButton0();
		
	}
	
	
		srand(p3);
	if(RT==RECV)
	{
		p7=p3;
		while(1)
			Rx();
	}
	else
	{
		p7=p3;
		Tx();
	}

	
}




void Rx()
{
	
	unsigned char *recvframe;
	int result,recvlen;
	unsigned char SendTimes,SendCount,ErrorNum;


	init_galois_tables();
	DispStr(SENDCOUNT1,16,1);
	g_Button1=0;
	SendTimes=0;
	while(!g_Button1)
	{
		DispInt(SendTimes);
		if(SendTimes!=p3)
		{
			SendTimes=p3;
			DispInt(SendTimes);
			p7=SendTimes;
		}
		
	}
	
	DispStr(BEGIN1,16,1);
	WaitButton0();
	
	Timer0Init();
	SendCount=0;
	result=0;
	ErrorNum=0;
	g_PrvType=0;

	while(1)
	{	
	
		if(result>=0)
		{

			DispStr(RECV1,16,1);
			DispInt(SendCount);
			//p7=SendCount;
			ReqNewFrame();

		}
		else
		{
			DispStr(RERECV1,16,1);
			ErrorNum+=result*(-1);
			ReqPrvFrame();
		}	
		if(SendCount==SendTimes)
			break;
		
		recvlen=RecvFrame(&recvframe);
		result=TestFrame(recvframe,recvlen);
		if(result>=0)
		{
			SendCount++;

			//p7=0x00;
		}
		else
		{
			//p7=0xff;
			
		}


	}
	Timer0Close();
	DispStr(FINISH1,16,1);
	WaitButton0();
	
	DispStr(ERRCOUNT1,16,1);
	DispInt(ErrorNum);
	WaitButton0();

	DispStr(TIMECOUNT1,16,1);
	DispInt(g_TimerCount);
	WaitButton0();



}

void Tx()
{
	
	unsigned char *recvframe;
	unsigned char CodingType;
	unsigned char DataLen,ErrPro;
	int length,len,recvlen,SendCount,RandNum,ErrorNum;

	DispStr(CODINGTYPE1,16,1);
	CodingType=CRC16_CODING;
	g_Button1=0;
	p7=0xff;
	while(!g_Button1)
	{	
		CodingType++;
		if(CodingType>RS_128_10)
			CodingType=CRC16_CODING;
		DispStr(CODINGTYPE2[CodingType],16,2);		
		WaitButton0();
		
	}
	switch(CodingType)
	{
	case RS_128_10:
		initialize_ecc(10);
		break;
	case RS_128_8:
		initialize_ecc(8);
		break;
	case RS_128_4:
		initialize_ecc(4);
		break;
	}
	
	DispStr(CODINGLEN1,16,1);
	DataLen=LEN128;
	g_Button1=0;
	p7=0xff;
	while(!g_Button1)
	{
		DataLen++;
		if(DataLen>LEN1024)
			DataLen=LEN128;
		DispStr(DATALENGTH[DataLen],16,2);
		WaitButton0();
		
	}

	switch(DataLen)
	{
	case LEN128:
		length=128;
		break;
	case LEN256:
		length=256;
		break;
	case LEN512:
		length=512;
		break;
	case LEN1024:
		length=1024;
		break;
	}

	ErrPro=0;
	g_Button1=0;
	p7=0xff;
	while(!g_Button1)
	{
		DispInt(ErrPro);
		if(ErrPro!=p3)
		{
			ErrPro=p3;
			DispInt(ErrPro);
			p7=ErrPro;
		}		
	}

	
	DispStr(BEGIN2,16,1);
	SendCount=0;
	ErrorNum=0;
	while(1)
	{
		recvlen=RecvFrame(&recvframe);
		switch(*recvframe)
		{
		case REQNEW_SIGN:
			DispStr(SEND1,16,1);
			DispInt(SendCount++);
			
			len=DataCreate(&recvframe,length);

			len=CreateFrame(recvframe,len,CodingType);
			
			RandNum=rand();
			RandNum=RandNum%100;
			if(RandNum<ErrPro)
				ErrorNum+=ErrorCreate(recvframe,len);
			

			SendFrame(recvframe,len);
			break;
		case REQPRV_SIGN:
			DispStr(RESEND1,16,1);
			DispInt(SendCount-1);
			len=DataCreate(&recvframe,length);

			len=CreateFrame(recvframe,len,CodingType);
			RandNum=rand();
			RandNum=RandNum%100;
			if(RandNum<ErrPro)
				ErrorNum+=ErrorCreate(recvframe,len);

			SendFrame(recvframe,len);
			break;


		}
	}

	
}

⌨️ 快捷键说明

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