我试图将两个8位数字组合为一个无符号的int,但是无论我使用哪种类型的转换,结果仍然是带符号的数字。该代码使用适用于飞思卡尔微处理器MC9S08LH64的CodeWarrior 10.1进行编译。
我尝试过的事没有用
-移位并添加两个8位数字,然后在每个步骤中将其强制转换为unsigned int。
-union / struct将两个8位类型组合在一起,将它们和结果编号组合为unsigned int。
-使用无符号的int指针(下面的代码)
unsigned int acquire_sensor_voltage_internal_adc()
{ //this is internal ADC
unsigned int result;
unsigned int* data_ptr;
char print_buffer [50];
int_convert cvt;
//internal adc collecting counts of input voltage
//______________________________________________
//writing to ADCSC1A initiate the conversion sequence
ADCSC1A= 0x09;
while(!ADCSC1A_COCOA){}
cvt.parts.p0 = ADCRHA;
cvt.parts.p1 = ADCRLA;
data_ptr = &cvt.int_number;
result = (unsigned int)*data_ptr;
sprintf(print_buffer,"here!!!>>>>>>>%d\r\n",result);
serial_sendString(print_buffer,strlen(print_buffer));
//_______________________________________________
return (unsigned int) result;
}
//definition of int_convert from.h file
typedef union{
unsigned int int_number;
struct{
unsigned char p0;
unsigned char p1;
}parts;
}int_convert;
最佳答案
您可以尝试:
result = ((unsigned)ADCRHA) << 8 | (unsigned)ADCRHB;
然后使用正确的格式说明符
%u
代替%d
。