MCU Serial Communication - Character Array

Publisher:TranquilMindLatest update time:2012-11-06 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
As can be seen in the previous article, using characters one by one to judge commands and communication protocols is not only troublesome, but also has great defects. For example, if a string is entered, it will be executed as long as there is a command, and the program in the previous article cannot handle some special strings (the stop command is end, but if an en is entered, it will be stuck in the while (RI==0) statement. There is no input of the next character. RI is set to 0 in the above program, and it will not come out of the while statement. If it is set to 1 at the beginning, the next character cannot be received)

So I started to think about using character arrays to process commands again. Before using the last program, I actually used character arrays to do this program, but there were always some problems that were not solved; yesterday I used C++ to make a character receiving and reading function, and then used Keil to make the same program and found that I still used some of the same:

There are two forms of defining character arrays in C language and C++:

For example, (1) char t[6]="start"; the length of this string is 5, but the system adds a \0 at the end of the string by default; so a character array with a length of 6 bits is required to store it. If 5 bits are defined (t[5]), the compiler will report an error saying that the array cannot be stored;

(2) char a[5]={'s','t','a','r','t'} This definition can omit the system default \0, but it can be clearly seen that this definition method is not practical for defining long strings;

Define the array in keil:

The second method above can also be defined, but the first method will be used differently:

Define a start array: char t[5]="start"; Obviously, the compiler in Keil does not give a default \0 to the character array. This is the difference between the two compilers.

In some cases, this difference can be fatal:

#include
#include
#include
#define uchar unsigned char
uchar t[5]="start";
int i;uchar a[5];
void main()
{
TMOD=0x20;
TH1=0xfd;TL1=0xfd;
SM1=1;SM0=0;
REN=1;ET1=1;TR1=1;ES=1;
while(1)
{
for(i=0;i<5;) //Input into the array
{RI=0;
while(RI==0); //If the length of the string defined here is 6 bits, it will keep waiting for the sixth input, causing the program to fail to execute
a[i]=SBUF;i++;
}
for(i=0;i<5;) //Output to the computer through the microcontroller
{
SBUF=a[i];
while(TI==0);i++;TI=0;
}
}
}

Keywords:MCU Reference address:MCU Serial Communication - Character Array

Previous article:Single chip microcomputer output matrix wave experiment
Next article:STC12C4052 PCA for 16-bit timer test program

Recommended ReadingLatest update time:2024-11-16 15:26

51 MCU special function register and bit definition
In the next few sections, we will show you how to write your first MCU program. Before that, let's first learn about some of the 51 MCU's unique programming syntax and the basic operating steps of Keil software. Please be patient. We mainly use C language to program MCUs, and some MCUs have a few very special and uni
[Microcontroller]
51 MCU special function register and bit definition
51 MCU Learning Road (IV) - Timer
Preface: The following procedures and circuit diagrams are not guaranteed to be the best solutions, and there will be many deficiencies. Please understand. In addition, the content of C51 is a summary of the knowledge learned a long time ago, and there may be errors. Please correct me. text Learning objectives: Lear
[Microcontroller]
51 MCU Learning Road (IV) - Timer
Study Notes - 51 MCU Special Function Registers
I. Introduction In the process of learning 51 single-chip microcomputer, we are actually learning how to use its special function registers (some registers are invisible to us). Special function registers are registers corresponding to each functional component in 80C51 single-chip microcomputer, which are used to sto
[Microcontroller]
Study Notes - 51 MCU Special Function Registers
Briefly describe AVR microcontroller interrupts
About AVR interrupts: When the system is running the main program normally, if there is suddenly an important task to be processed immediately, the system will save the current work and then process this task. After completing this important task, it will return to the original main program to continue running. This
[Microcontroller]
Briefly describe AVR microcontroller interrupts
MCU DS18b20 Temperature detection LCD display
/*-----------------------------------------------     Name: DS18b20 temperature detection LCD display     Forum: www.doflye.net     Written by: shifang     Date: 2009.5     Modification: None     content:   ------------------------------------------------*/   #include reg52.h //Include the header file. Generally, no m
[Microcontroller]
How to use ADC conversion built into HOLTEK microcontroller
Recently, some friends have asked about the usage of HOLTEK microcontroller ADC conversion. Here is an introduction! Take HT46RU232 as an example: First, take a look at its conversion timing diagram: The following are the steps for ADC conversion: ADC data conversion calculation: The data format inside the
[Microcontroller]
Interpretation of ATmega2560 UAV joystick microcontroller design solution
  As drones are becoming a new economic growth point and the national income level is rising, they have received more attention from colleges and universities and the public in recent years. UAV is the abbreviation of unmanned aircraft. It is a reusable unmanned aircraft that uses radio remote control (including remote
[Microcontroller]
Interpretation of ATmega2560 UAV joystick microcontroller design solution
SEPTNY256 single chip microcomputer switching power supply and its application
1 Performance characteristics of TNY256   ·Built-in automatic restart circuit, no external components required. Once output short circuit or control loop open circuit failure occurs, the duty cycle can be reduced to protect the chip.   ·In the input DC high-voltage circuit, there is no need to use a clamp protectio
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号