-
Hex digit verification
Hey guys,
As you've gathered from my "parts sourcing" post, I am in the heat of the battle on my "code sniffer" project. I thought I had the code to check for valid digits licked but then...doh!
Code:
EEAddress=414
I2CREAD I2cData,I2cClock,$A0,EEAddress,[CheckPanelDigit]
IF CheckPanelDigit < 153 THEN
EEAddress=415
I2CREAD DPIN,CPIN,$A0,EEAddress,[CheckPanelDigit]
IF CheckPanelDigit < 153 THEN 'BETWEEN HEX 00 AND 99
Goto ConcordExpress
else
goto SnifferErr
endif
else
goto SnifferErr
endif
As I am checking for "valid" digits, I figured I could just make sure they are below DEC 153 (Hex 99). Well, what if I happen to get a "8E" (DEC 142)? The code will execute, which I can't allow to happen. I guess the easiest way to explain it is: the hex received must have 2 valid digits in it, 0-9.
Is there a routine, or a bit of math trickery I can perform to do the verification of the hex pair?
Whew, I think I confused myself after reading this post! :confused:
I hope it makes sense to one of you.
Thanks guys,
Chris
-
I think this will work...
Code:
<<<SNIP>>>
EEAddress=0
I2CREAD I2cData,I2cClock,$A0,EEAddress,[CheckPanelDigit]
pause 50
gosub TestHex
if ValidDigits = 1 then
EEAddress=1
I2CREAD DPIN,CPIN,$A0,EEAddress,[CheckPanelDigit]
pause 50
gosub TestHex
if ValidDigits = 1 then
Goto Vista
else
goto SnifferErr
endif
else
goto SnifferErr
endif
goto MainLoop
SnifferErr:
LCDout $fe, 1
LCDout $fe, 2, " ERROR "
Lcdout $fe, $C0, "PANEL NOT RECOGNIZED"
Lcdout $fe, $94, "CHECK CONNECTIONS OR"
Lcdout $fe, $D4, "SELECT CORRECT PANEL"
GOTO MainLoop
TestHex:
ValidDigits = 0
TestDigit = CheckPanelDigit & $0F ' Isolate the lower nibble
if TestDigit < 10 then ' First digit is 0-9
TestDigit = CheckPanelDigit >> 4 ' move high nibble to low nibble
if TestDigit < 10 then ' Second digit is 0-9
ValidDigits = 1 ' Set flag for good digits
endif
endif
return
How does that look? Am I thinking along the correct lines?
Thanks guys,
Chris
-
Hi,
It looks to me like you have a two digit BCD value packed into one byte, is that correct?
I'm sure there are better and more clever ways of doing it but this seems to do what I think it is you're asking about:
Code:
If (CheckPanelDigit & 15) < 10 AND (CheckPanelDigit & 240) >> 4 < 10 then
'...more code here
Try it out and see if it does what you want.
/Henrik.
-
Thanks Henrick,
Sure is prettier than my code :D. As far as the BCD in a byte, yep, pretty much it. It can be any Hex digit in there. So, I just need to make sure each nibble in the byte is a number, not a letter. I'll give your code a shot in a bit. I'll have to intentionally read from the wrong address to make a go of it. On another quick note, would you have a quick and easy, down and dirty way to "flip" the 2 hex characters around? ie: HEX59 becomes HEX95. One of the panels I am working on stores its codes "backwards". Sneaky 'lil buggers. I need to flip 'em so I can display them on the lcd. I've got a routine working now, the low nibble - high nibble way, but just wondering if there was a better way?
Thanks again,
Chris
-
Hi Chris,
You actually posted while I was typing my reply - I didn't see your own response.
Anyway, if you can afford two new bytes, lets call them Ones and Tens then you could do something like this instead (it produces slightly smaller code:
Code:
Ones VAR BYTE
Tens VAR BYTE
Ones = CheckPanelDigit & 15
Tens = (CheckPanelDigit & 240) >> 4
If Ones < 10 AND Tens < 10 then
'....more code here
This will then allow you to do something like:
Code:
SwappedValue VAR BYTE
SwappedValue = Ones << 4 + Tens
Which swappes the order of the high and low nibble in the byte.
/Henrik.
-
Very cool Henrick, thanks. When I get a second to try that code, I will. I'll report back asap.
Thanks again,
Chris