51 MCU serial port receiving and sending string source program

Publisher:明理厚德Latest update time:2020-08-08 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I want to control the LED through the serial port, but the serial port can only send one character each time it is interrupted. So it is not easy to know which part each command controls when controlling it. So I refer to the information on the Internet and write a simple string receiving and sending function. The function of this program is: the computer serial port assistant sends data to the microcontroller, and the microcontroller returns it to the computer after receiving it. When sending, you need to leave two spaces and then enter the content you want to send, otherwise the transmitted data will be lost. Because this situation does not affect the serial port control of the LED, I don't care about it.


The microcontroller source program is as follows:

#include

typedef unsigned char uchar;

typedef unsigned int uint;

sbit led=P2^0;

void Usart_Init()

{

TMOD=0X20;

PCON=0X80;

SCON=0X50;

TH1=0XF3;

TL1=0XF3;

TR1=1;

EN=1;

EA=1;

}



float strk[50];


void delay(unsigned int i){unsigned int j;for(i;i>0;i--)for(j=0;j<120;j++);}//延时函数

void sendbyte(unsigned char byt){SBUF=byt;while(!TI);TI=0;} //Serial port character sending function


void receivebyte() /*Serial port receiving function*/

{

uchar shiftdata; //Transfer data variable, store the data line received by SBUF to this place, and then transfer it to the array

static uint i=0;

                               

if(RI){

RI=0;shiftdata=SBUF;strk[i]=shiftdata; i++;

if(shiftdata==''||shiftdata=='n'){i=0;}

      }                   

}


/*The following is the serial port string sending function*/

void sendstring(unsigned char *string){       

uint k;

while(*string!='')

{

sendbyte(*string);delay(100);string++;

}for(k=0;k<50;k++)strk[k]=0;

}

                                                                                                                                                                                    


void main()

{

usart_Init();

while(1)

{

if(strk[2]=='o'&&strk[3]=='n')led=0;

if(strk[2]=='o'&&strk[3]=='f'&&strk[4]=='f')led=1;

if(RI){sendstring(strk);}


}

}



void Usart_o() interrupt 4

{


  receivebyte();


       

}

1111.png


Reference address:51 MCU serial port receiving and sending string source program

Previous article:STC15 series MCU RS-485 communication routine (with CRC check)
Next article:Chinese menu MP3 song selector based on SC95F8616 microcontroller (2)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号