Answer the question
In order to leave comments, you need to log in
How to get rid of the message about unused variables?
In embedder practice, quite often you have to use a variable to read the value of a register. Moreover, the read value itself is not required in the future, only the fact of reading the register during which the service bits are reset is important.
Everything would be fine, but annoying messages appear that the variable is initialized, but is not used anywhere. As in the example below, it happened with the status variable.
Question - is it possible to eliminate the generation of these warnings with the help of a cunning variable declaration? And yes, we are talking about the ANSI C language.
void I2C1_EV_IRQHandler()//приём данных в режиме SLAVE
{
uint16_t status;
if (I2C_GetITStatus(I2C1, I2C_IT_ADDR) == SET)
{//проверяем адрес устройства
status = I2C1->SR1;
status = I2C1->SR2;
SlaveBufPos = 0;
}
status = I2C1->SR1;
status = I2C1->SR2;
Answer the question
In order to leave comments, you need to log in
The ban on "optimization" can be done by declaring the variable volatile
Try assigning the variable to itself. The normal compiler will throw out this operation, and the warning will not be generated.
It all depends on the specific compiler, for example, in Visual Studio, warnings can be disabled via #pragma by number.
void check_register( uint16_t status ) { status = status; }
or
uint16_t check_register( uint16_t status ) { return status; }
and call
check_register(I2C1->SR1);
a normal compiler will not call such a function, but it will also not be able to ignore it;)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question