* Using the return statement is very convenient for subsequent program upgrades and modifications.
**/
void usart_service(void)
{
unsigned char i = 0;
// /*If there is no new data from the serial port after a certain period of time*/
// if(uiSendCnt >= const_receive_time && ucSendLock == 1)
// {
// The original statement is now replaced by two return statements
if(uiSendCnt < const_receive_time) /* If the delay has not exceeded the specified time, exit the program directly without executing any statements after return. */
{
return; /* Forcefully exit this subroutine and do not execute any of the following statements*/
}
if(ucSendLock == 0) /* If it is not the latest serial port data received, exit this program directly without executing any statements after return. */
{
return; /* Forcefully exit this subroutine and do not execute any of the following statements*/
}
/*
* The above two return statements are equivalent to the original if (uiSendCnt>=const_receive_time&&ucSendLock==1) statement.
* After using the return statement, one if nesting is obviously reduced.
*/
ucSendLock = 0; /*Lock after processing once, no need to come in every time unless there is new data to be received*/
/*The following code enters the stage of data protocol parsing and data processing*/
uiRcMoveIndex = 0; /*Since it is to judge the data header, the subscript movement variable starts from 0 of the array and moves to the end*/
// /*
// * To judge the data header, two conditions must be met to enter the loop to parse the data protocol:
// * First: The maximum receive buffer data must be greater than the length of a string of data (here is 5. Including 2 valid data and 3 data headers)
// * Second: The cursor uiRcMoveIndex must be less than or equal to the maximum receive buffer data minus the length of a string of data (here is 5. Including 2 valid data and 3 data headers)
// */
// while(uiRcregTotal >= 5 && uiRcMoveIndex <= (uiRcregTotal - 5))
// {
// The original statement is now replaced by two return statements
while(1) /* The infinite loop can be interrupted by the following return or break statement. The return statement itself already contains the function of the break statement. */
{
if(uiRcregTotal < 5) /* The serial port receives too little data*/
{
uiRcregTotal = 0; /* Clear the buffer index to facilitate receiving new data from index 0 next time*/
return; /* Forcefully exit the while(1) loop nest and directly exit the program without executing any of the following statements*/
}
if(uiRcMoveIndex > (uiRcregTotal - 5)) /* The data in the array buffer has been processed*/
{
uiRcregTotal = 0; /* Clear the buffer index to facilitate receiving new data from index 0 next time*/
return; /* Forcefully exit the while(1) loop nest and directly exit the program without executing any of the following statements*/
}
/*
* The above two return statements are equivalent to the original while(uiRcregTotal>=5&&uiRcMoveIndex<=(uiRcregTotal-5)) statement.
* The usage of the above two return statements also shows that return itself already contains the function of break statement, no matter how many layers of inner loop nesting is currently in.
* can force exit the loop and exit the program directly.
*/
if(ucRcregBuf[uiRcMoveIndex + 0] == 0xeb && ucRcregBuf[uiRcMoveIndex + 1] == 0x00 && ucRcregBuf[uiRcMoveIndex + 2] == 0x55)
{
for(i = 0; i < const_array_size; i ++) /* The original data received from the serial port that needs to be sorted*/
{
ucUsartBuffer[i] = ucRcregBuf[uiRcMoveIndex+3+i];
}
/* The third operation method uses pointers to add an array output interface to the function*/
/* Through the pointer output interface, the result of the sorting operation is directly exported from this output port to the ucGlobalBuffer_3 array*/
big_to_small_sort_3(ucUsartBuffer, ucGlobalBuffer_3); /* ucUsartBuffer is the input array, ucGlobalBuffer_3 is the array that receives the sorting results*/
for(i = 0; i < const_array_size; i++)
{
eusart_send(ucGlobalBuffer_3[i]); /* Return the results sorted by the third method to the host computer for observation*/
}
/* In order to facilitate the host computer to observe, send 3 more bytes ee ee ee as the dividing line between the first method and the second method*/
eusart_send(0xee);
eusart_send(0xee);
eusart_send(0xee);
/* The fourth operation method relies on a pointer as the input and output interface of the function. */
/* Through this pointer input and output interface, the ucGlobalBuffer_4 array is both an input array and an output array. The results of the sorting operation are directly stored in it, similar to the characteristics of a global variable. */
for(i = 0; i < const_array_size; i ++) /* The original data received from the serial port that needs to be sorted*/
{
ucGlobalBuffer_4[i] = ucUsartBuffer[i];
}
big_to_small_sort_4(ucGlobalBuffer_4); /* ucUsartBuffer is the input array, ucGlobalBuffer_3 is the array that receives the sorting results*/
for(i = 0; i < const_array_size; i++)
{
eusart_send(ucGlobalBuffer_4[i]); /* Return the results sorted by the third method to the host computer for observation*/
}
break; /*Exit while(1) loop*/
}
uiRcMoveIndex ++; /*Because it is to judge the data head, the cursor moves towards the end of the array*/
}
// }
uiRcregTotal = 0; /* Clear the buffer index to facilitate receiving new data from index 0 next time*/
// }
}
/**
* @brief Timer 0 interrupt function
* @param None
* @retval None
**/
void ISR_T0(void) interrupt 1
{
TF0 = 0; /* Clear interrupt flag */
TR0 = 0; /*Disable interrupt*/
if(uiSendCnt < const_receive_time) /*If no serial data comes in after this time, it is considered that a string of data has been received*/
{
uiSendCnt++; /*On the surface, this data is continuously accumulated, but in the serial port interrupt, it will be cleared every time a byte is received, unless there is no serial port data in the middle*/
ucSendLock = 1; /*Open the self-locking flag*/
}
if(uiVoiceCnt != 0)
{
uiVoiceCnt --;
BEEP = 0;
}
else
{
;
BEEP = 1;
}
TL0 = T1MS; /*initial timer0 low byte*/
TH0 = T1MS >> 8; /*initial timer0 high byte*/
TR0 = 1; /*Enable interrupt*/
}
/**
* @brief Serial port receiving data interrupt
* @param None
* @retval None
**/
void usart_receive(void) interrupt 4
{
if(RI == 1)
{
RI = 0;
++ uiRcregTotal;
if(uiRcregTotal > const_rc_size)
{
uiRcregTotal = const_rc_size;
}
ucRcregBuf[uiRcregTotal - 1] = SBUF; /*Cache the data received by the serial port into the receiving buffer*/
uiSendCnt = 0; /* Feed the dog in time. Although the main function keeps accumulating, as long as the serial port data has not been sent, it will never grow because each interrupt is cleared. */
}
else
{
TI = 0;
}
}
/*————————————Main function————————————*/
/**
* @brief main function
* @param None
* @retval makes the LED light flash
**/
void main()
{
/*MCU initialization*/
Init();
/*Delay, the delay time is generally between 0.3 seconds and 2 seconds, waiting for the peripheral chips and modules to be powered on and stable*/
Delay_Long(100);
/*MCU peripheral initialization*/
Init_Peripheral();
while(1)
{
usart_service();
}
}
3. Simulation Implementation
The second biggest benefit of pointers is that pointers serve as input interfaces for arrays in functions.
Previous article:The fifth benefit of pointers: pointers act as a transit station in many arrays
Next article:The fourth benefit of pointers is that pointers serve as input and output interfaces for arrays in functions.
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- What does the outer diameter of the color ring of MULTI-LAYER mean? The size of the drill hole is the inner diameter, so what does the outer diameter mean?
- 2020 Luoyang in my eyes
- cc2640 to cc2640R2F
- Do you know where I can find a 5V to 1.5V power supply?
- Vote for reliable national chips - MCU: Test, test, test, which reliable national chips have the first-line forum friends detected?
- C6000 embedded assembly C and assembly comparison and function description
- [GD32L233C-START Review] 1. Receive the development board
- Bicycle modification series: fixed solar panels
- After reading these two books, you will never be embarrassed to talk about the Internet of Things again!
- Verilog007