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

📄 serial.c

📁 用MCS51 单片机的TCIP协议的测试,很基本的程序,对新手可能有帮助!
💻 C
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename      : serial.c
* Programmer(s) : 
* Version       : 1.0 
* Created       : 2002/04/10
* Modified      :
* Description   : 
********************************************************************************
*/

#include "config.h"
#include "serial.h"		

extern u_char S_datasize;
extern u_char S_Parity;
extern bit TI_FLAG;

/*
********************************************************************************
* Description : Initialize serial interrupt
* Arguments   : None
* Returns     : None
* Note        : 
********************************************************************************
*/
void Init_SerialInt(void)
{
	IPL0 = 0x10; // for Atmel
	IPH0 = 0x10; // for Atmel
	
	TI_FLAG = 1;
	ES   = 1;	 	// Interrupt Enable
	RI = 0;
	TI = 0;
}

/*
********************************************************************************
* Description : Calculate serial data and return even or odd parity 
* Arguments   : sdata - serial data
* Returns     : parity value
* Note        : 
********************************************************************************
*/
u_char Parity(u_char sdata)
{
	u_char i, cnt = 0;
	u_char comp;
	
	if (S_datasize == 8) comp = 0x80;
	else if (S_datasize == 7) comp = 0x40;
	
	for(i = 0; i <= (S_datasize-1); i++) {
		if ((sdata<<i) & comp) cnt++;
	}
	
	if (S_Parity == 2) { // even
		if ((cnt & 0x01) == 0x01) return(1);
		else return(0);
	}
	else if (S_Parity == 1) { // odd
		if ((cnt & 0x01) == 0x00) return(1);
		else return(0);
	}
}

/*
********************************************************************************
* Description : Send a character over the serial link. 
* Arguments   : byData - character to output
* Returns     : None
* Note        : 
********************************************************************************
*/
void PutByte(u_char byData)
{       
	unsigned char temp = 0;
	
	if (S_Parity != 0) {
		temp = Parity(byData);
		if ( S_datasize == 7 ) {		// 7-O-1, 7-E-1 
			if (temp) byData = (byData | 0x80);
		}
		else if (S_datasize == 8) {		// 8-O-1, 8-E-1
			if (temp) TB8 = 1;
			else TB8 = 0;
		}
	}
	while(!TI_FLAG);
	SBUF = byData;	
	TI_FLAG = 0;
}


/*
********************************************************************************
* Description : Send characters to serial routine
* Arguments   : Str - Character Stream pointer
* Returns     : None
* Note        : 
********************************************************************************
*/
void PutString(char *Str)
{
	UCHAR i;
	
	for (i = 0; Str[i] != '\0'; i++) {
		PutByte(Str[i]);
	}
}

void PutStringLn(char * Str)
{
    PutString(Str);
	PutByte(0x0a);
	PutByte(0x0d);
}

/*
********************************************************************************
* Description : ASCII Conversion routine
* Arguments   : byData - 1 byte
* Returns     : 
* Note        : 
********************************************************************************
*/
void PutHTOA(u_char byData)
{
	if((byData / 0x10) >= 10)
		PutByte('A'+((byData/0x10)%0x0A));
	else
		PutByte('0'+((byData/0x10)%0x0A));
	if((byData % 0x10) >= 10)
		PutByte('A' + ((byData%0x10)%0x0A));
	else
		PutByte('0' + ((byData%0x10)%0x0A));
}

void PutITOA(u_int byData)
{
	PutHTOA(byData / 0x100);
	PutHTOA(byData % 0x100);
}

void PutLTOA(u_long byData)
{
	PutITOA(byData / 0x10000);
	PutITOA(byData % 0x10000);
}


/*
********************************************************************************
* Description : Convert Hex(0-F) to ASCII data format
* Arguments   : 
* Returns     : 
* Note        : 
********************************************************************************
*/
UCHAR D2C(char c)
{
	if( c >= 0 && c <= 9)
		return '0' + c;
	if( c >= 10 && c <= 15)
		return 'A' + c - 10;
	return c;	
}

/*
********************************************************************************
* Description :  
* Arguments   : 
* Returns     : 
* Note        : 
********************************************************************************
*/
void inet_ntoa(u_char* addr, char* addr_str)
{
	char i ;
	int d;

	for(i = 0; i < 4; i++)
	{
		d = *(addr+i);
		d = d & 0x00FF;
		
		*addr_str++ = D2C(d/100);		
		*addr_str++ = D2C(( d / 10 )%10);
		*addr_str++ = D2C(D2C(d%10));		 
		*addr_str++ = '.';
	}
	*(--addr_str) = 0;
}

⌨️ 快捷键说明

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