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;
}
}
}
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
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Is this the Keil5 that STM32 needs?
- Upload two photosensitive detection circuits. The target detects different voltages according to different light exposure. Which one is correct?
- MSP430F149, HC_SR04 distance measurement
- MM32F103 BUG reminder to avoid pitfalls
- Will MakeCode support Python?
- What are the specific differences between triodes and MOS tubes?
- How to set the GND pin of a separate component as a flower pad in PADS?
- EEWORLD University Hall----Keithley 4200A-SCS parameter analyzer accelerates semiconductor equipment, materials and process development
- EEWORLD University Hall ---- Advanced C Language Programming for Embedded Systems (Ling Ming, Southeast University)
- Two-phase brushless DC motor speed control system based on DSP and CPLD