【发布时间】:2012-08-13 04:31:03
【问题描述】:
我试图将两个 8 位数字组合成一个无符号整数,但无论我使用什么类型的转换,结果仍然是有符号数。代码使用飞思卡尔微处理器 MC9S08LH64 的 CodeWarrior 10.1 编译。
我尝试过的方法不起作用 - 移位并添加两个 8 位数字,然后在每一步将它们强制转换为无符号整数。 - union/struct 将这两个 8 位类型组合起来,并将结果编号转换为 unsigned 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;
【问题讨论】: