Skip to main content
Renesas Electronics America - Knowledgebase

Software timer and optimization by the compiler(CC-RH/CC-RX/CC-RL)

Last Updated:04/17/2017


I have programmed a software timer that is intended to cause a wait for a certain amount of time by looping a specified number of times. However, the loop is not processed the number of times I specified, so the time is not as I expected. What might be the reason for this?


Optimization might be deleting the loop processing.
You can prevent the deletion of loop processing by calling the nop* intrinsic function in the for statement or by qualifying the loop variable i as volatile.
However, the version of the compiler and compiler options may affect the output code, so the processing time might be different even for the same C code. 
Thus, if you change compiler versions or options, you need to check if the processing time is still as you expected.
If you write the code to set up the specified waiting time in assembly rather than C, the above phenomena will not apply.
If interrupt processing also proceeds within the loop, the time for this will also be included, so this must also be considered in choosing the number of times to loop when adjusting the waiting time.
If the waiting time requires precision, we recommend using a timer of the MCU to measure the time, instead of a software timer where the time is set up by program code.
*: The nop function differs according to whether it is the nop function of CC-RX or the __nop function of the CC-RH and CC-RL compilers. Refer to the user's manual of the given compiler for details.

- C Compiler Package for RH850 Family (CC-RH) Embedded Functions
- C/C++ Compiler Package for RX Family (CC-RX)
 4.2.6 Intrinsic Functions
- C Compiler Package for RL78 Family (CC-RL)
 4.2.5 Embedded Functions

Suitable Products
e² studio
CS+ (formerly CubeSuite+)
C Compiler Package for RH850 Family
C/C++ Compiler Package for RX Family
C Compiler Package for RL78 Family