Hi everyone, Regarding to my previous post: http://www.keil.com/forum/docs/thread4443.asp I manage to write a code to suits my needs, the code more likely like this:
command[9] = {0x3C , 0x4A , 0x33 , 0x25 , 0xB1 , 0xD3 , 0x59 , 0x34 , 0x2E } command2[9] = {0x53 , 0x66 , 0x39 , 0xA2 , 0xB1 , 0xA2 , 0x25 , 0x1E , 0x00 }
//init outside unsigned char K[5] , P[5] , asciiK[9] , asciiP[9] , tempb[9]; long value , amount , temp; K[0] = command [5]; K[1] = command [6]; K[2] = command [7]; K[3] = command [8]; P[0] = command [5]; P[1] = command [6]; P[2] = command [7]; P[3] = command [8]; hextoascii (asciiK, K, 4);//convert K to ascii asciiK[8] = 0x00;//put null terminator value = strtoul (asciiK, NULL, 16);//convert K to long hextoascii (asciiP, P, 4);//convert P to ascii asciiP[8] = 0x00;//put null terminator amount= strtoul (asciiP, NULL, 16);//convert P to long temp = value - amount; sprintf ( tempb, "%08lX\0" , temp); //copy the long to string with leading zeros asciitohex(K, tempb , 4);//convert to hex buf[4] = k[0]; buf[5] = k[1]; buf[6] = k[2]; buf[7] = k[3]; sendcom ( buf , 15);//send to serial function
void hextoascii (unsigned char *des , unsigned char *source , int size) { int i , temp , j; i=temp=j = 0; for (i=0; i<=size-1; i++) { temp = source[i]; temp = temp & 0xF0; temp = temp >> 4; //temp = temp + 0x30; temp = digithex[temp]; des[j] = temp; j++; temp = source[i]; temp = temp & 0x0F; //temp = temp + 0x30; temp = digithex[temp]; des[j] = temp; j++; } } //********************************* void asciitohex(unsigned char *des , unsigned char *source , int size) { int i , tmp0 , tmp1, j; i= tmp0 = tmp1 = j = 0; for (i=0; i<=size-1; i++) { tmp0 = source[j]; if (tmp0 < 0x40) {//its 0 to 9 num tmp0 = tmp0 - 0x30; } else {//its A to F tmp0 = tmp0 - 0x40; tmp0 = hex[tmp0]; } tmp0 = tmp0 << 4; j++; tmp1 = source[j]; if (tmp1 < 0x40) {//its 0 to 9 num tmp1 = tmp1 - 0x30; } else {//its A to F tmp1 = tmp1 - 0x40; tmp1 = hex[tmp1]; } des[i] = tmp0 + tmp1; j++; } }