Hi, recently I had to incorporate an RTC into an design, as most RTC = provides the data in packed BCD I also had to dwell into BCD conversion, addition etc. Thre were a few snippets on the piclist site but there were one thing = that I needed=20 that I could not find. Namely an BCD test and clamp function, as my design has provisions for a = user to input arbitray data input into the RTC I wanted to perform an = BCD sanity check before the actual write into the RTC.=20 The requirments: Input: 0x00 - 0xFF BCD Min limit: 0x00-0x98 BCD Max Limit: 0x01-0x99 (note: BCD Max > BCD Min ) Formula: If(Input!BCD)OutPut =3D MaxBCD; else if(Input>MaxBCD) Output =3DMaxBCD; else if(Input BCD XORWF Temp,F ; check if w has changed=20 BTFSS STATUS,Z MOVLW MaxBCD ; w has changed, ie input was not BCD, set to maxvalue =09 ; check if below max ADDLW LOW(0xFF-MaxBCD) BTFSC STATUS,C MOVLW 0xFF ; overflow set to max =09 ; now check for minium value ADDLW LOW((MaxBCD-MinBCD)+0x01) BTFSS STATUS,C MOVLW MinBCD-0x01 ; value to low, set to min ADDLW 0x01 ; finally fix-up W ENDM Usage: MOVLW 0x1A ; put in a *non* bcd number CHECK_W_BCD_DIGIT_CLAMP 0x01,0x12,Temp,KnownZero ; W =3D 0x12 here MOVLW 0x00 ; put in a to low bcd number CHECK_W_BCD_DIGIT_CLAMP 0x01,0x12,Temp,KnownZero ; W =3D 0x01 here MOVLW 0x61 ; put in a to high bcd number CHECK_W_BCD_DIGIT_CLAMP 0x00,0x59,Temp,KnownZero ; W =3D 0x59 here I think the above works, I haven't tested it yet, but I see no obvious = errors. Anyone have anything better ? /Tony -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads