/***************************************************************************/ typedef union { byte byteval[4]; dword result; } BCDval; /* Global variables */ byte RFID[6]; /* Function prototypes */ void disp_data (void); dword hex2bin (byte *); dword bin2BCD_conv (dword ); byte BCD_adj (byte ); byte upper_char (byte ); byte lower_char (byte ); void printf_LCD_4bits(byte, byte, char *); /***************************************************************************/ /* Convert data and display 8-digit result on LCD On entry, global array RFID contains the hexadecimal data to be converted. Leading zeros are not suppressed. ****************************************************************************/ void disp_data (void) { BCDval temp; byte LCDtext[9]; byte i, j; temp.result = bin2BCD_conv (hex2bin (RFID)); for (i = 0, j = 0; i < 4; i++) { LCDtext[j] = upper_char (temp.byteval[i]); LCDtext[j+1] = lower_char (temp.byteval[i]); j += 2; } LCDtext[8] = 0; /* Null terminate string data */ printf_LCD_4bits (1,1,LCDtext); } /***************************************************************************/ /* Hexadecimal -> binary conversion The array data referenced by the pointer hexseq must contain only one nybble in each byte of the array, right justified. The binary value returned is a 24-bit value, right justified. */ dword hex2bin (byte *hexseq) { byte i; dword temp = 0; for (i = 0; i < 6; i++) { temp = (temp << 4) | *hexseq; hexseq++; ) return (temp); } /***************************************************************************/ /* Binary -> BCD conversion A 24-bit binary value is converted to eight BCD digits, with two digits packed per return byte. */ dword bin2BCD_conv (dword val) ( BCDval res; byte i; res.result = 0; for (i = 0; i < 24; i++) { res.byteval[0] = BCD_adj (res.byteval[0]); res.byteval[1] = BCD_adj (res.byteval[1]); res.byteval[2] = BCD_adj (res.byteval[2]); res.byteval[3] = BCD_adj (res.byteval[3]); res.result <<= 1; if (val & 0x800000) res.result += 1; val <<= 1; } return (res.result); } byte BCD_adj (byte val) { if ((val & 0x0F) >= 5) val += 3; if ((val & 0xF0) >= 0x50) val += 0x30; return (val); } /***************************************************************************/ /* Unpack BCD digit and convert to ASCII value */ byte upper_char (byte value) { return ((value >> 4) + 0x30); } byte lower_char (byte value) { return ((value & 0x0F) + 0x30); } /***************************************************************************/ /* Write ASCII string to LCD */ void printf_LCD_4bits(byte fila, byte columna, char *texto) { byte adrs; adrs = columna - 1; if (fila == 2) adrs = adrs | 0x40; Ctrl4bits(adrs | 0x80); while (*texto) Datos4bits(texto++); }