Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 sbit LED1 at LATD0_bit; sbit LED2 at LATD1_bit; sbit LED3 at LATD2_bit; sbit LED4 at LATD3_bit; sbit LED5 at LATD4_bit; sbit LED1_Direction at TRISD0_bit; sbit LED2_Direction at TRISD1_bit; sbit LED3_Direction at TRISD2_bit; sbit LED4_Direction at TRISD3_bit; sbit LED5_Direction at TRISD4_bit; unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03}; unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03}; unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03}; unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03}; unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03}; unsigned char uartBuffer[14], received = 0, i = 0; void interrupt() { if(RC1IF_bit) { if(OERR1_bit) { OERR1_bit = 0; CREN1_bit = 0; CREN1_bit = 1; } uartBuffer[i] = UART1_Read(); if(received == 0) { if(uartBuffer[i] == 2) { received = 1; i++; } } else if(received == 1) { if(uartBuffer[i] != 3) i++; else { received = 2; GIE_bit = 0; } } RC1IF_bit = 0; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0xC0; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; UART1_Init(9615); Delay_ms(200); RC1IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; RC1IF_bit = 0; LED1_Direction = 0; LED2_Direction = 0; LED3_Direction = 0; LED4_Direction = 0; LED5_Direction = 0; LED1 = 0; LED2 = 0; LED3 = 0; LED4 = 0; LED5 = 0; PWM1_Init(2000); PWM1_Set_Duty(255); PWM1_Stop(); UART1_Write_Text("Swipe a card\r\n"); while(1) { if(received == 2) { if(memcmp(uartBuffer, tag1, 14) == 0) { LED1 = ~LED1; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag2, 14) == 0) { LED2 = ~LED2; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag3, 14) == 0) { LED3 = ~LED3; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag4, 14) == 0) { LED4 = ~LED4; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag5, 14) == 0) { LED5 = ~LED5; UART1_Write_Text("Tag matched\r\n"); } else UART1_Write_Text("Tag doesn't match\r\n"); PWM1_Start(); Delay_ms(3000); PWM1_Stop(); received = 0; i = 0; memset(uartBuffer, '\0', sizeof(uartBuffer)); UART1_Write_Text("Swipe a card\r\n"); GIE_bit = 1; RC1IF_bit = 0; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 sbit LED1 at LATD0_bit; sbit LED2 at LATD1_bit; sbit LED3 at LATD2_bit; sbit LED4 at LATD3_bit; sbit LED5 at LATD4_bit; sbit LED1_Direction at TRISD0_bit; sbit LED2_Direction at TRISD1_bit; sbit LED3_Direction at TRISD2_bit; sbit LED4_Direction at TRISD3_bit; sbit LED5_Direction at TRISD4_bit; unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03}; unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03}; unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03}; unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03}; unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03}; unsigned char uartBuffer[14], received = 0, i = 0; int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0; char buffer[3]; void interrupt() { if(RC1IF_bit) { if(OERR1_bit) { OERR1_bit = 0; CREN1_bit = 0; CREN1_bit = 1; } uartBuffer[i] = UART1_Read(); if(received == 0) { if(uartBuffer[i] == 2) { received = 1; i++; } } else if(received == 1) { if(uartBuffer[i] != 3) i++; else { received = 2; GIE_bit = 0; } } RC1IF_bit = 0; } } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0xC0; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; UART1_Init(9615); Delay_ms(200); RC1IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; RC1IF_bit = 0; LED1_Direction = 0; LED2_Direction = 0; LED3_Direction = 0; LED4_Direction = 0; LED5_Direction = 0; LED1 = 0; LED2 = 0; LED3 = 0; LED4 = 0; LED5 = 0; PWM1_Init(2000); PWM1_Set_Duty(255); PWM1_Stop(); UART1_Write_Text("Swipe a card\r\n"); while(1) { if(received == 2) { buffer[0] = uartBuffer[1]; buffer[1] = uartBuffer[2]; buffer[2] = '\0'; val1 = atoi(buffer); buffer[0] = uartBuffer[3]; buffer[1] = uartBuffer[4]; buffer[2] = '\0'; val2 = atoi(buffer); buffer[0] = uartBuffer[5]; buffer[1] = uartBuffer[6]; buffer[2] = '\0'; val3 = atoi(buffer); buffer[0] = uartBuffer[7]; buffer[1] = uartBuffer[8]; buffer[2] = '\0'; val4 = atoi(buffer); buffer[0] = uartBuffer[9]; buffer[1] = uartBuffer[10]; buffer[2] = '\0'; val5 = atoi(buffer); buffer[0] = uartBuffer[11]; buffer[1] = uartBuffer[12]; buffer[2] = '\0'; val6 = atoi(buffer); if(val6 == ((val1) ^ (val2) ^ (val3) ^ (val4) ^ (val5))) { if(memcmp(uartBuffer, tag1, 14) == 0) { LED1 = ~LED1; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag2, 14) == 0) { LED2 = ~LED2; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag3, 14) == 0) { LED3 = ~LED3; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag4, 14) == 0) { LED4 = ~LED4; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag5, 14) == 0) { LED5 = ~LED5; UART1_Write_Text("Tag matched\r\n"); } } else UART1_Write_Text("Tag doesn't match\r\n"); PWM1_Start(); Delay_ms(3000); PWM1_Stop(); received = 0; i = 0; memset(uartBuffer, '\0', sizeof(uartBuffer)); UART1_Write_Text("Swipe a card\r\n"); GIE_bit = 1; RC1IF_bit = 0; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 sbit LED1 at LATD0_bit; sbit LED2 at LATD1_bit; sbit LED3 at LATD2_bit; sbit LED4 at LATD3_bit; sbit LED5 at LATD4_bit; sbit LED1_Direction at TRISD0_bit; sbit LED2_Direction at TRISD1_bit; sbit LED3_Direction at TRISD2_bit; sbit LED4_Direction at TRISD3_bit; sbit LED5_Direction at TRISD4_bit; unsigned char tag1[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x32, 0x43, 0x45, 0x31, 0x03}; unsigned char tag2[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x31, 0x32, 0x31, 0x30, 0x41, 0x36, 0x45, 0x03}; unsigned char tag3[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x35, 0x42, 0x44, 0x33, 0x38, 0x43, 0x34, 0x03}; unsigned char tag4[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x42, 0x37, 0x45, 0x38, 0x31, 0x42, 0x03}; unsigned char tag5[] = {0x02, 0x31, 0x34, 0x30, 0x30, 0x35, 0x30, 0x38, 0x39, 0x33, 0x31, 0x46, 0x43, 0x03}; unsigned char uartBuffer[14], received = 0, i = 0; unsigned int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0; char buffer[3]; void interrupt() { if(RC1IF_bit) { if(OERR1_bit) { OERR1_bit = 0; CREN1_bit = 0; CREN1_bit = 1; } uartBuffer[i] = UART1_Read(); if(received == 0) { if(uartBuffer[i] == 2) { received = 1; i++; } } else if(received == 1) { if(uartBuffer[i] != 3) i++; else { received = 2; GIE_bit = 0; } } RC1IF_bit = 0; } } unsigned int Ascii2Hex(char *s) { unsigned int num1 = 0, num2 = 0; if((*s >= '0') & (*s <= '9')) num1 = *s - 48; else if((*s >= 'A') && (*s <= 'F')) num1 = *s - 55; *s++; if((*s >= '0') & (*s <= '9')) num2 = *s - 48; else if((*s >= 'A') && (*s <= 'F')) num2 = *s - 55; return (num1 * 16) + (num2); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; ANSELD = 0x00; ANSELE = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0xC0; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LATE = 0x00; UART1_Init(9615); Delay_ms(200); RC1IE_bit = 1; PEIE_bit = 1; GIE_bit = 1; RC1IF_bit = 0; LED1_Direction = 0; LED2_Direction = 0; LED3_Direction = 0; LED4_Direction = 0; LED5_Direction = 0; LED1 = 0; LED2 = 0; LED3 = 0; LED4 = 0; LED5 = 0; PWM1_Init(2000); PWM1_Set_Duty(255); PWM1_Stop(); UART1_Write_Text("Swipe a card\r\n"); while(1) { if(received == 2) { buffer[0] = uartBuffer[1]; buffer[1] = uartBuffer[2]; buffer[2] = '\0'; val1 = Ascii2Hex(buffer); buffer[0] = uartBuffer[3]; buffer[1] = uartBuffer[4]; buffer[2] = '\0'; val2 = Ascii2Hex(buffer); buffer[0] = uartBuffer[5]; buffer[1] = uartBuffer[6]; buffer[2] = '\0'; val3 = Ascii2Hex(buffer); buffer[0] = uartBuffer[7]; buffer[1] = uartBuffer[8]; buffer[2] = '\0'; val4 = Ascii2Hex(buffer); buffer[0] = uartBuffer[9]; buffer[1] = uartBuffer[10]; buffer[2] = '\0'; val5 = Ascii2Hex(buffer); buffer[0] = uartBuffer[11]; buffer[1] = uartBuffer[12]; buffer[2] = '\0'; val6 = Ascii2Hex(buffer); if(val6 == ((val1) ^ (val2) ^ (val3) ^ (val4) ^ (val5))) { if(memcmp(uartBuffer, tag1, 14) == 0) { LED1 = ~LED1; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag2, 14) == 0) { LED2 = ~LED2; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag3, 14) == 0) { LED3 = ~LED3; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag4, 14) == 0) { LED4 = ~LED4; UART1_Write_Text("Tag matched\r\n"); } else if(memcmp(uartBuffer, tag5, 14) == 0) { LED5 = ~LED5; UART1_Write_Text("Tag matched\r\n"); } } else UART1_Write_Text("Tag doesn't match\r\n"); PWM1_Start(); Delay_ms(3000); PWM1_Stop(); received = 0; i = 0; memset(uartBuffer, '\0', sizeof(uartBuffer)); UART1_Write_Text("Swipe a card\r\n"); GIE_bit = 1; RC1IF_bit = 0; } } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 unsigned int getHex(char s) { unsigned int num = 0; if((s >= '0') & (s <= '9')) num = s - 48; else if((s >= 'A') && (s <= 'F')) num = s - 55; return num; } unsigned int Ascii2Hex(char *s) { unsigned int num1 = 0, num2 = 0; num1 = getHex(*s); *s++; num2 = getHex(*s); return (num1 * 16) + (num2); }
...
How to convert decimal to hex ?
- - - Updated - - -
How to convert Ascii to Hex ? Like if I have "12AF" it should be converted to 0x12 and 0xAF. How to do this ?
static char hex(unsigned char x){
//convert 4bits binary to 1 text hex digit
x &= 0x0f;
return (x<10) ? x +'0': x-10+'a';
}
static unsigned char _1dig_hex2int(char h){
//convert 1 text hex digit to equivalent binary
if(h>='0' && h<='9')
return h - '0';
else if(h>='a' || h<='f')
return h - 'a' + 10;
else if(h>='A' || h<='F')
return h - 'A' + 10;
return 0;
}
unsigned hex2uint(const char *hex){
//priority in portability in stead of speed
char i;
unsigned v = 0;
for(i=0;i<sizeof(v)*2 && hex[i];i++){
v <<= 4;
v += (_1dig_hex2int(hex[i]) & 0x0f);
}
return v;
}
const char *uint2hex(unsigned num,char *hx){
//priority in portability in stead of speed
signed char i,len=0;
do{
hx[len++] = hex(num&0x0f);
num>>=4;
}while(num);
hx[len]='\0';
//reverse order
for(i=len/2;i>=0;i--){
char tmp = hx[i];
hx[i] = hx[len-i-1];
hx[len-i-1] = tmp;
}
return hx;
}
void main(){
char s[sizeof(int) * 2 + 1];
printf("%u = %u\n",0x345a,hex2uint("345a"));
printf("%x = %s\n",0x345a,uint2hex(0x345a,s));
}
//*******************************************************************************
// convert an ascii hex digit to it's equivalent binary value
unsigned char AscToBin(char AsciiChar)
{
if(AsciiChar > '9') AsciiChar -= 7;
return((AsciiChar - '0') & 0x0F);
}
//*******************************************************************************
// convert a binary value to it's equivalent ascii hex character
unsigned char BinToAsc(unsigned char BinValue)
{
BinValue &= 0x0F;
if(BinValue > 9) BinValue += 7;
return(BinValue + '0');
}
@ bigdogguru
In post #2 you mentioned
Card Number in Decimal: 0005278001
Card Number in Binary: 10100001000100100110001
RFID Output Stream in Hexadecimal: 1400508931FC
If I convert dec 5278001 to hex I get 508931. FC is checksum. Where did 1400 came from ?
Bits D00 to D03 and bits D10 to D13 are customer specific identification.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?