Single-chip serial port communication: the code for receiving multiple bits of data from the serial port and saving them to the array, and sending multiple bits of data to the serial port

In the serial communication of the single chip microcomputer, the code for receiving multiple bits of data to the array and sending multiple bits of data

// 下面的代码用于:单片机的串口何上位机的串口进行通信,上位机发送的数据是“abcd”就开灯,如果是“abde”就关灯。
/**************************************************************************************
*		              串口通信实验												  *
实现现象:下载程序后打开串口调试助手,将波特率设置为9600,选择发送的数据就可以显示在串口助手上。																			  
***************************************************************************************/

#include "reg52.h"			 //此文件中定义了单片机的一些特殊功能寄存器
#include "string.h"			//用与字符串比较

sbit led=P2^0;		//定义灯对应的引脚

typedef unsigned int u16;	  //对数据类型进行声明定义
typedef unsigned char u8;

u8 receiveData[4];			//全局变量,用于接收串口数据
u8 usart_receive_flag=0;	//全局变量,用于判断4位数据是否接收完
u16 i=0;					//接收数据的序号自增

/*******************************************************************************
* 函数名         :UsartInit()
* 函数功能		   :设置串口
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/
void UsartInit()
{
    
    
	SCON=0X50;			//设置为工作方式1
	TMOD=0X20;			//设置计数器工作方式2
	//PCON=0X80;			//波特率加倍
	TH1=0xfd;				//计数器初始值设置,注意波特率是9600的
	TL1=0xfd;
	ES=1;						//打开接收中断
	EA=1;						//打开总中断
	TR1=1;					//打开计数器
}

//串口发送字符
void SendChar(char Char)
{
    
    
    SBUF=Char;
		while(!TI);
		TI=0;
}

//串口发送字符串
void SendString(char *p)
{
    
    
    while(*p!='\0')
    {
    
    
			SendChar(*p);
			p++;
    }
}

/*******************************************************************************
* 函 数 名       : main
* 函数功能		 : 主函数
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
void main()
{
    
    	
	UsartInit();  //	串口初始化
	while(1)
	{
    
    
		
		/*
		if(usart_receive_flag==1&&receiveData[0]=='a'&&receiveData[1]=='b'&&receiveData[2]=='c'&&receiveData[3]=='d')
		{
			led=0;
			//strcpy(receiveData,"\0");
			SendString("abcd have received!---");
			usart_receive_flag=0;
		}
		else if(usart_receive_flag==1&&receiveData[0]=='a'&&receiveData[1]=='b'&&receiveData[2]=='c'&&receiveData[3]=='e')
		{
			led=1;
			//strcpy(receiveData,"\0");
			SendString("abce have received!---");	
			usart_receive_flag=0;
		*/
		
		if(usart_receive_flag==1&&(strcmp("abcd",receiveData)==0))			//如果接收完数据,并且比较数组符合
		{
    
    
			led=0;					//开灯
			//strcpy(receiveData,"\0");
			SendString("abcd have received!---\n");			//提示对方串口收到数据
			usart_receive_flag=0;												//将数据接收完4位的标志位清0,用于下次判断是否接收完4位
			receiveData[0]='\0';												//我这里对数据清空了
		}
		else if(usart_receive_flag==1&&(strcmp("abce",receiveData)==0))
		{
    
    
			led=1;					//关灯
			//strcpy(receiveData,"\0");
			SendString("abce have received!---\n");	
			usart_receive_flag=0;
			receiveData[0]='\0';
		}
	}		
}

/*******************************************************************************
* 函数名         : Usart() interrupt 4
* 函数功能		  : 串口通信中断函数
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/
void Usart() interrupt 4
{
    
    

		if(RI)										//如果是串口接收到一帧数据,就会产生中断,RI标志变为1
		{
    
    
			RI = 0;									//手动将标志置0,方便下次判断
			receiveData[i++]=SBUF;	//出去接收到的数据
							
			if(i==4)								//4位数据接收完
			{
    
    
				i=0;
				usart_receive_flag=1;		//标志位用于,只有当4位数据接收完,主函数才进行数据比较
			}
		}
}

Guess you like

Origin blog.csdn.net/qq_41873236/article/details/115297933