Need Help With Fractional Data Types

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Need Help With Fractional Data Types

2,312 次查看
Anewguy
Contributor I
I'm having trouble converting the results of an A/D conversion
into Frac16 type.
I Have:

#define FRAC16(x) ((Frac16)((x) 1 ? ((x) >= -1 ? (x)*0x8000 : 0x8000) : 0x7FFF))

void AD1_OnEnd(void)
{
word ADResult=0;
Frac16 ADVal=0;
errB=AD1_GetValue16(&ADResult);
if(GetFiFoCnt() FifoSize)
{
ADVal=(FRAC16(ADResult));
PutFifo(ADVal);
}
}
ADResult returns the expected values.
ADVal returns only 0 or 32767!

What am I doing wrong here?

Also, is there a macro to go the other way? Frac16 to int ?

Thanks!
标签 (1)
0 项奖励
回复
2 回复数

1,130 次查看
J2MEJediMaster
Specialist I
As written, your macro will only return one or the highest possible value. It needs to be modified as so:

#define FRAC16(x) (x < 1 ? ( x >= -1 ? x * 0x7FFF : 0x8000) : 0x7FFF)


If you'll look at your macro, you only have the value one, not a x < 1 test at the start of your macro logic.

---Tom
0 项奖励
回复

1,130 次查看
Anewguy
Contributor I
Tom,

I actuallly have:

#define FRAC16(x) ((Frac16)((x) "less then symbol" 1 ? ((x) >= -1 ? (x)*0x8000 : 0x8000) : 0x7FFF))

The posting seems to have droped the less then symbol in 2 places.
It was in my code.
But I will try your code.

Thanks!
0 项奖励
回复