This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
The following code fragment contains an unintentional error in that the ISR contains an expression with no effect on the highlighted line:
#include <msp430.h>
// Updated by I2C ISR
#define LCD_WRITE_IN_PROGRESS 0
#define LCD_WRITE_COMPLETE 1
#define LCD_WRITE_NACK 2
volatile char LCDWriteStatus = LCD_WRITE_IN_PROGRESS;
void main(void) {
}
/* The USCI_B0 interrupt vector service routine */
#pragma vector = USCI_B0_VECTOR
__interrupt void USCI_B0_ISR (void)
{
switch (__even_in_range(UCB0IV,12))
{
case USCI_I2C_UCNACKIFG:
{
LCDWriteStatus == LCD_WRITE_NACK;
// Turn on LED1 for debugging
P1DIR |= BIT0;
P1OUT |= BIT0;
//Exit LPM3 on interrupt return
__bic_SR_register_on_exit(LPM3_bits);
break;
}
}
}
I found that if the volatile qualifier is removed from the definition of LCDWriteStatus then the following warning is reported:
"../main.c", line 22: warning #176-D: expression has no effect
'Finished building: ../main.c'
i.e. it seems that the use of volatile suppresses the "#176-D expression has no effect" warning. In this example it doesn't seem correct for the compiler to suppress the warning (since writing LCDWriteStatus == LCD_WRITE_NACK; instead of LCDWriteStatus = LCD_WRITE_NACK; caused my code not to work)
It is correct to suppress the warning. Declaring a variable volatile is a signal to the compiler that there is something going on that the compiler cannot see. For instance, a volatile variable may represent a memory-mapped device, for which a read or a write might trigger a side-effect that is not represented in the C code. Thus, for all the compiler knows, that expression might actually have an effect.