Add const to pointers to avoid accidental modification of input-only data

Publisher:Blissful444Latest update time:2021-11-04 Source: eefocusKeywords:Pointer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

* 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.

[1] [2]
Keywords:Pointer Reference address:Add const to pointers to avoid accidental modification of input-only data

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.

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号