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

📄 uart.c.bak

📁 mifarea卡程序mifarea卡程序mifarea卡程序
💻 BAK
字号:
//////////////////////////////////////////////
//	uart.c									//
//	设置波特率(需要晶振频率支持)			//
//  晶振频率:11m0592,12m0,18m0,24m0		//
//  design by: 刘俐训                       //
//////////////////////////////////////////////

#include<reg52.h>
#include"uart.h"
#include"stimer.h"
#include"ascii.h"
#include <v51rx2.h>
#include "intrsc.h"

#ifndef BUF_SIZE
#define BUF_SIZE 	32
#define OBUF_SIZE 	16
#endif

#ifndef enable
#define enable()   EA=1
#define disable()  EA=0
#endif

#define StartSend() TI=1

static bit SbufWorking=0;

static unsigned char idata InputBuf[BUF_SIZE];
static unsigned char xdata OutputBuf[OBUF_SIZE];
static unsigned int InputOutTime=0;
static unsigned int OutputOutTime=0;

#if (BUF_SIZE>256)
static unsigned int data IBufGetIdx=0;
static unsigned int data IBufPutIdx=0;
#else
static unsigned char data IBufGetIdx=0;
static unsigned char data IBufPutIdx=0;
#endif
#if (OBUF_SIZE>256)
static unsigned int data OBufGetIdx=0;
static unsigned int data OBufPutIdx=0;
#else
static unsigned char data OBufGetIdx=0;
static unsigned char data OBufPutIdx=0;
#endif

static bit PutInputData( void );
static int GetInputData( void );
static bit PutOutputData( unsigned char abyte );
static bit GetOutputData( void );
#define EnterCrtcl()  // ES = 0
#define LeaveCrtcl()  // ES = 1

void InterruptCom(void) interrupt SIO_VECTOR using 1
{
	if(RI)
	{
		RI=0;
		PutInputData();
	}
	
	if(TI)
	{
		TI=0;
		GetOutputData();
	}
}

bit PutInputData( void )
{
	#if (BUF_SIZE!=256)
   		if( IBufGetIdx ==0 )
   		{
   			if( IBufPutIdx == BUF_SIZE-1 )
   			    return 0;
   		}
	#endif
   	
   	if(IBufPutIdx == IBufGetIdx-1)
   	{
   		return 0;
   	}
	InputBuf[IBufPutIdx] = SBUF;
	IBufPutIdx++;
	
	#if (BUF_SIZE != 256)
   		if(IBufPutIdx==BUF_SIZE)
   		    IBufPutIdx=0;
	#endif
	
   	return 1;
}

int GetInputData( void )
{
	unsigned char tmp; 
	
    EnterCrtcl();
	if( IBufGetIdx == IBufPutIdx )
	{
	    LeaveCrtcl();
		return -1;
	}

	tmp = InputBuf[IBufGetIdx++];

	#if (BUF_SIZE != 256)
	  	if(IBufGetIdx==BUF_SIZE)
			IBufGetIdx=0;
	#endif
	
	LeaveCrtcl();
	return tmp;
}

bit GetOutputData( void )
{
   	if( OBufPutIdx == OBufGetIdx )
   	{
   		SbufWorking=0;
   		return 0;
   	}

   	SBUF = OutputBuf[OBufGetIdx];
   	OBufGetIdx++;
	#if (OBUF_SIZE != 256)
	  	if(OBufGetIdx==OBUF_SIZE)
	  	    OBufGetIdx=0;
	#endif
    return 1;
}

bit PutOutputData( unsigned char abyte )
{
   	EnterCrtcl();
	#if (OBUF_SIZE!=256)
   		if( OBufGetIdx ==0 )
   			if( OBufPutIdx == OBUF_SIZE-1 )
   			{
   			    LeaveCrtcl();
   			    return 0;
   			}
	#endif
   	
   	if( OBufPutIdx == OBufGetIdx-1 )
   	{
   	    LeaveCrtcl();
   	    return 0;
   	}
	
   	OutputBuf[OBufPutIdx++] = abyte;
	
	#if (OBUF_SIZE != 256)
   		if(OBufPutIdx==OBUF_SIZE)
   		    OBufPutIdx=0;
	#endif

	if(!SbufWorking)
	{
		SbufWorking = 1;
		StartSend();
	}
	LeaveCrtcl();
  	
   	return 1;
}

void ComOpen(unsigned long clk, unsigned long baudrate)
{	
	unsigned char tmp;
	
	if(baudrate >= 9000)
	{
	    PCON |= SMOD_;
	    tmp = (clk >> 4) / baudrate;
	}
	else
	{
	    PCON &= ~SMOD_;
	    tmp = (clk >> 5) / baudrate;
	}
	    
    SetIntPri(SIO_VECTOR, 1);
    
	TI = RI = 0;
	SCON = 	0x50;
  	TMOD &= 0x0F;
  	TMOD |=	0x20;
  	TR1 = 1;
  	TH1 = 256 - tmp;
  	enable();
  	ES=1;
}

unsigned int ComWrite( unsigned char *buf, unsigned int len )
{
	unsigned int i;
	unsigned int starttime;
	
	for(i=0; i<len; i++)
	{
    	starttime = GetTickCount();
		while( !PutOutputData(buf[i]) )
		{
			if(OutputOutTime==0 || (GetTickCount() - starttime >= OutputOutTime))
			    return i;
		}
	}
	return i;
}

unsigned int ComRead( unsigned char *buf, unsigned int len )
{
	unsigned int i;
	unsigned int starttime;
	int tmp;

	for(i=0; i<len; i++)
	{
    	starttime = GetTickCount();
		while((tmp = GetInputData()) < 0)
		{
			if(InputOutTime==0 || (GetTickCount() - starttime >= InputOutTime))
				return i;
		}
		buf[i] = (unsigned char)tmp;
	}
	return i;
}

void ComSetTimeOut(unsigned int iouttime, unsigned int oouttime)
{
	InputOutTime=iouttime;
	OutputOutTime=oouttime;
}

unsigned int ComIBufBytesTell( void )
{
	unsigned int ii;

	if(IBufPutIdx>=IBufGetIdx)
	{
		ii=(unsigned int)(IBufPutIdx-IBufGetIdx);
	}
	else
	{
		ii=(unsigned int)(BUF_SIZE-IBufGetIdx+IBufPutIdx);
	}
	return ii;
}

void ComClear( unsigned char flag )
{
	if( flag & 0x01 ) IBufGetIdx = IBufPutIdx;
	if( flag & 0x02 ) OBufGetIdx = OBufPutIdx;
}

⌨️ 快捷键说明

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