On Tue, Jan 24, 2012 at 4:27 PM, PICdude wrote: > Hmmm... still tinkering with C18 and ran into this oddity... > > #define S2_MIN 90 > #define S2_MAX 125 > #define S2_STEP 5 > ... > void main(void) > { > =A0 =A0 =A0 =A0... > =A0 =A0 =A0 =A0S2 =3D S2_MIN; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0// Initialize > =A0 =A0 =A0 =A0... > =A0 =A0 =A0 =A0// (In button responder routine...) > =A0 =A0 =A0 =A0S2 +=3D S2_STEP; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0// Increment to next value > =A0 =A0 =A0 =A0if (S2 >=3D (S2_MAX + S2_STEP)) =A0 =A0 =A0 =A0 =A0 // If = max passed... > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0S2 =3D S2_MIN; =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0// ...cycle back to start > =A0 =A0 =A0 =A0... > } > > Every time S2 is incremented, the comparison checks to see if the max > value has been exceeded, and if so, it will reset it to the min value. > =A0But it fails. > > If I change the comparison to this, it works... > > =A0 =A0 =A0 =A0if (S2 > S2_MAX) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0S2 =3D S2_MIN; =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0// Cycle back around to start > > Disassembly of the non-working code (with comments I've now added). > 0xc0 and 0xbf are the upper and lower bytes of S2 resp. =A0Computations > in comments are at S2=3D130. > > 845: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0if (S2 >=3D (S2_MAX + S2_STEP)) > =A0 0B16 =A0 =A00E82 =A0 =A0 MOVLW 0x82 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0// 130, correct. > =A0 0B18 =A0 =A05DBF =A0 =A0 SUBWF 0xbf, W, BANKED =A0 =A0 =A0 // Result= =3D0, so C=3D1, Z=3D1 > =A0 0B1A =A0 =A00EFF =A0 =A0 MOVLW 0xff > =A0 0B1C =A0 =A059C0 =A0 =A0 SUBWFB 0xc0, W, BANKED =A0 =A0 =A0// 511-0 i= s +ve, so C=3D1, Z=3D0. > =A0 0B1E =A0 =A0E303 =A0 =A0 BNC 0xb26 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 // So this should not branch...? > 847: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0S2 =3D S2_MIN; > =A0 0B20 =A0 =A00E5A =A0 =A0 MOVLW 0x5a > =A0 0B22 =A0 =A06FBF =A0 =A0 MOVWF 0xbf, BANKED > =A0 0B24 =A0 =A06BC0 =A0 =A0 CLRF 0xc0, BANKED > > > If I understand what's happening here, the lower bytes are subtracted > first, and C is carried over into the next operation where the upper > byte is subtracted. =A0Not sure why 255 is being used for that though. > But at S2=3D130, C should be 1 when it reaches the BNC instruction. > But's it's skipping the code after that branch. =A0What's really > confusing me here is the SUBWFB instruction. =A0I've looked at the > datasheet for it and the examples provided for its use, and can't see > how they get those results. > > Huh? > How is "S2" declared? If it is an int8_t (aka char) then 125+5 =3D -126, which is always going to be less than S2_MIN, so S2 will remain at 90. --=20 Regards, Mark markrages@gmail --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .