#include <stdio.h>
#define DATA_LEN_TEMP 4 //twice DATA_LEN
#define ADDR_LEN_TEMP 4 //twice ADDR_LEN
#define DATA_LEN 2
#define TX_DATA_LEN 16
#define RX_DATA_LEN 16
#define ADDR_LEN 2
unsigned char DevAddr = 0;
unsigned char DataLen = 2;
unsigned char AddrLen = 2;
unsigned char I2CWData[DATA_LEN] = {0};
unsigned char SubAddr[ADDR_LEN] = {0};
unsigned char *CONVERT_DEVCODE_USERCODE(unsigned char *UserBuf,unsigned char *DevBuf)
{
unsigned char temp[DATA_LEN] = {0};
unsigned char i = 0;
unsigned char j = 0;
unsigned char count = 0;
unsigned char count1 = 0;
unsigned char Txdata[TX_DATA_LEN] = {0};
unsigned char *p;
p = UserBuf;
if((NULL == UserBuf) || (NULL == DevBuf))
{
return NULL;
}
/*strcpy(temp,buf);*/
for(i = 0;i< (DataLen*2);i++)
*(temp + i) = *(DevBuf + i);
for(j = 0;j < DataLen;j ++)
{
UserBuf[j*2] = temp[j] / 16;
UserBuf[j*2 + 1] = temp[j] % 16 ;
count = j * 2 + 1;
}
/*follow the valid data is '\n',others n valid data is null*/
/*pay attention:the data length must be less than TX_DATA_LEN/2,otherwise will not '\n'*/
if(count < TX_DATA_LEN)
UserBuf[count+1] = 58;
else
return NULL;
count1 = count +2;
for(;count1 < TX_DATA_LEN;count1 ++)
{
UserBuf[count1] = 48;
}
/*change machine number to ASCII*/
for(i = 0;i < TX_DATA_LEN;i++)
{
if(UserBuf[i] >= 0 && UserBuf[i] < 9)
UserBuf[i] = UserBuf[i] + '0';
else if(UserBuf[i] >= 10 && UserBuf[i] < 16)
UserBuf[i] = UserBuf[i] + 55; //Txdata[i] = Txdata[i] + 'A' -10
else if(UserBuf[i] == 48 || UserBuf[i] == 58)
UserBuf[i] = UserBuf[i] -48;
else
UserBuf[i] = UserBuf[i];
}
return p;
}
main(void)
{
unsigned char DevData[2] = {0xFF,0X0};
unsigned char UserData[TX_DATA_LEN] = {0x0};
int i =0;
CONVERT_DEVCODE_USERCODE(UserData,DevData);
for(;i < TX_DATA_LEN;i++)
{
printf("%d\n",UserData[i]);
}
}
以上函数是把设备数据转换成ASCII码。
在VC上仿真,此函数达到了预想功能。
但是在MPLAB IDE v8.80上仿真,CONVERT_DEVCODE_USERCODE(UserData,DevData);执行完,UserData中的数据还是为0。
不知道问题出在哪里了,请各位同学指教。 |