Mac 67 Posted July 29, 2011 Share Posted July 29, 2011 Hi guys. A couple questions, please? (1) Anyone know if there's a reciprocal for the _bcd_add_short() intrinsic function that I can use to decrement packed BCD variables for a countdown timer routine? (2) If there isn't a BCD subtract instrinsic, is there a good way to do decrement a packed BCD number in C, without resorting to in-line assembly code? TIA! Regards, Mike #define timer_running (hh|mm|ss) /* * countdown timer, hh:mm:ss (1 second intervals) */ if(timer_running) // { if(ss != 0x00) // if seconds not 0x00 { asm(" clrc "); // decrement seconds asm(" dadd.b #0x99,&ss "); // (packed BCD) } else // else, seconds == 0x00 so { ss = 0x59; // reset seconds to 0x59 and if(mm != 0x00) // if minutes not 0x00 { asm(" clrc "); // decrement minutes asm(" dadd.b #0x99,&mm "); // (packed BCD) } else // else, minutes == 0x00 so { mm = 0x59; // reset minutes to 0x59 and asm(" clrc "); // decrment hours asm(" dadd.b #0x99,&hh "); // (packed BCD) } // } // if(!timer_running) // if timer has timed-out P1OUT ^= relay_pin_mask; // turn relay output off } // Quote Link to post Share on other sites
RobG 1,892 Posted July 29, 2011 Share Posted July 29, 2011 How about _bcd_add_short(-1) Never mind, that function takes unsigned Mac 1 Quote Link to post Share on other sites
Mac 67 Posted July 29, 2011 Author Share Posted July 29, 2011 How about _bcd_add_short(-1) Excellent! Thank you! I really should have thought of that (argh!)... Quote Link to post Share on other sites
RobG 1,892 Posted July 29, 2011 Share Posted July 29, 2011 Never mind, that function takes unsigned How about overflowing your destination variable? a = _bcd_add_short(a, 0x9999); Quote Link to post Share on other sites
Mac 67 Posted July 29, 2011 Author Share Posted July 29, 2011 I spoke too soon (grin). I'm trying some variations right now... Quote Link to post Share on other sites
RobG 1,892 Posted July 29, 2011 Share Posted July 29, 2011 a = _bcd_add_short(a, 0x9999); Works as expected, 0x9999 is -1, 0x9998 is -2, etc. Quote Link to post Share on other sites
Mac 67 Posted July 29, 2011 Author Share Posted July 29, 2011 That should do it... /* * countdown timer, hh:mm:ss (1 second intervals) */ if(timer_running) // { if(ss) // if seconds > 0x00 { ss = _bcd_add_short(ss,0x99); // decrement seconds } else // else, seconds == 0x00 so { ss = 0x59; // reset seconds to 0x59 and if(mm) // if minutes > 0x00 { mm = _bcd_add_short(mm,0x99); // decrement minutes } else // else, minutes == 0x00 so { mm = 0x59; // reset minutes to 0x59 and hh = _bcd_add_short(hh,0x99); // decrement hours } // } // if(!timer_running) // if timer has timed-out P1OUT ^= relay_pin_mask; // turn relay output off } // Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.