2
votes

This line isn't working as expected:

uartPushPos = (uartPushPos + 1) % UART_TX_BUFF_LENGTH;

However this below, which in theory does the same, does work:

//if (uartPushPos == UART_TX_BUFF_LENGTH - 1){
if (uartPushPos >= UART_TX_BUFF_LENGTH - 1){
    uartPushPos = 0;
} else {
    uartPushPos++;
}

UartPopPos is type char, and UART_TX_BUFF_LENGTH is a preprocessor variable set to 16.

Why does the second code segment work, but not the first?

If it makes much of a difference, I'm using the SourceBoost BoostC compiler for the PIC microcontroller 16f.

Thanks

1
In what way are you getting unexpected results with the first option? - Paul W
Is there a flag, such as the -S flag for gcc, that lets you have a look at the compiled assembler code? I think that would help you. - André Laszlo
All I know is it's not working as expected, I can't get my head round what's actually happening, perhaps I should some how isolate the code segment to test it properly, I was just hoping there was an obvious answer I'd missed! - CL22

1 Answers

4
votes

They are different if uartPushPos is less than 0, or if it is more than or equal to UART_TX_BUFF_LENGTH.

See also Mod of negative number is melting my brain