Tony, Correct the text: ; MinBCD - Constant to ; MaxBCD - Constant You have applied three filters: DAW, and the range detector 255-HI; (HI-LOW)+1 it works fine, but I have not tested thoroughly. This filter force to Max when the number isn't BCD format? Why don't simply reject the entry? Very fast detector. Congrats. Denis. ----- Original Message ----- From: "Tony K|bek" To: Sent: Thursday, October 09, 2003 6:40 AM Subject: [PIC]: Tweaking a PIC for .. BCD.. Challenge 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 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. 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 = MaxBCD; else if(Input>MaxBCD) Output =MaxBCD; else if(Input BCD XORWF Temp,F ; check if w has changed BTFSS STATUS,Z MOVLW MaxBCD ; w has changed, ie input was not BCD, set to maxvalue ; check if below max ADDLW LOW(0xFF-MaxBCD) BTFSC STATUS,C MOVLW 0xFF ; overflow set to max ; 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 = 0x12 here MOVLW 0x00 ; put in a to low bcd number CHECK_W_BCD_DIGIT_CLAMP 0x01,0x12,Temp,KnownZero ; W = 0x01 here MOVLW 0x61 ; put in a to high bcd number CHECK_W_BCD_DIGIT_CLAMP 0x00,0x59,Temp,KnownZero ; W = 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 -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads