Ok so I got this:
const int PERIOD_NORMAL = 10; //brings the error down to something the controller can deal with
volatile uint16_t r_period = 0; //time between right clicks
void do_some_math(int right_target)
{
int r_error = 0;
r_error = (right_target/PERIOD_NORMAL - r_period/PERIOD_NORMAL);
rprintf("r_error: %d\n",r_error);
}
If I call this all is well and r_error can be negative.
If r_error = (right_target/PERIOD_NORMAL - r_period/PERIOD_NORMAL);
is changed to r_error = (right_target - r_period)/PERIOD_NORMAL;
r_error can no longer be negative. If it should be negative, it will instead become some number around 16000.
What C voodoo is going on here?