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

📄 串行口.txt

📁 串口程序
💻 TXT
字号:

#include <REG52.H>                /* special function register declarations   */
#include "def.h"
#include "public.h"
                                 /* for the intended 8051 derivative         */
void Uart0Init()
{
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 0xfd;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
	ES=1;
	EA=1;
//	SCON=0x50;
//	IE=0X92;
//   TI    = 1;                  /* TI:   set TI to send first char of UART    */

}

void putchar (uchar ch)  {                  /* Write character to Serial Port    */

  if (ch == LF)  {
    SBUF = CR;                          /* output CR */
    while (!(TI));
	TI=0;
  }
  SBUF = ch;
  while (!(TI));
  TI=0;
}



void comm (void) interrupt 4 using 1
{   							// Int Vector at 000BH, Reg Bank 1
uchar temp;
uchar c;
	if (RI)
	{
		RI=0;
		c=SBUF;
		if (gTimeDisplay==FALSE)
		{
			if ((gCommandDeal==FALSE)&&(gUart0RecCount<(MAXCOMMLENGTH-1)))
			{
		    	if (c  == CR)
				{
		    		c = LF;    /* read character                 */
					gCommandDeal=TRUE;
				}
		
			    if (c == BACKSPACE  ||  c == DEL)  {    /* process backspace              */
			      if (gUart0RecCount != 0)  {
			        gUart0RecCount--;                              /* decrement count                */
			        putchar (BACKSPACE);                /* echo backspace                 */
			        putchar (' ');
			        putchar (BACKSPACE);
			      }
			    }
			    else if (c != CNTLQ && c != CNTLS)  {   /* ignore Control S/Q             */
					gUart0Buf[gUart0RecCount]=c;
					gUart0RecCount++;
					putchar (c);                  /* echo and store character       */
					
			    }
			}
			else
			{
				temp=SBUF;
			}
			if (c== LF)
			{
				gUart0RecCount--;
				gUart0Buf[gUart0RecCount]=0;
				gCommandDeal=TRUE;
			}
			if (gUart0RecCount>=(MAXCOMMLENGTH-2))
			{
				gUart0RecCount--;
				gUart0Buf[gUart0RecCount]=0;
				gCommandDeal=TRUE;

			}
		}
		else
		{
			if (c==ESC)
			{
			    SBUF = CR;                          /* output CR */
			    while (!(TI));
				TI=0;
			    SBUF = LF;                          /* output CR */
			    while (!(TI));
				TI=0;
				gTimeDisplay=FALSE;
			}
		}
	}
}

void Uart0SendStr(uchar * sp)
{
	do
	{
		if (*sp!='\n')
		{
			SBUF=*sp;
			sp++;
			while(!TI);
			TI=0;
		}
		else
		{
			SBUF=0x0d;
			sp++;
			while(!TI);
			TI=0;
			SBUF=0x0a;
			while(!TI);
			TI=0;

		}
	}while(*sp!='\0');
}



⌨️ 快捷键说明

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