given the following declarations for a bunch of 24 bit fixed point values...
- Code: Select all
struct notestep{ // type for a 24 bit fixed point value
uint8_t integer;
uint16_t fraction;
};
const notestep basestep = { 3, 0x46dc }; // 200Hz
notestep offsetstep = { 3, 0x70cd }; // 210Hz
notestep baseaccumulator;
notestep offsetaccumulator;
this works...
- Code: Select all
offsetaccumulator.fraction += offsetstep.fraction;
if (SREG & (1 << SREG_C)) // check for carry
offsetaccumulator.integer++;
offsetaccumulator.integer += offsetstep.integer;
and this doesn't ...
- Code: Select all
baseaccumulator.fraction += basestep.fraction;
if (SREG & (1 << SREG_C)) // check for carry
baseaccumulator.integer++;
baseaccumulator.integer += basestep.integer;
i finally printed out enough intermediate values to figure out that the carries were not happening when they should, so i disassembled the binary and it turned out that with baseoffset declared as a constant the addition compiles to a subtraction, as if i had written ...
- Code: Select all
baseaccumulator.fraction -= -(0x46dc);
if (SREG & (1 << SREG_C)) // check for carry
baseaccumulator.integer++;
baseaccumulator.integer += basestep.integer;
... and the damned carry flag is not set when i expect it to be.
removing the const attribute from the baseoffset declaration fixed things.
now, maybe i can start working on some real bugs.


