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

📄 serial.c

📁 串口服务器KEIL C51原代码,单片机采用STC89C58RD+,网络控制器采用RTL8019,经过三层交换机测试,可以直接应用在产品,串口波特率576
💻 C
字号:
#include <uip\uip.h>
#include "uip\uip.h"
#include "serial.h"
#include <R8051.H>
#include <LCD\LCD.h>

sbit   LED_RED  = P3 ^ 3;
sbit   LED_GRE  = P3 ^ 4; 

UBYTE  Rec_len=0;
UBYTE  Rec_data[100];
UBYTE  dataRec_timecount=0;
bit REC_OK=0;
bit uip_connected=0;    //tcp connected or not;
bit data_Rec=0;         //rec at least 1 byte when in tuip_connected;
//UBYTE  idata test[18];
//***********************串口初始化波特率定义*****原有********************
#define OSC_FREQ               22118400L
#define BAUD_115200             256 - (OSC_FREQ/192L)/115200L
#define BAUD_57600              256 - (OSC_FREQ/192L)/57600L
#define BAUD_38400              256 - (OSC_FREQ/192L)/38400L
#define BAUD_28800              256 - (OSC_FREQ/192L)/28800L
#define BAUD_19200              256 - (OSC_FREQ/192L)/19200L
#define BAUD_14400              256 - (OSC_FREQ/192L)/14400L
#define BAUD_9600               256 - (OSC_FREQ/192L)/9600L
#define BAUD_4800               256 - (OSC_FREQ/192L)/4800L
#define BAUD_2400               256 - (OSC_FREQ/192L)/2400L
#define BAUD_1200               256 - (OSC_FREQ/192L)/1200L
//************************************************************************
void InitSerial_0(void)
{
	///////////////////////////////////////////////////////////////////////////
	//-if you need isd(in system debugger)you need add the list code in your main
	//--------------add for serial bebug s0----------zhzhl-------
	SCON  = 0x50;      /* enable serial uart & receiver  s0,mode 1    */
	PCON = PCON | 0x80;

    TMOD|=0X20; //TIMER1: MODE 2, ;
	TH1=BAUD_57600;//BAUD_19200 ; // BAUD_9600;//BAUD_4800;//247;    //32M-->18938
	TL1=BAUD_57600;//BAUD_19200 ; // BAUD_9600;//BAUD_4800;//247;
	TR1=1;     //TIMER1 START;
	//-----------end of s0 initial ----------------------------
	ES=1;
}
//************************************************************************
void serial(void) interrupt 4
{    
    if(RI)
	{
        RI=0;
        if(Rec_len<100) Rec_data[Rec_len++]=SBUF;
        //SBUF=Rec_data[Rec_len-1];//FOR TEST
        #if UIP_ACTIVE_OPEN
           if(!data_Rec) data_Rec=1;
		     dataRec_timecount=0;
        #else
           if(uip_connected)         
		     {
               if(!data_Rec) data_Rec=1;
		         dataRec_timecount=0;
	       	 }
        #endif
	}
    //if(TI) TI=0;    
}
//************************************************************************
////////////////////////////////////////////////////////////////
//print xdata memory(hex);
//pxdata: start address
//n: byte number
//e.g: Uart0_printR(0x8800,4); will print: 0x** ** ** **;
////////////////////////////////////////////////////////////////
void Uart0_printR(UBYTE *pxdata, UWORD n)
{
     UWORD i;
     ES=0;   //stop uart1 interrupt;
     TI=0;
     for(i=0;i<n;i++)
        {
          //j=(*(pxdata+i));
          TI=0;
          SBUF=*(pxdata+i);
          while(!TI);
        }
     TI=0;
     ES=1;
} 
//************************************************************************
void Uart0_print(char *p)
{
     ES=0;      //disabe uart0 interrupt;
     TI=1;
     while(*p)
	 {
        if(*p==10)
          {
	        TI=0;
            SBUF=0x0d;
            while(!TI);
	      }
        TI=0;
        SBUF=(*p);
        ++p;
        while(!TI);      
	 }
     //while(!TI);
	 TI=0;
     ES=1;
}
//************************************************************************


⌨️ 快捷键说明

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